From b2bc449e243726c9e2887478f495cb04375d62ea Mon Sep 17 00:00:00 2001 From: Kevin Scroggins Date: Mon, 29 Jan 2018 13:03:40 -0500 Subject: [PATCH] Add check for undefined user email in setter --- common/models/user.js | 2 +- test/user.test.js | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/common/models/user.js b/common/models/user.js index a3baecb2..bdbe74c7 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -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; diff --git a/test/user.test.js b/test/user.test.js index a5ef88e4..5509a407 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -138,16 +138,29 @@ 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']); + }); }); // will change in future versions where password will be optional by default