UserControllerTests.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. @SubscriptionDomainHandler =
  37. autoAllocate:sinon.stub()
  38. @UserUpdater =
  39. changeEmailAddress:sinon.stub()
  40. @settings =
  41. siteUrl: "sharelatex.example.com"
  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/SubscriptionDomainHandler":@SubscriptionDomainHandler
  53. "settings-sharelatex": @settings
  54. "logger-sharelatex": {log:->}
  55. "../../infrastructure/Metrics": inc:->
  56. @req =
  57. session:
  58. destroy:->
  59. user :
  60. _id : @user_id
  61. body:{}
  62. @res =
  63. send: sinon.stub()
  64. json: sinon.stub()
  65. @next = sinon.stub()
  66. describe "deleteUser", ->
  67. it "should delete the user", (done)->
  68. @res.sendStatus = (code)=>
  69. @UserDeleter.deleteUser.calledWith(@user_id)
  70. code.should.equal 200
  71. done()
  72. @UserController.deleteUser @req, @res
  73. describe "unsubscribe", ->
  74. it "should send the user to unsubscribe", (done)->
  75. @res.send = (code)=>
  76. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  77. done()
  78. @UserController.unsubscribe @req, @res
  79. describe "updateUserSettings", ->
  80. beforeEach ->
  81. @newEmail = "hello@world.com"
  82. it "should call save", (done)->
  83. @req.body = {}
  84. @res.sendStatus = (code)=>
  85. @user.save.called.should.equal true
  86. done()
  87. @UserController.updateUserSettings @req, @res
  88. it "should set the first name", (done)->
  89. @req.body =
  90. first_name: "bobby "
  91. @res.sendStatus = (code)=>
  92. @user.first_name.should.equal "bobby"
  93. done()
  94. @UserController.updateUserSettings @req, @res
  95. it "should set the role", (done)->
  96. @req.body =
  97. role: "student"
  98. @res.sendStatus = (code)=>
  99. @user.role.should.equal "student"
  100. done()
  101. @UserController.updateUserSettings @req, @res
  102. it "should set the institution", (done)->
  103. @req.body =
  104. institution: "MIT"
  105. @res.sendStatus = (code)=>
  106. @user.institution.should.equal "MIT"
  107. done()
  108. @UserController.updateUserSettings @req, @res
  109. it "should set some props on ace", (done)->
  110. @req.body =
  111. theme: "something"
  112. @res.sendStatus = (code)=>
  113. @user.ace.theme.should.equal "something"
  114. done()
  115. @UserController.updateUserSettings @req, @res
  116. it "should send an error if the email is 0 len", (done)->
  117. @req.body.email = ""
  118. @res.sendStatus = (code)->
  119. code.should.equal 400
  120. done()
  121. @UserController.updateUserSettings @req, @res
  122. it "should send an error if the email does not contain an @", (done)->
  123. @req.body.email = "bob at something dot com"
  124. @res.sendStatus = (code)->
  125. code.should.equal 400
  126. done()
  127. @UserController.updateUserSettings @req, @res
  128. it "should call the user updater with the new email and user _id", (done)->
  129. @req.body.email = @newEmail.toUpperCase()
  130. @UserUpdater.changeEmailAddress.callsArgWith(2)
  131. @res.sendStatus = (code)=>
  132. code.should.equal 200
  133. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  134. done()
  135. @UserController.updateUserSettings @req, @res
  136. describe "logout", ->
  137. it "should destroy the session", (done)->
  138. @req.session.destroy = sinon.stub().callsArgWith(0)
  139. @res.redirect = (url)=>
  140. url.should.equal "/login"
  141. @req.session.destroy.called.should.equal true
  142. done()
  143. @UserController.logout @req, @res
  144. describe "register", ->
  145. beforeEach ->
  146. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  147. @req.body.email = @user.email = @email = "email@example.com"
  148. @UserController.register @req, @res
  149. it "should register the user and send them an email", ->
  150. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  151. .calledWith(@email)
  152. .should.equal true
  153. it "should return the user and activation url", ->
  154. @res.json
  155. .calledWith({
  156. email: @email,
  157. setNewPasswordUrl: @url
  158. })
  159. .should.equal true
  160. describe "changePassword", ->
  161. it "should check the old password is the current one at the moment", (done)->
  162. @AuthenticationManager.authenticate.callsArgWith(2)
  163. @req.body =
  164. currentPassword: "oldpasshere"
  165. @res.send = =>
  166. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  167. @AuthenticationManager.setUserPassword.called.should.equal false
  168. done()
  169. @UserController.changePassword @req, @res
  170. it "it should not set the new password if they do not match", (done)->
  171. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  172. @req.body =
  173. newPassword1: "1"
  174. newPassword2: "2"
  175. @res.send = =>
  176. @AuthenticationManager.setUserPassword.called.should.equal false
  177. done()
  178. @UserController.changePassword @req, @res
  179. it "should set the new password if they do match", (done)->
  180. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  181. @AuthenticationManager.setUserPassword.callsArgWith(2)
  182. @req.body =
  183. newPassword1: "newpass"
  184. newPassword2: "newpass"
  185. @res.send = =>
  186. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  187. done()
  188. @UserController.changePassword @req, @res