UserControllerTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. Errors = require "../../../../app/js/Features/Errors/Errors"
  13. describe "UserController", ->
  14. beforeEach ->
  15. @user_id = "323123"
  16. @user =
  17. _id:@user_id
  18. save: sinon.stub().callsArgWith(0)
  19. ace:{}
  20. @req =
  21. user: {}
  22. session:
  23. destroy:->
  24. user :
  25. _id : @user_id
  26. email:"old@something.com"
  27. body:{}
  28. @UserDeleter =
  29. deleteUser: sinon.stub().callsArgWith(1)
  30. @UserGetter =
  31. getUser: sinon.stub().callsArgWith(1, null, @user)
  32. @User =
  33. findById: sinon.stub().callsArgWith(1, null, @user)
  34. @NewsLetterManager =
  35. unsubscribe: sinon.stub().callsArgWith(1)
  36. @UserRegistrationHandler =
  37. registerNewUser: sinon.stub()
  38. @AuthenticationController =
  39. establishUserSession: sinon.stub().callsArg(2)
  40. getLoggedInUserId: sinon.stub().returns(@user._id)
  41. getSessionUser: sinon.stub().returns(@req.session.user)
  42. setInSessionUser: sinon.stub()
  43. @AuthenticationManager =
  44. authenticate: sinon.stub()
  45. setUserPassword: sinon.stub()
  46. @ReferalAllocator =
  47. allocate:sinon.stub()
  48. @SubscriptionDomainHandler =
  49. autoAllocate:sinon.stub()
  50. @UserUpdater =
  51. changeEmailAddress:sinon.stub()
  52. @settings =
  53. siteUrl: "sharelatex.example.com"
  54. @UserHandler =
  55. populateTeamInvites: sinon.stub().callsArgWith(1)
  56. @UserSessionsManager =
  57. trackSession: sinon.stub()
  58. untrackSession: sinon.stub()
  59. revokeAllUserSessions: sinon.stub().callsArgWith(2, null)
  60. @SudoModeHandler =
  61. clearSudoMode: sinon.stub()
  62. @UserController = SandboxedModule.require modulePath, requires:
  63. "./UserGetter": @UserGetter
  64. "./UserDeleter": @UserDeleter
  65. "./UserUpdater":@UserUpdater
  66. "../../models/User": User:@User
  67. '../Newsletter/NewsletterManager':@NewsLetterManager
  68. "./UserRegistrationHandler":@UserRegistrationHandler
  69. "../Authentication/AuthenticationController": @AuthenticationController
  70. "../Authentication/AuthenticationManager": @AuthenticationManager
  71. "../Referal/ReferalAllocator":@ReferalAllocator
  72. "../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler
  73. "./UserHandler":@UserHandler
  74. "./UserSessionsManager": @UserSessionsManager
  75. "../SudoMode/SudoModeHandler": @SudoModeHandler
  76. "settings-sharelatex": @settings
  77. "logger-sharelatex":
  78. log:->
  79. err:->
  80. "metrics-sharelatex": inc:->
  81. "../Errors/Errors": Errors
  82. @res =
  83. send: sinon.stub()
  84. sendStatus: sinon.stub()
  85. json: sinon.stub()
  86. @next = sinon.stub()
  87. describe 'tryDeleteUser', ->
  88. beforeEach ->
  89. @req.body.password = 'wat'
  90. @req.logout = sinon.stub()
  91. @req.session.destroy = sinon.stub().callsArgWith(0, null)
  92. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@user._id)
  93. @AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, @user)
  94. @UserDeleter.deleteUser = sinon.stub().callsArgWith(1, null)
  95. it 'should send 200', (done) ->
  96. @res.sendStatus = (code) =>
  97. code.should.equal 200
  98. done()
  99. @UserController.tryDeleteUser @req, @res, @next
  100. it 'should try to authenticate user', (done) ->
  101. @res.sendStatus = (code) =>
  102. @AuthenticationManager.authenticate.callCount.should.equal 1
  103. @AuthenticationManager.authenticate.calledWith({_id: @user._id}, @req.body.password).should.equal true
  104. done()
  105. @UserController.tryDeleteUser @req, @res, @next
  106. it 'should delete the user', (done) ->
  107. @res.sendStatus = (code) =>
  108. @UserDeleter.deleteUser.callCount.should.equal 1
  109. @UserDeleter.deleteUser.calledWith(@user._id).should.equal true
  110. done()
  111. @UserController.tryDeleteUser @req, @res, @next
  112. describe 'when no password is supplied', ->
  113. beforeEach ->
  114. @req.body.password = ''
  115. it 'should return 403', (done) ->
  116. @res.sendStatus = (code) =>
  117. code.should.equal 403
  118. done()
  119. @UserController.tryDeleteUser @req, @res, @next
  120. describe 'when authenticate produces an error', ->
  121. beforeEach ->
  122. @AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, new Error('woops'))
  123. it 'should call next with an error', (done) ->
  124. @next = (err) =>
  125. expect(err).to.not.equal null
  126. expect(err).to.be.instanceof Error
  127. done()
  128. @UserController.tryDeleteUser @req, @res, @next
  129. describe 'when authenticate does not produce a user', ->
  130. beforeEach ->
  131. @AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, null)
  132. it 'should return 403', (done) ->
  133. @res.sendStatus = (code) =>
  134. code.should.equal 403
  135. done()
  136. @UserController.tryDeleteUser @req, @res, @next
  137. describe 'when deleteUser produces an error', ->
  138. beforeEach ->
  139. @UserDeleter.deleteUser = sinon.stub().callsArgWith(1, new Error('woops'))
  140. it 'should call next with an error', (done) ->
  141. @next = (err) =>
  142. expect(err).to.not.equal null
  143. expect(err).to.be.instanceof Error
  144. done()
  145. @UserController.tryDeleteUser @req, @res, @next
  146. describe 'when session.destroy produces an error', ->
  147. beforeEach ->
  148. @req.session.destroy = sinon.stub().callsArgWith(0, new Error('woops'))
  149. it 'should call next with an error', (done) ->
  150. @next = (err) =>
  151. expect(err).to.not.equal null
  152. expect(err).to.be.instanceof Error
  153. done()
  154. @UserController.tryDeleteUser @req, @res, @next
  155. describe "unsubscribe", ->
  156. it "should send the user to unsubscribe", (done)->
  157. @res.send = (code)=>
  158. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  159. done()
  160. @UserController.unsubscribe @req, @res
  161. describe "updateUserSettings", ->
  162. beforeEach ->
  163. @newEmail = "hello@world.com"
  164. @req.externalAuthenticationSystemUsed = sinon.stub().returns(false)
  165. it "should call save", (done)->
  166. @req.body = {}
  167. @res.sendStatus = (code)=>
  168. @user.save.called.should.equal true
  169. done()
  170. @UserController.updateUserSettings @req, @res
  171. it "should set the first name", (done)->
  172. @req.body =
  173. first_name: "bobby "
  174. @res.sendStatus = (code)=>
  175. @user.first_name.should.equal "bobby"
  176. done()
  177. @UserController.updateUserSettings @req, @res
  178. it "should set the role", (done)->
  179. @req.body =
  180. role: "student"
  181. @res.sendStatus = (code)=>
  182. @user.role.should.equal "student"
  183. done()
  184. @UserController.updateUserSettings @req, @res
  185. it "should set the institution", (done)->
  186. @req.body =
  187. institution: "MIT"
  188. @res.sendStatus = (code)=>
  189. @user.institution.should.equal "MIT"
  190. done()
  191. @UserController.updateUserSettings @req, @res
  192. it "should set some props on ace", (done)->
  193. @req.body =
  194. theme: "something"
  195. @res.sendStatus = (code)=>
  196. @user.ace.theme.should.equal "something"
  197. done()
  198. @UserController.updateUserSettings @req, @res
  199. it "should send an error if the email is 0 len", (done)->
  200. @req.body.email = ""
  201. @res.sendStatus = (code)->
  202. code.should.equal 400
  203. done()
  204. @UserController.updateUserSettings @req, @res
  205. it "should send an error if the email does not contain an @", (done)->
  206. @req.body.email = "bob at something dot com"
  207. @res.sendStatus = (code)->
  208. code.should.equal 400
  209. done()
  210. @UserController.updateUserSettings @req, @res
  211. it "should call the user updater with the new email and user _id", (done)->
  212. @req.body.email = @newEmail.toUpperCase()
  213. @UserUpdater.changeEmailAddress.callsArgWith(2)
  214. @res.sendStatus = (code)=>
  215. code.should.equal 200
  216. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  217. done()
  218. @UserController.updateUserSettings @req, @res
  219. it "should update the email on the session", (done)->
  220. @req.body.email = @newEmail.toUpperCase()
  221. @UserUpdater.changeEmailAddress.callsArgWith(2)
  222. callcount = 0
  223. @User.findById = (id, cb)=>
  224. if ++callcount == 2
  225. @user.email = @newEmail
  226. cb(null, @user)
  227. @res.sendStatus = (code)=>
  228. code.should.equal 200
  229. @AuthenticationController.setInSessionUser.calledWith(
  230. @req, {email: @newEmail, first_name: undefined, last_name: undefined}
  231. ).should.equal true
  232. done()
  233. @UserController.updateUserSettings @req, @res
  234. it "should call populateTeamInvites", (done)->
  235. @req.body.email = @newEmail.toUpperCase()
  236. @UserUpdater.changeEmailAddress.callsArgWith(2)
  237. @res.sendStatus = (code)=>
  238. code.should.equal 200
  239. @UserHandler.populateTeamInvites.calledWith(@user).should.equal true
  240. done()
  241. @UserController.updateUserSettings @req, @res
  242. describe 'when using an external auth source', ->
  243. beforeEach ->
  244. @UserUpdater.changeEmailAddress.callsArgWith(2)
  245. @newEmail = 'someone23@example.com'
  246. @req.externalAuthenticationSystemUsed = sinon.stub().returns(true)
  247. it 'should not set a new email', (done) ->
  248. @req.body.email = @newEmail
  249. @res.sendStatus = (code)=>
  250. code.should.equal 200
  251. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal false
  252. done()
  253. @UserController.updateUserSettings @req, @res
  254. describe "logout", ->
  255. it "should destroy the session", (done)->
  256. @req.session.destroy = sinon.stub().callsArgWith(0)
  257. @res.redirect = (url)=>
  258. url.should.equal "/login"
  259. @req.session.destroy.called.should.equal true
  260. done()
  261. @UserController.logout @req, @res
  262. it 'should clear sudo-mode', (done) ->
  263. @req.session.destroy = sinon.stub().callsArgWith(0)
  264. @SudoModeHandler.clearSudoMode = sinon.stub()
  265. @res.redirect = (url)=>
  266. url.should.equal "/login"
  267. @SudoModeHandler.clearSudoMode.callCount.should.equal 1
  268. @SudoModeHandler.clearSudoMode.calledWith(@user._id).should.equal true
  269. done()
  270. @UserController.logout @req, @res
  271. describe "register", ->
  272. beforeEach ->
  273. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  274. @req.body.email = @user.email = @email = "email@example.com"
  275. @UserController.register @req, @res
  276. it "should register the user and send them an email", ->
  277. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  278. .calledWith(@email)
  279. .should.equal true
  280. it "should return the user and activation url", ->
  281. @res.json
  282. .calledWith({
  283. email: @email,
  284. setNewPasswordUrl: @url
  285. })
  286. .should.equal true
  287. describe 'clearSessions', ->
  288. it 'should call revokeAllUserSessions', (done) ->
  289. @UserController.clearSessions @req, @res
  290. @UserSessionsManager.revokeAllUserSessions.callCount.should.equal 1
  291. done()
  292. it 'send a 201 response', (done) ->
  293. @res.sendStatus = (status) =>
  294. status.should.equal 201
  295. done()
  296. @UserController.clearSessions @req, @res
  297. describe 'when revokeAllUserSessions produces an error', ->
  298. it 'should call next with an error', (done) ->
  299. @UserSessionsManager.revokeAllUserSessions.callsArgWith(2, new Error('woops'))
  300. next = (err) =>
  301. expect(err).to.not.equal null
  302. expect(err).to.be.instanceof Error
  303. done()
  304. @UserController.clearSessions @req, @res, next
  305. describe "changePassword", ->
  306. it "should check the old password is the current one at the moment", (done)->
  307. @AuthenticationManager.authenticate.callsArgWith(2)
  308. @req.body =
  309. currentPassword: "oldpasshere"
  310. @res.send = =>
  311. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  312. @AuthenticationManager.setUserPassword.called.should.equal false
  313. done()
  314. @UserController.changePassword @req, @res
  315. it "it should not set the new password if they do not match", (done)->
  316. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  317. @req.body =
  318. newPassword1: "1"
  319. newPassword2: "2"
  320. @res.send = =>
  321. @AuthenticationManager.setUserPassword.called.should.equal false
  322. done()
  323. @UserController.changePassword @req, @res
  324. it "should set the new password if they do match", (done)->
  325. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  326. @AuthenticationManager.setUserPassword.callsArgWith(2)
  327. @req.body =
  328. newPassword1: "newpass"
  329. newPassword2: "newpass"
  330. @res.send = =>
  331. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  332. done()
  333. @UserController.changePassword @req, @res