add affiliation before confirming email

This commit is contained in:
Tim Alby 2018-07-04 15:47:49 +02:00
parent 9d4df4271a
commit cfd5c65be4
2 changed files with 35 additions and 15 deletions

View file

@ -59,7 +59,7 @@ module.exports = UserUpdater =
addAffiliation userId, newEmail, affiliationOptions, (error) => addAffiliation userId, newEmail, affiliationOptions, (error) =>
if error? if error?
logger.err error: error, 'problem adding affiliation' logger.err error: error, 'problem adding affiliation while adding email'
return callback(error) return callback(error)
update = $push: emails: email: newEmail, createdAt: new Date() update = $push: emails: email: newEmail, createdAt: new Date()
@ -110,6 +110,11 @@ module.exports = UserUpdater =
email = EmailHelper.parseEmail(email) email = EmailHelper.parseEmail(email)
return callback(new Error('invalid email')) if !email? return callback(new Error('invalid email')) if !email?
logger.log {userId, email}, 'confirming user email' logger.log {userId, email}, 'confirming user email'
addAffiliation userId, email, (error) =>
if error?
logger.err error: error, 'problem adding affiliation while confirming email'
return callback(error)
query = query =
_id: userId _id: userId
'emails.email': email 'emails.email': email

View file

@ -19,7 +19,7 @@ describe "UserUpdater", ->
getUserByAnyEmail: sinon.stub() getUserByAnyEmail: sinon.stub()
ensureUniqueEmailAddress: sinon.stub() ensureUniqueEmailAddress: sinon.stub()
@logger = err: sinon.stub(), log: -> @logger = err: sinon.stub(), log: ->
@addAffiliation = sinon.stub().callsArgWith(3, null) @addAffiliation = sinon.stub().yields()
@removeAffiliation = sinon.stub().callsArgWith(2, null) @removeAffiliation = sinon.stub().callsArgWith(2, null)
@UserUpdater = SandboxedModule.require modulePath, requires: @UserUpdater = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger "logger-sharelatex": @logger
@ -201,9 +201,10 @@ describe "UserUpdater", ->
done() done()
describe 'confirmEmail', -> describe 'confirmEmail', ->
it 'should update the email record', (done)-> beforeEach ->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1) @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
it 'should update the email record', (done)->
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=> @UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
should.not.exist(err) should.not.exist(err)
@UserUpdater.updateUser.calledWith( @UserUpdater.updateUser.calledWith(
@ -212,6 +213,13 @@ describe "UserUpdater", ->
).should.equal true ).should.equal true
done() done()
it 'add affiliation', (done)->
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
should.not.exist(err)
@addAffiliation.calledOnce.should.equal true
sinon.assert.calledWith(@addAffiliation, @stubbedUser._id, @newEmail)
done()
it 'handle error', (done)-> it 'handle error', (done)->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope')) @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
@ -230,3 +238,10 @@ describe "UserUpdater", ->
@UserUpdater.confirmEmail @stubbedUser._id, '@', (err)=> @UserUpdater.confirmEmail @stubbedUser._id, '@', (err)=>
should.exist(err) should.exist(err)
done() done()
it 'handle affiliation error', (done)->
@addAffiliation.callsArgWith(2, new Error('nope'))
@UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
should.exist(err)
@UserUpdater.updateUser.called.should.equal false
done()