UserControllerTests.coffee 7.6 KB

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