UserControllerTests.coffee 7.7 KB

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