UserControllerTests.coffee 7.7 KB

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