UserControllerTests.coffee 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/User/UserController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. MockResponse = require "../helpers/MockResponse"
  9. MockRequest = require "../helpers/MockRequest"
  10. ObjectId = require("mongojs").ObjectId
  11. assert = require("assert")
  12. describe "UserController", ->
  13. beforeEach ->
  14. @user_id = "323123"
  15. @user =
  16. _id:@user_id
  17. save: sinon.stub().callsArgWith(0)
  18. ace:{}
  19. @req =
  20. user: {}
  21. session:
  22. destroy:->
  23. user :
  24. _id : @user_id
  25. email:"old@something.com"
  26. body:{}
  27. @UserDeleter =
  28. deleteUser: sinon.stub().callsArgWith(1)
  29. @UserLocator =
  30. findById: sinon.stub().callsArgWith(1, null, @user)
  31. @User =
  32. findById: sinon.stub().callsArgWith(1, null, @user)
  33. @NewsLetterManager =
  34. unsubscribe: sinon.stub().callsArgWith(1)
  35. @UserRegistrationHandler =
  36. registerNewUser: sinon.stub()
  37. @AuthenticationController =
  38. establishUserSession: sinon.stub().callsArg(2)
  39. getLoggedInUserId: sinon.stub().returns(@user._id)
  40. getSessionUser: sinon.stub().returns(@req.session.user)
  41. setInSessionUser: sinon.stub()
  42. @AuthenticationManager =
  43. authenticate: sinon.stub()
  44. setUserPassword: sinon.stub()
  45. @ReferalAllocator =
  46. allocate:sinon.stub()
  47. @SubscriptionDomainHandler =
  48. autoAllocate:sinon.stub()
  49. @UserUpdater =
  50. changeEmailAddress:sinon.stub()
  51. @settings =
  52. siteUrl: "sharelatex.example.com"
  53. @UserHandler =
  54. populateGroupLicenceInvite:sinon.stub().callsArgWith(1)
  55. @UserSessionsManager =
  56. trackSession: sinon.stub()
  57. untrackSession: sinon.stub()
  58. revokeAllUserSessions: sinon.stub().callsArgWith(2, null)
  59. @UserController = SandboxedModule.require modulePath, requires:
  60. "./UserLocator": @UserLocator
  61. "./UserDeleter": @UserDeleter
  62. "./UserUpdater":@UserUpdater
  63. "../../models/User": User:@User
  64. '../Newsletter/NewsletterManager':@NewsLetterManager
  65. "./UserRegistrationHandler":@UserRegistrationHandler
  66. "../Authentication/AuthenticationController": @AuthenticationController
  67. "../Authentication/AuthenticationManager": @AuthenticationManager
  68. "../Referal/ReferalAllocator":@ReferalAllocator
  69. "../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler
  70. "./UserHandler":@UserHandler
  71. "./UserSessionsManager": @UserSessionsManager
  72. "settings-sharelatex": @settings
  73. "logger-sharelatex":
  74. log:->
  75. err:->
  76. "../../infrastructure/Metrics": inc:->
  77. @res =
  78. send: sinon.stub()
  79. sendStatus: sinon.stub()
  80. json: sinon.stub()
  81. @next = sinon.stub()
  82. describe "deleteUser", ->
  83. it "should delete the user", (done)->
  84. @res.sendStatus = (code)=>
  85. @UserDeleter.deleteUser.calledWith(@user_id)
  86. code.should.equal 200
  87. done()
  88. @UserController.deleteUser @req, @res
  89. describe "unsubscribe", ->
  90. it "should send the user to unsubscribe", (done)->
  91. @res.send = (code)=>
  92. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  93. done()
  94. @UserController.unsubscribe @req, @res
  95. describe "updateUserSettings", ->
  96. beforeEach ->
  97. @newEmail = "hello@world.com"
  98. it "should call save", (done)->
  99. @req.body = {}
  100. @res.sendStatus = (code)=>
  101. @user.save.called.should.equal true
  102. done()
  103. @UserController.updateUserSettings @req, @res
  104. it "should set the first name", (done)->
  105. @req.body =
  106. first_name: "bobby "
  107. @res.sendStatus = (code)=>
  108. @user.first_name.should.equal "bobby"
  109. done()
  110. @UserController.updateUserSettings @req, @res
  111. it "should set the role", (done)->
  112. @req.body =
  113. role: "student"
  114. @res.sendStatus = (code)=>
  115. @user.role.should.equal "student"
  116. done()
  117. @UserController.updateUserSettings @req, @res
  118. it "should set the institution", (done)->
  119. @req.body =
  120. institution: "MIT"
  121. @res.sendStatus = (code)=>
  122. @user.institution.should.equal "MIT"
  123. done()
  124. @UserController.updateUserSettings @req, @res
  125. it "should set some props on ace", (done)->
  126. @req.body =
  127. theme: "something"
  128. @res.sendStatus = (code)=>
  129. @user.ace.theme.should.equal "something"
  130. done()
  131. @UserController.updateUserSettings @req, @res
  132. it "should send an error if the email is 0 len", (done)->
  133. @req.body.email = ""
  134. @res.sendStatus = (code)->
  135. code.should.equal 400
  136. done()
  137. @UserController.updateUserSettings @req, @res
  138. it "should send an error if the email does not contain an @", (done)->
  139. @req.body.email = "bob at something dot com"
  140. @res.sendStatus = (code)->
  141. code.should.equal 400
  142. done()
  143. @UserController.updateUserSettings @req, @res
  144. it "should call the user updater with the new email and user _id", (done)->
  145. @req.body.email = @newEmail.toUpperCase()
  146. @UserUpdater.changeEmailAddress.callsArgWith(2)
  147. @res.sendStatus = (code)=>
  148. code.should.equal 200
  149. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  150. done()
  151. @UserController.updateUserSettings @req, @res
  152. it "should update the email on the session", (done)->
  153. @req.body.email = @newEmail.toUpperCase()
  154. @UserUpdater.changeEmailAddress.callsArgWith(2)
  155. callcount = 0
  156. @User.findById = (id, cb)=>
  157. if ++callcount == 2
  158. @user.email = @newEmail
  159. cb(null, @user)
  160. @res.sendStatus = (code)=>
  161. code.should.equal 200
  162. @AuthenticationController.setInSessionUser.calledWith(
  163. @req, {email: @newEmail, first_name: undefined, last_name: undefined}
  164. ).should.equal true
  165. done()
  166. @UserController.updateUserSettings @req, @res
  167. it "should call populateGroupLicenceInvite", (done)->
  168. @req.body.email = @newEmail.toUpperCase()
  169. @UserUpdater.changeEmailAddress.callsArgWith(2)
  170. @res.sendStatus = (code)=>
  171. code.should.equal 200
  172. @UserHandler.populateGroupLicenceInvite.calledWith(@user).should.equal true
  173. done()
  174. @UserController.updateUserSettings @req, @res
  175. describe "logout", ->
  176. it "should destroy the session", (done)->
  177. @req.session.destroy = sinon.stub().callsArgWith(0)
  178. @res.redirect = (url)=>
  179. url.should.equal "/login"
  180. @req.session.destroy.called.should.equal true
  181. done()
  182. @UserController.logout @req, @res
  183. describe "register", ->
  184. beforeEach ->
  185. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  186. @req.body.email = @user.email = @email = "email@example.com"
  187. @UserController.register @req, @res
  188. it "should register the user and send them an email", ->
  189. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  190. .calledWith(@email)
  191. .should.equal true
  192. it "should return the user and activation url", ->
  193. @res.json
  194. .calledWith({
  195. email: @email,
  196. setNewPasswordUrl: @url
  197. })
  198. .should.equal true
  199. describe 'clearSessions', ->
  200. it 'should call revokeAllUserSessions', (done) ->
  201. @UserController.clearSessions @req, @res
  202. @UserSessionsManager.revokeAllUserSessions.callCount.should.equal 1
  203. done()
  204. it 'send a 201 response', (done) ->
  205. @res.sendStatus = (status) =>
  206. status.should.equal 201
  207. done()
  208. @UserController.clearSessions @req, @res
  209. describe 'when revokeAllUserSessions produces an error', ->
  210. it 'should call next with an error', (done) ->
  211. @UserSessionsManager.revokeAllUserSessions.callsArgWith(2, new Error('woops'))
  212. next = (err) =>
  213. expect(err).to.not.equal null
  214. expect(err).to.be.instanceof Error
  215. done()
  216. @UserController.clearSessions @req, @res, next
  217. describe "changePassword", ->
  218. it "should check the old password is the current one at the moment", (done)->
  219. @AuthenticationManager.authenticate.callsArgWith(2)
  220. @req.body =
  221. currentPassword: "oldpasshere"
  222. @res.send = =>
  223. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  224. @AuthenticationManager.setUserPassword.called.should.equal false
  225. done()
  226. @UserController.changePassword @req, @res
  227. it "it should not set the new password if they do not match", (done)->
  228. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  229. @req.body =
  230. newPassword1: "1"
  231. newPassword2: "2"
  232. @res.send = =>
  233. @AuthenticationManager.setUserPassword.called.should.equal false
  234. done()
  235. @UserController.changePassword @req, @res
  236. it "should set the new password if they do match", (done)->
  237. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  238. @AuthenticationManager.setUserPassword.callsArgWith(2)
  239. @req.body =
  240. newPassword1: "newpass"
  241. newPassword2: "newpass"
  242. @res.send = =>
  243. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  244. done()
  245. @UserController.changePassword @req, @res