UserUpdaterTests.coffee 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/User/UserUpdater"
  7. expect = require("chai").expect
  8. tk = require('timekeeper')
  9. describe "UserUpdater", ->
  10. beforeEach ->
  11. tk.freeze(Date.now())
  12. @mongojs =
  13. db:{}
  14. ObjectId:(id)-> return id
  15. @UserGetter =
  16. getUserEmail: sinon.stub()
  17. getUserByAnyEmail: sinon.stub()
  18. ensureUniqueEmailAddress: sinon.stub()
  19. @logger = err: sinon.stub(), log: ->
  20. @addAffiliation = sinon.stub().callsArgWith(3, null)
  21. @removeAffiliation = sinon.stub().callsArgWith(2, null)
  22. @UserUpdater = SandboxedModule.require modulePath, requires:
  23. "logger-sharelatex": @logger
  24. "./UserGetter": @UserGetter
  25. './UserAffiliationsManager':
  26. addAffiliation: @addAffiliation
  27. removeAffiliation: @removeAffiliation
  28. "../../infrastructure/mongojs":@mongojs
  29. "metrics-sharelatex": timeAsyncMethod: sinon.stub()
  30. @stubbedUser =
  31. _id: "3131231"
  32. name:"bob"
  33. email:"hello@world.com"
  34. @newEmail = "bob@bob.com"
  35. afterEach ->
  36. tk.reset()
  37. describe 'changeEmailAddress', ->
  38. beforeEach ->
  39. @UserGetter.getUserEmail.callsArgWith(1, null, @stubbedUser.email)
  40. @UserUpdater.addEmailAddress = sinon.stub().callsArgWith(2)
  41. @UserUpdater.setDefaultEmailAddress = sinon.stub().callsArgWith(2)
  42. @UserUpdater.removeEmailAddress = sinon.stub().callsArgWith(2)
  43. it 'change email', (done)->
  44. @UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  45. should.not.exist(err)
  46. @UserUpdater.addEmailAddress.calledWith(
  47. @stubbedUser._id, @newEmail
  48. ).should.equal true
  49. @UserUpdater.setDefaultEmailAddress.calledWith(
  50. @stubbedUser._id, @newEmail
  51. ).should.equal true
  52. @UserUpdater.removeEmailAddress.calledWith(
  53. @stubbedUser._id, @stubbedUser.email
  54. ).should.equal true
  55. done()
  56. it 'handle error', (done)->
  57. @UserUpdater.removeEmailAddress.callsArgWith(2, new Error('nope'))
  58. @UserUpdater.changeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  59. should.exist(err)
  60. done()
  61. describe 'addEmailAddress', ->
  62. beforeEach ->
  63. @UserGetter.ensureUniqueEmailAddress = sinon.stub().callsArgWith(1)
  64. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null)
  65. it 'add email', (done)->
  66. @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
  67. @UserGetter.ensureUniqueEmailAddress.called.should.equal true
  68. should.not.exist(err)
  69. @UserUpdater.updateUser.calledWith(
  70. @stubbedUser._id,
  71. $push: { emails: { email: @newEmail, createdAt: sinon.match.date } }
  72. ).should.equal true
  73. done()
  74. it 'add affiliation', (done)->
  75. affiliationOptions =
  76. university: { id: 1 }
  77. role: 'Prof'
  78. department: 'Math'
  79. @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, affiliationOptions, (err)=>
  80. should.not.exist(err)
  81. @addAffiliation.calledOnce.should.equal true
  82. args = @addAffiliation.lastCall.args
  83. args[0].should.equal @stubbedUser._id
  84. args[1].should.equal @newEmail
  85. args[2].should.equal affiliationOptions
  86. done()
  87. it 'handle error', (done)->
  88. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
  89. @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
  90. @logger.err.called.should.equal true
  91. should.exist(err)
  92. done()
  93. it 'handle affiliation error', (done)->
  94. body = errors: 'affiliation error message'
  95. @addAffiliation.callsArgWith(3, new Error('nope'))
  96. @UserUpdater.addEmailAddress @stubbedUser._id, @newEmail, (err)=>
  97. should.exist(err)
  98. @UserUpdater.updateUser.called.should.equal false
  99. done()
  100. describe 'removeEmailAddress', ->
  101. beforeEach ->
  102. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, nMatched: 1)
  103. it 'remove email', (done)->
  104. @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  105. should.not.exist(err)
  106. @UserUpdater.updateUser.calledWith(
  107. { _id: @stubbedUser._id, email: { $ne: @newEmail } },
  108. $pull: { emails: { email: @newEmail } }
  109. ).should.equal true
  110. done()
  111. it 'remove affiliation', (done)->
  112. @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  113. should.not.exist(err)
  114. @removeAffiliation.calledOnce.should.equal true
  115. args = @removeAffiliation.lastCall.args
  116. args[0].should.equal @stubbedUser._id
  117. args[1].should.equal @newEmail
  118. done()
  119. it 'handle error', (done)->
  120. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
  121. @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  122. should.exist(err)
  123. done()
  124. it 'handle missed update', (done)->
  125. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
  126. @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  127. should.exist(err)
  128. done()
  129. it 'handle affiliation error', (done)->
  130. @removeAffiliation.callsArgWith(2, new Error('nope'))
  131. @UserUpdater.removeEmailAddress @stubbedUser._id, @newEmail, (err)=>
  132. should.exist(err)
  133. @UserUpdater.updateUser.called.should.equal false
  134. done()
  135. describe 'setDefaultEmailAddress', ->
  136. it 'set default', (done)->
  137. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
  138. @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
  139. should.not.exist(err)
  140. @UserUpdater.updateUser.calledWith(
  141. { _id: @stubbedUser._id, 'emails.email': @newEmail },
  142. $set: { email: @newEmail }
  143. ).should.equal true
  144. done()
  145. it 'handle error', (done)->
  146. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
  147. @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
  148. should.exist(err)
  149. done()
  150. it 'handle missed update', (done)->
  151. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
  152. @UserUpdater.setDefaultEmailAddress @stubbedUser._id, @newEmail, (err)=>
  153. should.exist(err)
  154. done()
  155. describe 'confirmEmail', ->
  156. it 'should update the email record', (done)->
  157. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 1)
  158. @UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
  159. should.not.exist(err)
  160. @UserUpdater.updateUser.calledWith(
  161. { _id: @stubbedUser._id, 'emails.email': @newEmail },
  162. $set: { 'emails.$.confirmedAt': new Date() }
  163. ).should.equal true
  164. done()
  165. it 'handle error', (done)->
  166. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, new Error('nope'))
  167. @UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
  168. should.exist(err)
  169. done()
  170. it 'handle missed update', (done)->
  171. @UserUpdater.updateUser = sinon.stub().callsArgWith(2, null, n: 0)
  172. @UserUpdater.confirmEmail @stubbedUser._id, @newEmail, (err)=>
  173. should.exist(err)
  174. done()