UserControllerTests.coffee 6.9 KB

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