UserControllerTests.coffee 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 return an error if the email address is null", (done)->
  93. @req.body.email = null
  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 is 0 len", (done)->
  99. @req.body.email = ""
  100. @res.send = (code)->
  101. code.should.equal 400
  102. done()
  103. @UserController.updateUserSettings @req, @res
  104. it "should send an error if the email does not contain an @", (done)->
  105. @req.body.email = "bob at something dot com"
  106. @res.send = (code)->
  107. code.should.equal 400
  108. done()
  109. @UserController.updateUserSettings @req, @res
  110. it "should call the user updater with the new email and user _id", (done)->
  111. @req.body.email = @newEmail
  112. @UserUpdater.changeEmailAddress.callsArgWith(2)
  113. @res.send = (code)=>
  114. code.should.equal 200
  115. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  116. done()
  117. @UserController.updateUserSettings @req, @res
  118. describe "logout", ->
  119. it "should destroy the session", (done)->
  120. @req.session.destroy = sinon.stub().callsArgWith(0)
  121. @res.redirect = (url)=>
  122. url.should.equal "/login"
  123. @req.session.destroy.called.should.equal true
  124. done()
  125. @UserController.logout @req, @res
  126. describe "register", ->
  127. it "should ask the UserRegistrationHandler to register user", (done)->
  128. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  129. @res.send = =>
  130. @UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
  131. done()
  132. @UserController.register @req, @res
  133. it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
  134. @UserRegistrationHandler.registerNewUser.callsArgWith(1, "EmailAlreadyRegisterd")
  135. @AuthenticationController.login = (req, res)=>
  136. assert.deepEqual req, @req
  137. assert.deepEqual res, @res
  138. done()
  139. @UserController.register @req, @res
  140. it "should put the user on the session and mark them as justRegistered", (done)->
  141. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  142. @res.send = =>
  143. assert.deepEqual @user, @req.session.user
  144. assert.equal @req.session.justRegistered, true
  145. done()
  146. @UserController.register @req, @res
  147. it "should redirect to project page", (done)->
  148. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  149. @res.send = (opts)=>
  150. opts.redir.should.equal "/project"
  151. done()
  152. @UserController.register @req, @res
  153. it "should redirect passed redir if it exists", (done)->
  154. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  155. @req.body.redir = "/somewhere"
  156. @res.send = (opts)=>
  157. opts.redir.should.equal "/somewhere"
  158. done()
  159. @UserController.register @req, @res
  160. it "should allocate the referals", (done)->
  161. @req.session =
  162. referal_id : "23123"
  163. referal_source : "email"
  164. referal_medium : "bob"
  165. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  166. @req.body.redir = "/somewhere"
  167. @res.send = (opts)=>
  168. @ReferalAllocator.allocate.calledWith(@req.session.referal_id, @user._id, @req.session.referal_source, @req.session.referal_medium).should.equal true
  169. done()
  170. @UserController.register @req, @res
  171. describe "changePassword", ->
  172. it "should check the old password is the current one at the moment", (done)->
  173. @AuthenticationManager.authenticate.callsArgWith(2)
  174. @req.body =
  175. currentPassword: "oldpasshere"
  176. @res.send = =>
  177. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  178. @AuthenticationManager.setUserPassword.called.should.equal false
  179. done()
  180. @UserController.changePassword @req, @res
  181. it "it should not set the new password if they do not match", (done)->
  182. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  183. @req.body =
  184. newPassword1: "1"
  185. newPassword2: "2"
  186. @res.send = =>
  187. @AuthenticationManager.setUserPassword.called.should.equal false
  188. done()
  189. @UserController.changePassword @req, @res
  190. it "should set the new password if they do match", (done)->
  191. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  192. @AuthenticationManager.setUserPassword.callsArgWith(2)
  193. @req.body =
  194. newPassword1: "newpass"
  195. newPassword2: "newpass"
  196. @res.send = =>
  197. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  198. done()
  199. @UserController.changePassword @req, @res