Merge pull request #3779 from nitro404/hotfix/case-insensitive-user-email

Fix bugs with case insensitive user emails
This commit is contained in:
Miroslav Bajtoš 2018-05-29 08:53:23 +02:00 committed by GitHub
commit c2077dfcb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View File

@ -1061,7 +1061,7 @@ module.exports = function(User) {
this.settings.ttl = this.settings.ttl || DEFAULT_TTL; this.settings.ttl = this.settings.ttl || DEFAULT_TTL;
UserModel.setter.email = function(value) { UserModel.setter.email = function(value) {
if (!UserModel.settings.caseSensitiveEmail) { if (!UserModel.settings.caseSensitiveEmail && typeof value === 'string') {
this.$email = value.toLowerCase(); this.$email = value.toLowerCase();
} else { } else {
this.$email = value; this.$email = value;

View File

@ -138,16 +138,29 @@ describe('User', function() {
}); });
}); });
it('Email is required', function(done) { it('fails when the required email is missing (case-sensitivity on)', () => {
User.create({password: '123'}, function(err) { User.create({password: '123'})
assert(err); .then(
assert.equal(err.name, 'ValidationError'); success => { throw new Error('create should have failed'); },
assert.equal(err.statusCode, 422); err => {
assert.equal(err.details.context, User.modelName); expect(err.name).to.equal('ValidationError');
assert.deepEqual(err.details.codes.email, ['presence']); expect(err.statusCode).to.equal(422);
expect(err.details.context).to.equal(User.modelName);
expect(err.details.codes.email).to.deep.equal(['presence']);
});
});
done(); it('fails when the required email is missing (case-sensitivity off)', () => {
}); User.settings.caseSensitiveEmail = false;
User.create({email: undefined, password: '123'})
.then(
success => { throw new Error('create should have failed'); },
err => {
expect(err.name).to.equal('ValidationError');
expect(err.statusCode).to.equal(422);
expect(err.details.context).to.equal(User.modelName);
expect(err.details.codes.email).to.deep.equal(['presence']);
});
}); });
// will change in future versions where password will be optional by default // will change in future versions where password will be optional by default