UserControllerTests.coffee 7.8 KB

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