UserControllerTests.coffee 8.3 KB

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