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;
UserModel.setter.email = function(value) {
if (!UserModel.settings.caseSensitiveEmail) {
if (!UserModel.settings.caseSensitiveEmail && typeof value === 'string') {
this.$email = value.toLowerCase();
} else {
this.$email = value;

View File

@ -138,15 +138,28 @@ describe('User', function() {
});
});
it('Email is required', function(done) {
User.create({password: '123'}, function(err) {
assert(err);
assert.equal(err.name, 'ValidationError');
assert.equal(err.statusCode, 422);
assert.equal(err.details.context, User.modelName);
assert.deepEqual(err.details.codes.email, ['presence']);
it('fails when the required email is missing (case-sensitivity on)', () => {
User.create({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']);
});
});
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']);
});
});