UserControllerTests.coffee 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. @UserController = SandboxedModule.require modulePath, requires:
  36. "./UserLocator": @UserLocator
  37. "./UserDeleter": @UserDeleter
  38. "../../models/User": User:@User
  39. '../Newsletter/NewsletterManager':@NewsLetterManager
  40. "./UserRegistrationHandler":@UserRegistrationHandler
  41. "../Authentication/AuthenticationController": @AuthenticationController
  42. "../Authentication/AuthenticationManager": @AuthenticationManager
  43. "../Referal/ReferalAllocator":@ReferalAllocator
  44. "logger-sharelatex": {log:->}
  45. @req =
  46. session:
  47. destroy:->
  48. user :
  49. _id : @user_id
  50. body:{}
  51. @res = {}
  52. @next = sinon.stub()
  53. describe "deleteUser", ->
  54. it "should delete the user", (done)->
  55. @res.send = (code)=>
  56. @UserDeleter.deleteUser.calledWith(@user_id)
  57. code.should.equal 200
  58. done()
  59. @UserController.deleteUser @req, @res
  60. describe "unsubscribe", ->
  61. it "should send the user to unsubscribe", (done)->
  62. @res.send = (code)=>
  63. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  64. done()
  65. @UserController.unsubscribe @req, @res
  66. describe "updateUserSettings", ->
  67. it "should call save", (done)->
  68. @req.body = {}
  69. @res.send = (code)=>
  70. @user.save.called.should.equal true
  71. done()
  72. @UserController.updateUserSettings @req, @res
  73. it "should set the first name", (done)->
  74. @req.body =
  75. first_name: "bobby "
  76. @res.send = (code)=>
  77. @user.first_name.should.equal "bobby"
  78. done()
  79. @UserController.updateUserSettings @req, @res
  80. it "should set some props on ace", (done)->
  81. @req.body =
  82. theme: "something "
  83. @res.send = (code)=>
  84. @user.ace.theme.should.equal "something"
  85. done()
  86. @UserController.updateUserSettings @req, @res
  87. describe "logout", ->
  88. it "should destroy the session", (done)->
  89. @req.session.destroy = sinon.stub().callsArgWith(0)
  90. @res.redirect = (url)=>
  91. url.should.equal "/login"
  92. @req.session.destroy.called.should.equal true
  93. done()
  94. @UserController.logout @req, @res
  95. describe "register", ->
  96. it "should ask the UserRegistrationHandler to register user", (done)->
  97. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  98. @res.send = =>
  99. @UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
  100. done()
  101. @UserController.register @req, @res
  102. it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
  103. @UserRegistrationHandler.registerNewUser.callsArgWith(1, "EmailAlreadyRegisterd")
  104. @AuthenticationController.login = (req, res)=>
  105. assert.deepEqual req, @req
  106. assert.deepEqual res, @res
  107. done()
  108. @UserController.register @req, @res
  109. it "should put the user on the session and mark them as justRegistered", (done)->
  110. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  111. @res.send = =>
  112. assert.deepEqual @user, @req.session.user
  113. assert.equal @req.session.justRegistered, true
  114. done()
  115. @UserController.register @req, @res
  116. it "should redirect to project page", (done)->
  117. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  118. @res.send = (opts)=>
  119. opts.redir.should.equal "/project"
  120. done()
  121. @UserController.register @req, @res
  122. it "should redirect passed redir if it exists", (done)->
  123. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  124. @req.body.redir = "/somewhere"
  125. @res.send = (opts)=>
  126. opts.redir.should.equal "/somewhere"
  127. done()
  128. @UserController.register @req, @res
  129. it "should allocate the referals", (done)->
  130. @req.session =
  131. referal_id : "23123"
  132. referal_source : "email"
  133. referal_medium : "bob"
  134. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  135. @req.body.redir = "/somewhere"
  136. @res.send = (opts)=>
  137. @ReferalAllocator.allocate.calledWith(@req.session.referal_id, @user._id, @req.session.referal_source, @req.session.referal_medium).should.equal true
  138. done()
  139. @UserController.register @req, @res
  140. describe "changePassword", ->
  141. it "should check the old password is the current one at the moment", (done)->
  142. @AuthenticationManager.authenticate.callsArgWith(2)
  143. @req.body =
  144. currentPassword: "oldpasshere"
  145. @res.send = =>
  146. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  147. @AuthenticationManager.setUserPassword.called.should.equal false
  148. done()
  149. @UserController.changePassword @req, @res
  150. it "it should not set the new password if they do not match", (done)->
  151. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  152. @req.body =
  153. newPassword1: "1"
  154. newPassword2: "2"
  155. @res.send = =>
  156. @AuthenticationManager.setUserPassword.called.should.equal false
  157. done()
  158. @UserController.changePassword @req, @res
  159. it "should set the new password if they do match", (done)->
  160. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  161. @AuthenticationManager.setUserPassword.callsArgWith(2)
  162. @req.body =
  163. newPassword1: "newpass"
  164. newPassword2: "newpass"
  165. @res.send = =>
  166. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  167. done()
  168. @UserController.changePassword @req, @res