Skip to content

Commit f9a5808

Browse files
authored
Merge pull request #2795 from strongloop/PartialUpdate
Add partial update test case
2 parents 9fb3931 + 5f5e874 commit f9a5808

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

common/models/user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,9 @@ module.exports = function(User) {
689689
UserModel.observe('after save', function afterEmailUpdate(ctx, next) {
690690
if (!ctx.Model.relations.accessTokens) return next();
691691
var AccessToken = ctx.Model.relations.accessTokens.modelTo;
692+
if (!ctx.instance && !ctx.data) return next();
692693
var newEmail = (ctx.instance || ctx.data).email;
694+
if (!newEmail) return next();
693695
if (!ctx.hookState.originalUserData) return next();
694696
var idsToExpire = ctx.hookState.originalUserData.filter(function(u) {
695697
return u.email !== newEmail;

test/user.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,43 @@ describe('User', function() {
19771977
], done);
19781978
});
19791979

1980+
it('keeps sessions AS IS if non-email property is changed using updateAll', function(done) {
1981+
var userPartial;
1982+
async.series([
1983+
function createPartialUser(next) {
1984+
User.create(
1985+
{ email: '[email protected]', password: 'pass1', age: 25 },
1986+
function(err, partialInstance) {
1987+
if (err) return next (err);
1988+
userPartial = partialInstance;
1989+
next();
1990+
});
1991+
},
1992+
function loginPartiallUser(next) {
1993+
User.login({ email: '[email protected]', password: 'pass1' }, function(err, ats) {
1994+
if (err) return next (err);
1995+
next();
1996+
});
1997+
},
1998+
function updatePartialUser(next) {
1999+
User.updateAll(
2000+
{ id: userPartial.id },
2001+
{ age: userPartial.age + 1 },
2002+
function(err, info) {
2003+
if (err) return next (err);
2004+
next();
2005+
});
2006+
},
2007+
function verifyTokensOfPartialUser(next) {
2008+
AccessToken.find({ where: { userId: userPartial.id }}, function(err, tokens1) {
2009+
if (err) return next (err);
2010+
expect(tokens1.length).to.equal(1);
2011+
next();
2012+
});
2013+
},
2014+
], done);
2015+
});
2016+
19802017
function assertPreservedToken(done) {
19812018
AccessToken.find({ where: { userId: user.id }}, function(err, tokens) {
19822019
if (err) return done(err);

0 commit comments

Comments
 (0)