UserControllerTests.coffee 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. @UserDeleter =
  20. deleteUser: sinon.stub().callsArgWith(1)
  21. @UserLocator =
  22. findById: sinon.stub().callsArgWith(1, null, @user)
  23. @User =
  24. findById: sinon.stub().callsArgWith(1, null, @user)
  25. @NewsLetterManager =
  26. unsubscribe: sinon.stub().callsArgWith(1)
  27. @UserRegistrationHandler =
  28. registerNewUser: sinon.stub()
  29. @AuthenticationController = {}
  30. @AuthenticationManager =
  31. authenticate: sinon.stub()
  32. setUserPassword: sinon.stub()
  33. @ReferalAllocator =
  34. allocate:sinon.stub()
  35. @UserUpdater =
  36. changeEmailAddress:sinon.stub()
  37. @UserController = SandboxedModule.require modulePath, requires:
  38. "./UserLocator": @UserLocator
  39. "./UserDeleter": @UserDeleter
  40. "./UserUpdater":@UserUpdater
  41. "../../models/User": User:@User
  42. '../Newsletter/NewsletterManager':@NewsLetterManager
  43. "./UserRegistrationHandler":@UserRegistrationHandler
  44. "../Authentication/AuthenticationController": @AuthenticationController
  45. "../Authentication/AuthenticationManager": @AuthenticationManager
  46. "../Referal/ReferalAllocator":@ReferalAllocator
  47. "logger-sharelatex": {log:->}
  48. @req =
  49. session:
  50. destroy:->
  51. user :
  52. _id : @user_id
  53. body:{}
  54. @res = {}
  55. @next = sinon.stub()
  56. describe "deleteUser", ->
  57. it "should delete the user", (done)->
  58. @res.send = (code)=>
  59. @UserDeleter.deleteUser.calledWith(@user_id)
  60. code.should.equal 200
  61. done()
  62. @UserController.deleteUser @req, @res
  63. describe "unsubscribe", ->
  64. it "should send the user to unsubscribe", (done)->
  65. @res.send = (code)=>
  66. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  67. done()
  68. @UserController.unsubscribe @req, @res
  69. describe "updateUserSettings", ->
  70. beforeEach ->
  71. @newEmail = "hello@world.com"
  72. it "should call save", (done)->
  73. @req.body = {}
  74. @res.send = (code)=>
  75. @user.save.called.should.equal true
  76. done()
  77. @UserController.updateUserSettings @req, @res
  78. it "should set the first name", (done)->
  79. @req.body =
  80. first_name: "bobby "
  81. @res.send = (code)=>
  82. @user.first_name.should.equal "bobby"
  83. done()
  84. @UserController.updateUserSettings @req, @res
  85. it "should set some props on ace", (done)->
  86. @req.body =
  87. theme: "something"
  88. @res.send = (code)=>
  89. @user.ace.theme.should.equal "something"
  90. done()
  91. @UserController.updateUserSettings @req, @res
  92. it "should send an error if the email is 0 len", (done)->
  93. @req.body.email = ""
  94. @res.send = (code)->
  95. code.should.equal 400
  96. done()
  97. @UserController.updateUserSettings @req, @res
  98. it "should send an error if the email does not contain an @", (done)->
  99. @req.body.email = "bob at something dot com"
  100. @res.send = (code)->
  101. code.should.equal 400
  102. done()
  103. @UserController.updateUserSettings @req, @res
  104. it "should call the user updater with the new email and user _id", (done)->
  105. @req.body.email = @newEmail
  106. @UserUpdater.changeEmailAddress.callsArgWith(2)
  107. @res.send = (code)=>
  108. code.should.equal 200
  109. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  110. done()
  111. @UserController.updateUserSettings @req, @res
  112. describe "logout", ->
  113. it "should destroy the session", (done)->
  114. @req.session.destroy = sinon.stub().callsArgWith(0)
  115. @res.redirect = (url)=>
  116. url.should.equal "/login"
  117. @req.session.destroy.called.should.equal true
  118. done()
  119. @UserController.logout @req, @res
  120. describe "register", ->
  121. it "should ask the UserRegistrationHandler to register user", (done)->
  122. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  123. @res.send = =>
  124. @UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
  125. done()
  126. @UserController.register @req, @res
  127. it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
  128. @UserRegistrationHandler.registerNewUser.callsArgWith(1, "EmailAlreadyRegisterd")
  129. @AuthenticationController.login = (req, res)=>
  130. assert.deepEqual req, @req
  131. assert.deepEqual res, @res
  132. done()
  133. @UserController.register @req, @res
  134. it "should put the user on the session and mark them as justRegistered", (done)->
  135. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  136. @res.send = =>
  137. assert.deepEqual @user, @req.session.user
  138. assert.equal @req.session.justRegistered, true
  139. done()
  140. @UserController.register @req, @res
  141. it "should redirect to project page", (done)->
  142. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  143. @res.send = (opts)=>
  144. opts.redir.should.equal "/project"
  145. done()
  146. @UserController.register @req, @res
  147. it "should redirect passed redir if it exists", (done)->
  148. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  149. @req.body.redir = "/somewhere"
  150. @res.send = (opts)=>
  151. opts.redir.should.equal "/somewhere"
  152. done()
  153. @UserController.register @req, @res
  154. it "should allocate the referals", (done)->
  155. @req.session =
  156. referal_id : "23123"
  157. referal_source : "email"
  158. referal_medium : "bob"
  159. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  160. @req.body.redir = "/somewhere"
  161. @res.send = (opts)=>
  162. @ReferalAllocator.allocate.calledWith(@req.session.referal_id, @user._id, @req.session.referal_source, @req.session.referal_medium).should.equal true
  163. done()
  164. @UserController.register @req, @res
  165. describe "changePassword", ->
  166. it "should check the old password is the current one at the moment", (done)->
  167. @AuthenticationManager.authenticate.callsArgWith(2)
  168. @req.body =
  169. currentPassword: "oldpasshere"
  170. @res.send = =>
  171. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  172. @AuthenticationManager.setUserPassword.called.should.equal false
  173. done()
  174. @UserController.changePassword @req, @res
  175. it "it should not set the new password if they do not match", (done)->
  176. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  177. @req.body =
  178. newPassword1: "1"
  179. newPassword2: "2"
  180. @res.send = =>
  181. @AuthenticationManager.setUserPassword.called.should.equal false
  182. done()
  183. @UserController.changePassword @req, @res
  184. it "should set the new password if they do match", (done)->
  185. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  186. @AuthenticationManager.setUserPassword.callsArgWith(2)
  187. @req.body =
  188. newPassword1: "newpass"
  189. newPassword2: "newpass"
  190. @res.send = =>
  191. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  192. done()
  193. @UserController.changePassword @req, @res