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

Commit

Permalink
fix(users): don't fail on missing old image on image upload (#1839)
Browse files Browse the repository at this point in the history
Fixes scenarios where previously when old image file would be missing, uploading new image file over it would fail because unlinking previous file fails.
  • Loading branch information
simison authored Aug 13, 2017
1 parent 1e3eeb7 commit be88a2c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ exports.changeProfilePicture = function (req, res) {
if (existingImageUrl !== User.schema.path('profileImageURL').defaultValue) {
fs.unlink(existingImageUrl, function (unlinkError) {
if (unlinkError) {
console.log(unlinkError);

// If file didn't exist, no need to reject promise
if (unlinkError.code === 'ENOENT') {
console.log('Removing profile image failed because file did not exist.');
return resolve();
}

console.error(unlinkError);

reject({
message: 'Error occurred while deleting old profile picture'
});
Expand Down
34 changes: 34 additions & 0 deletions modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var semver = require('semver'),
path = require('path'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
config = require(path.resolve('./config/config')),
express = require(path.resolve('./config/lib/express'));

/**
Expand Down Expand Up @@ -1078,6 +1079,39 @@ describe('User CRUD tests', function () {
});
});

it('should be able to change profile picture and not fail if existing picture file does not exist', function (done) {

user.profileImageURL = config.uploads.profile.image.dest + 'non-existing.png';

user.save(function(saveErr) {
// Handle error
if (saveErr) {
return done(saveErr);
}

agent.post('/api/auth/signin')
.send(credentials)
.expect(200)
.end(function (signinErr) {
// Handle signin error
if (signinErr) {
return done(signinErr);
}

agent.post('/api/users/picture')
.attach('newProfilePicture', './modules/users/client/img/profile/default.png')
.expect(200)
.end(function (userInfoErr) {

should.not.exist(userInfoErr);

return done();
});
});

});
});

afterEach(function (done) {
User.remove().exec(done);
});
Expand Down

0 comments on commit be88a2c

Please sign in to comment.