Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Fixed multer, added test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperreality committed Aug 30, 2016
1 parent ad71421 commit b508661
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
8 changes: 5 additions & 3 deletions config/lib/multer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

module.exports.profileUploadFileFilter = function (req, file, cb) {
module.exports.profileUploadFileFilter = function (req, file, callback) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/gif') {
return cb(new Error('Only image files are allowed!'), false);
var err = new Error();
err.code = 'UNSUPPORTED_MEDIA_TYPE';
return callback(err, false);
}
cb(null, true);
callback(null, true);
};
5 changes: 4 additions & 1 deletion modules/core/server/controllers/errors.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ exports.getErrorMessage = function (err) {
case 11001:
message = getUniqueErrorMessage(err);
break;
case 'UNSUPPORTED_MEDIA_TYPE':
message = 'Unsupported filetype';
break;
case 'LIMIT_FILE_SIZE':
message = 'Image too big. Please maximum ' + (config.uploads.profileUpload.limits.fileSize / (1024 * 1024)).toFixed(2) + ' Mb files.';
message = 'Image too big. The maximum size allowed is ' + (config.uploads.profileUpload.limits.fileSize / (1024 * 1024)).toFixed(2) + ' Mb';
break;
case 'LIMIT_UNEXPECTED_FILE':
message = 'Missing `newProfilePicture` field';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ exports.update = function (req, res) {
*/
exports.changeProfilePicture = function (req, res) {
var user = req.user;
var upload = multer(config.uploads.profileUpload).single('newProfilePicture');
var profileUploadFileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;
var existingImageUrl;

// Filtering to upload only images
upload.fileFilter = profileUploadFileFilter;
var multerConfig = config.uploads.profileUpload;
multerConfig.fileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;
var upload = multer(multerConfig).single('newProfilePicture');

if (user) {
existingImageUrl = user.profileImageURL;
Expand Down
20 changes: 20 additions & 0 deletions modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,26 @@ describe('User CRUD tests', function () {
});
});

it('should not be able to upload a non-image file as a profile picture', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr, signinRes) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}

agent.post('/api/users/picture')
.attach('newProfilePicture', './modules/users/tests/server/user.server.model.tests.js')
.send(credentials)
.expect(400)
.end(function (userInfoErr, userInfoRes) {
done(userInfoErr);
});
});
});

it('should not be able to change profile picture if attach a picture with a different field name', function (done) {
agent.post('/api/auth/signin')
.send(credentials)
Expand Down

0 comments on commit b508661

Please sign in to comment.