UserControllerTests.coffee 7.9 KB

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