|
@@ -15,35 +15,135 @@ describe "UserUpdater", ->
|
|
|
db:{}
|
|
db:{}
|
|
|
ObjectId:(id)-> return id
|
|
ObjectId:(id)-> return id
|
|
|
@UserGetter =
|
|
@UserGetter =
|
|
|
- getUserByMainEmail: sinon.stub()
|
|
|
|
|
|
|
+ getUserEmail: sinon.stub()
|
|
|
|
|
+ getUserByAnyEmail: sinon.stub()
|
|
|
|
|
+ @logger = err: sinon.stub(), log: ->
|
|
|
@UserUpdater = SandboxedModule.require modulePath, requires:
|
|
@UserUpdater = SandboxedModule.require modulePath, requires:
|
|
|
"settings-sharelatex":@settings
|
|
"settings-sharelatex":@settings
|
|
|
- "logger-sharelatex": log:->
|
|
|
|
|
|
|
+ "logger-sharelatex": @logger
|
|
|
"./UserGetter": @UserGetter
|
|
"./UserGetter": @UserGetter
|
|
|
"../../infrastructure/mongojs":@mongojs
|
|
"../../infrastructure/mongojs":@mongojs
|
|
|
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
|
|
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
|
|
|
|
|
|
|
|
@stubbedUser =
|
|
@stubbedUser =
|
|
|
|
|
+ _id: "3131231"
|
|
|
name:"bob"
|
|
name:"bob"
|
|
|
email:"hello@world.com"
|
|
email:"hello@world.com"
|
|
|
- @user_id = "3131231"
|
|
|
|
|
@newEmail = "bob@bob.com"
|
|
@newEmail = "bob@bob.com"
|
|
|
|
|
|
|
|
- describe "changeEmailAddress", ->
|
|
|
|
|
|
|
+ describe 'changeEmailAddress', ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
- @UserUpdater.updateUser = sinon.stub().callsArgWith(2)
|
|
|
|
|
|
|
+ @UserGetter.getUserEmail.callsArgWith(1, null, @stubbedUser.email)
|
|
|
|
|
+ @UserUpdater.addEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
|
|
+ @UserUpdater.setDefaultEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
|
|
+ @UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
|
|
|
|
|
|
|
|
- it "should check if the new email already has an account", (done)->
|
|
|
|
|
- @UserGetter.getUserByMainEmail.callsArgWith(1, null, @stubbedUser)
|
|
|
|
|
- @UserUpdater.changeEmailAddress @user_id, @stubbedUser.email, (err)=>
|
|
|
|
|
- @UserUpdater.updateUser.called.should.equal false
|
|
|
|
|
|
|
+ it 'change email', (done)->
|
|
|
|
|
+ @UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.not.exist(err)
|
|
|
|
|
+ @UserUpdater.addEmailAddress.calledWith(
|
|
|
|
|
+ @stubbedUser._id, @newEmail
|
|
|
|
|
+ ).should.equal true
|
|
|
|
|
+ @UserUpdater.setDefaultEmailAddress.calledWith(
|
|
|
|
|
+ @stubbedUser._id, @newEmail
|
|
|
|
|
+ ).should.equal true
|
|
|
|
|
+ @UserUpdater.removeEmailAddress.calledWith(
|
|
|
|
|
+ @stubbedUser._id, @stubbedUser.email
|
|
|
|
|
+ ).should.equal true
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'handle error', (done)->
|
|
|
|
|
+ @UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
|
|
|
|
|
+ @UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ describe 'addEmailAddress', ->
|
|
|
|
|
+ beforeEach ->
|
|
|
|
|
+ @UserUpdater._ensureUniqueEmailAddress = sinon.stub().callsArgWith(1)
|
|
|
|
|
+
|
|
|
|
|
+ it 'add email', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null)
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ @UserUpdater._ensureUniqueEmailAddress.called.should.equal true
|
|
|
|
|
+ should.not.exist(err)
|
|
|
|
|
+ @UserUpdater.updateUser.calledWith(
|
|
|
|
|
+ @stubbedUser._id,
|
|
|
|
|
+ $push: { emails: { email: @newEmail, createdAt: sinon.match.date } }
|
|
|
|
|
+ ).should.equal true
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'handle error', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ @logger.err.called.should.equal true
|
|
|
should.exist(err)
|
|
should.exist(err)
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
|
|
+ describe 'removeEmailAddress', ->
|
|
|
|
|
+ it 'remove email', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1)
|
|
|
|
|
|
|
|
- it "should set the users password", (done)->
|
|
|
|
|
- @UserGetter.getUserByMainEmail.callsArgWith(1, null)
|
|
|
|
|
- @UserUpdater.changeEmailAddress @user_id, @newEmail, (err)=>
|
|
|
|
|
- @UserUpdater.updateUser.calledWith(@user_id, $set: { "email": @newEmail}).should.equal true
|
|
|
|
|
|
|
+ @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.not.exist(err)
|
|
|
|
|
+ @UserUpdater.updateUser.calledWith(
|
|
|
|
|
+ { _id: @stubbedUser._id, email: { $ne: @newEmail } },
|
|
|
|
|
+ $pull: { emails: { email: @newEmail } }
|
|
|
|
|
+ ).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
|
|
+ it 'handle error', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'handle missed update', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 0)
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ describe 'setDefaultEmailAddress', ->
|
|
|
|
|
+ it 'set default', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1)
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.not.exist(err)
|
|
|
|
|
+ @UserUpdater.updateUser.calledWith(
|
|
|
|
|
+ { _id: @stubbedUser._id, 'emails.email': @newEmail },
|
|
|
|
|
+ $set: { email: @newEmail }
|
|
|
|
|
+ ).should.equal true
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'handle error', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'handle missed update', (done)->
|
|
|
|
|
+ @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 0)
|
|
|
|
|
+
|
|
|
|
|
+ @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ describe '_ensureUniqueEmailAddress', ->
|
|
|
|
|
+ it 'should return error if existing user is found', (done)->
|
|
|
|
|
+ @UserGetter.getUserByAnyEmail.callsArgWith(1, null, @stubbedUser)
|
|
|
|
|
+ @UserUpdater._ensureUniqueEmailAddress @newEmail, (err)=>
|
|
|
|
|
+ should.exist(err)
|
|
|
|
|
+ done()
|
|
|
|
|
+
|
|
|
|
|
+ it 'should return null if no user is found', (done)->
|
|
|
|
|
+ @UserGetter.getUserByAnyEmail.callsArgWith(1)
|
|
|
|
|
+ @UserUpdater._ensureUniqueEmailAddress @newEmail, (err)=>
|
|
|
|
|
+ should.not.exist(err)
|
|
|
|
|
+ done()
|