UserControllerTests.coffee 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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":
  58. log:->
  59. err:->
  60. "../../infrastructure/Metrics": inc:->
  61. @req =
  62. session:
  63. destroy:->
  64. user :
  65. _id : @user_id
  66. email:"old@something.com"
  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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (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.sendStatus = (code)=>
  138. code.should.equal 200
  139. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  140. done()
  141. @UserController.updateUserSettings @req, @res
  142. it "should update the email on the session", (done)->
  143. @req.body.email = @newEmail.toUpperCase()
  144. @UserUpdater.changeEmailAddress.callsArgWith(2)
  145. callcount = 0
  146. @User.findById = (id, cb)=>
  147. if ++callcount == 2
  148. @user.email = @newEmail
  149. cb(null, @user)
  150. @res.sendStatus = (code)=>
  151. code.should.equal 200
  152. @req.session.user.email.should.equal @newEmail
  153. done()
  154. @UserController.updateUserSettings @req, @res
  155. it "should call populateGroupLicenceInvite", (done)->
  156. @req.body.email = @newEmail.toUpperCase()
  157. @UserUpdater.changeEmailAddress.callsArgWith(2)
  158. @res.sendStatus = (code)=>
  159. code.should.equal 200
  160. @UserHandler.populateGroupLicenceInvite.calledWith(@user).should.equal true
  161. done()
  162. @UserController.updateUserSettings @req, @res
  163. describe "logout", ->
  164. it "should destroy the session", (done)->
  165. @req.session.destroy = sinon.stub().callsArgWith(0)
  166. @res.redirect = (url)=>
  167. url.should.equal "/login"
  168. @req.session.destroy.called.should.equal true
  169. done()
  170. @UserController.logout @req, @res
  171. describe "register", ->
  172. beforeEach ->
  173. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  174. @req.body.email = @user.email = @email = "email@example.com"
  175. @UserController.register @req, @res
  176. it "should register the user and send them an email", ->
  177. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  178. .calledWith(@email)
  179. .should.equal true
  180. it "should return the user and activation url", ->
  181. @res.json
  182. .calledWith({
  183. email: @email,
  184. setNewPasswordUrl: @url
  185. })
  186. .should.equal true
  187. describe "changePassword", ->
  188. it "should check the old password is the current one at the moment", (done)->
  189. @AuthenticationManager.authenticate.callsArgWith(2)
  190. @req.body =
  191. currentPassword: "oldpasshere"
  192. @res.send = =>
  193. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  194. @AuthenticationManager.setUserPassword.called.should.equal false
  195. done()
  196. @UserController.changePassword @req, @res
  197. it "it should not set the new password if they do not match", (done)->
  198. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  199. @req.body =
  200. newPassword1: "1"
  201. newPassword2: "2"
  202. @res.send = =>
  203. @AuthenticationManager.setUserPassword.called.should.equal false
  204. done()
  205. @UserController.changePassword @req, @res
  206. it "should set the new password if they do match", (done)->
  207. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  208. @AuthenticationManager.setUserPassword.callsArgWith(2)
  209. @req.body =
  210. newPassword1: "newpass"
  211. newPassword2: "newpass"
  212. @res.send = =>
  213. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  214. done()
  215. @UserController.changePassword @req, @res