Skip to content

Commit dff3c12

Browse files
committed
Merge pull request #1010 from kingcody/fix/userModel-updates
fix(model): fix update bugs with mongoose and sequelize
2 parents 484a443 + 1805975 commit dff3c12

File tree

4 files changed

+84
-37
lines changed

4 files changed

+84
-37
lines changed

Diff for: app/templates/server/api/user(auth)/user.model(mongooseModels).js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var validatePresenceOf = function(value) {
102102
UserSchema
103103
.pre('save', function(next) {
104104
// Handle new/update passwords
105-
if (this.password) {
105+
if (this.isModified('password')) {
106106
if (!validatePresenceOf(this.password)<% if (filters.oauth) { %> && authTypes.indexOf(this.provider) === -1<% } %>) {
107107
next(new Error('Invalid password'));
108108
}

Diff for: app/templates/server/api/user(auth)/user.model(sequelizeModels).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ module.exports = function(sequelize, DataTypes) {
8989
},
9090
beforeUpdate: function(user, fields, fn) {
9191
if (user.changed('password')) {
92-
user.updatePassword(fn);
92+
return user.updatePassword(fn);
9393
}
94+
fn();
9495
}
9596
},
9697

Diff for: app/templates/server/api/user(auth)/user.model.spec(mongooseModels).js

+42-18
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22

33
var app = require('../../app');
44
var User = require('./user.model');
5-
6-
var user = new User({
7-
provider: 'local',
8-
name: 'Fake User',
9-
10-
password: 'password'
11-
});
5+
var user;
6+
var genUser = function() {
7+
user = new User({
8+
provider: 'local',
9+
name: 'Fake User',
10+
11+
password: 'password'
12+
});
13+
return user;
14+
};
1215

1316
describe('User Model', function() {
1417
before(function() {
1518
// Clear users before testing
16-
return User.remove().exec();
19+
return User.removeAsync();
20+
});
21+
22+
beforeEach(function() {
23+
genUser();
1724
});
1825

1926
afterEach(function() {
20-
return User.remove().exec();
27+
return User.removeAsync();
2128
});
2229

2330
it('should begin with no users', function() {
@@ -28,21 +35,38 @@ describe('User Model', function() {
2835
it('should fail when saving a duplicate user', function() {
2936
return user.saveAsync()
3037
.then(function() {
31-
var userDup = new User(user);
38+
var userDup = genUser();
3239
return userDup.saveAsync();
3340
}).should.be.rejected;
3441
});
3542

36-
it('should fail when saving without an email', function() {
37-
user.email = '';
38-
return user.saveAsync().should.be.rejected;
43+
describe('#email', function() {
44+
it('should fail when saving without an email', function() {
45+
user.email = '';
46+
return user.saveAsync().should.be.rejected;
47+
});
3948
});
4049

41-
it('should authenticate user if password is valid', function() {
42-
user.authenticate('password').should.be.true;
43-
});
50+
describe('#password', function() {
51+
beforeEach(function() {
52+
return user.saveAsync();
53+
});
54+
55+
it('should authenticate user if valid', function() {
56+
user.authenticate('password').should.be.true;
57+
});
4458

45-
it('should not authenticate user if password is invalid', function() {
46-
user.authenticate('blah').should.not.be.true;
59+
it('should not authenticate user if invalid', function() {
60+
user.authenticate('blah').should.not.be.true;
61+
});
62+
63+
it('should remain the same hash unless the password is updated', function() {
64+
user.name = 'Test User';
65+
return user.saveAsync()
66+
.spread(function(u) {
67+
return u.authenticate('password');
68+
}).should.eventually.be.true;
69+
});
4770
});
71+
4872
});

Diff for: app/templates/server/api/user(auth)/user.model.spec(sequelizeModels).js

+39-17
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
var app = require('../../app');
44
var User = require('../../sqldb').User;
5-
6-
var userTemplate = {
7-
provider: 'local',
8-
name: 'Fake User',
9-
10-
password: 'password'
5+
var user;
6+
var genUser = function() {
7+
user = User.build({
8+
provider: 'local',
9+
name: 'Fake User',
10+
11+
password: 'password'
12+
});
13+
return user;
1114
};
1215

13-
var user = User.build(userTemplate);
14-
1516
describe('User Model', function() {
1617
before(function() {
1718
// Sync and clear users before testing
@@ -20,6 +21,10 @@ describe('User Model', function() {
2021
});
2122
});
2223

24+
beforeEach(function() {
25+
genUser();
26+
});
27+
2328
afterEach(function() {
2429
return User.destroy({ where: {} });
2530
});
@@ -32,21 +37,38 @@ describe('User Model', function() {
3237
it('should fail when saving a duplicate user', function() {
3338
return user.save()
3439
.then(function() {
35-
var userDup = User.build(userTemplate);
40+
var userDup = genUser();
3641
return userDup.save();
3742
}).should.be.rejected;
3843
});
3944

40-
it('should fail when saving without an email', function() {
41-
user.email = '';
42-
return user.save().should.be.rejected;
45+
describe('#email', function() {
46+
it('should fail when saving without an email', function() {
47+
user.email = '';
48+
return user.save().should.be.rejected;
49+
});
4350
});
4451

45-
it('should authenticate user if password is valid', function() {
46-
user.authenticate('password').should.be.true;
47-
});
52+
describe('#password', function() {
53+
beforeEach(function() {
54+
return user.save();
55+
});
56+
57+
it('should authenticate user if valid', function() {
58+
user.authenticate('password').should.be.true;
59+
});
4860

49-
it('should not authenticate user if password is invalid', function() {
50-
user.authenticate('blah').should.not.be.true;
61+
it('should not authenticate user if invalid', function() {
62+
user.authenticate('blah').should.not.be.true;
63+
});
64+
65+
it('should remain the same hash unless the password is updated', function() {
66+
user.name = 'Test User';
67+
return user.save()
68+
.then(function(u) {
69+
return u.authenticate('password');
70+
}).should.eventually.be.true;
71+
});
5172
});
73+
5274
});

0 commit comments

Comments
 (0)