parse email in UserUpdater

This commit is contained in:
Tim Alby 2018-07-03 20:58:37 +02:00
parent e22d2c8da7
commit 3a541307b4
2 changed files with 33 additions and 0 deletions

View file

@ -29,6 +29,8 @@ module.exports = UserUpdater =
# emails and the default rather than calling this method directly # emails and the default rather than calling this method directly
# #
changeEmailAddress: (userId, newEmail, callback)-> changeEmailAddress: (userId, newEmail, callback)->
newEmail = EmailHelper.parseEmail(newEmail)
return callback(new Error('invalid email')) if !newEmail?
logger.log userId: userId, newEmail: newEmail, "updaing email address of user" logger.log userId: userId, newEmail: newEmail, "updaing email address of user"
oldEmail = null oldEmail = null
@ -49,6 +51,8 @@ module.exports = UserUpdater =
unless callback? # affiliationOptions is optional unless callback? # affiliationOptions is optional
callback = affiliationOptions callback = affiliationOptions
affiliationOptions = {} affiliationOptions = {}
newEmail = EmailHelper.parseEmail(newEmail)
return callback(new Error('invalid email')) if !newEmail?
UserGetter.ensureUniqueEmailAddress newEmail, (error) => UserGetter.ensureUniqueEmailAddress newEmail, (error) =>
return callback(error) if error? return callback(error) if error?
@ -69,6 +73,8 @@ module.exports = UserUpdater =
# remove one of the user's email addresses. The email cannot be the user's # remove one of the user's email addresses. The email cannot be the user's
# default email address # default email address
removeEmailAddress: (userId, email, callback) -> removeEmailAddress: (userId, email, callback) ->
email = EmailHelper.parseEmail(email)
return callback(new Error('invalid email')) if !email?
removeAffiliation userId, email, (error) => removeAffiliation userId, email, (error) =>
if error? if error?
logger.err error: error, 'problem removing affiliation' logger.err error: error, 'problem removing affiliation'
@ -88,6 +94,8 @@ module.exports = UserUpdater =
# set the default email address by setting the `email` attribute. The email # set the default email address by setting the `email` attribute. The email
# must be one of the user's multiple emails (`emails` attribute) # must be one of the user's multiple emails (`emails` attribute)
setDefaultEmailAddress: (userId, email, callback) -> setDefaultEmailAddress: (userId, email, callback) ->
email = EmailHelper.parseEmail(email)
return callback(new Error('invalid email')) if !email?
query = _id: userId, 'emails.email': email query = _id: userId, 'emails.email': email
update = $set: email: email update = $set: email: email
@updateUser query, update, (error, res) -> @updateUser query, update, (error, res) ->

View file

@ -60,6 +60,11 @@ describe "UserUpdater", ->
).should.equal true ).should.equal true
done() done()
it 'validates email', (done)->
@UserUpdater.changeEmailAddress @stubbedUser._id, 'foo', (err)=>
should.exist(err)
done()
it 'handle error', (done)-> it 'handle error', (done)->
@UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope')) @UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
@UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=> @UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
@ -111,6 +116,11 @@ describe "UserUpdater", ->
@UserUpdater.updateUser.called.should.equal false @UserUpdater.updateUser.called.should.equal false
done() done()
it 'validates email', (done)->
@UserUpdater.addEmailAddress @stubbedUser._id, 'bar', (err)=>
should.exist(err)
done()
describe 'removeEmailAddress', -> describe 'removeEmailAddress', ->
beforeEach -> beforeEach ->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1) @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1)
@ -154,6 +164,11 @@ describe "UserUpdater", ->
@UserUpdater.updateUser.called.should.equal false @UserUpdater.updateUser.called.should.equal false
done() done()
it 'validates email', (done)->
@UserUpdater.removeEmailAddress @stubbedUser._id, 'baz', (err)=>
should.exist(err)
done()
describe 'setDefaultEmailAddress', -> describe 'setDefaultEmailAddress', ->
it 'set default', (done)-> it 'set default', (done)->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1) @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
@ -180,6 +195,11 @@ describe "UserUpdater", ->
should.exist(err) should.exist(err)
done() done()
it 'validates email', (done)->
@UserUpdater.setDefaultEmailAddress @stubbedUser._id, '.edu', (err)=>
should.exist(err)
done()
describe 'confirmEmail', -> describe 'confirmEmail', ->
it 'should update the email record', (done)-> it 'should update the email record', (done)->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1) @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
@ -205,3 +225,8 @@ describe "UserUpdater", ->
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=> @UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
should.exist(err) should.exist(err)
done() done()
it 'validates email', (done)->
@UserUpdater.confirmEmail @stubbedUser._id, '@', (err)=>
should.exist(err)
done()