UserControllerTests.coffee 8.0 KB

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