UserControllerTests.coffee 13 KB

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