UserControllerTests.coffee 7.4 KB

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