UserControllerTests.coffee 7.3 KB

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