UserControllerTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. editorTheme: "something"
  195. @res.sendStatus = (code)=>
  196. @user.ace.theme.should.equal "something"
  197. done()
  198. @UserController.updateUserSettings @req, @res
  199. it "should set the overall theme", (done)->
  200. @req.body =
  201. overallTheme: "green-ish"
  202. @res.sendStatus = (code)=>
  203. @user.overallTheme.should.equal "green-ish"
  204. done()
  205. @UserController.updateUserSettings @req, @res
  206. it "should send an error if the email is 0 len", (done)->
  207. @req.body.email = ""
  208. @res.sendStatus = (code)->
  209. code.should.equal 400
  210. done()
  211. @UserController.updateUserSettings @req, @res
  212. it "should send an error if the email does not contain an @", (done)->
  213. @req.body.email = "bob at something dot com"
  214. @res.sendStatus = (code)->
  215. code.should.equal 400
  216. done()
  217. @UserController.updateUserSettings @req, @res
  218. it "should call the user updater with the new email and user _id", (done)->
  219. @req.body.email = @newEmail.toUpperCase()
  220. @UserUpdater.changeEmailAddress.callsArgWith(2)
  221. @res.sendStatus = (code)=>
  222. code.should.equal 200
  223. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  224. done()
  225. @UserController.updateUserSettings @req, @res
  226. it "should update the email on the session", (done)->
  227. @req.body.email = @newEmail.toUpperCase()
  228. @UserUpdater.changeEmailAddress.callsArgWith(2)
  229. callcount = 0
  230. @User.findById = (id, cb)=>
  231. if ++callcount == 2
  232. @user.email = @newEmail
  233. cb(null, @user)
  234. @res.sendStatus = (code)=>
  235. code.should.equal 200
  236. @AuthenticationController.setInSessionUser.calledWith(
  237. @req, {email: @newEmail, first_name: undefined, last_name: undefined}
  238. ).should.equal true
  239. done()
  240. @UserController.updateUserSettings @req, @res
  241. it "should call populateTeamInvites", (done)->
  242. @req.body.email = @newEmail.toUpperCase()
  243. @UserUpdater.changeEmailAddress.callsArgWith(2)
  244. @res.sendStatus = (code)=>
  245. code.should.equal 200
  246. @UserHandler.populateTeamInvites.calledWith(@user).should.equal true
  247. done()
  248. @UserController.updateUserSettings @req, @res
  249. describe 'when using an external auth source', ->
  250. beforeEach ->
  251. @UserUpdater.changeEmailAddress.callsArgWith(2)
  252. @newEmail = 'someone23@example.com'
  253. @req.externalAuthenticationSystemUsed = sinon.stub().returns(true)
  254. it 'should not set a new email', (done) ->
  255. @req.body.email = @newEmail
  256. @res.sendStatus = (code)=>
  257. code.should.equal 200
  258. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal false
  259. done()
  260. @UserController.updateUserSettings @req, @res
  261. describe "logout", ->
  262. it "should destroy the session", (done)->
  263. @req.session.destroy = sinon.stub().callsArgWith(0)
  264. @res.redirect = (url)=>
  265. url.should.equal "/login"
  266. @req.session.destroy.called.should.equal true
  267. done()
  268. @UserController.logout @req, @res
  269. it 'should clear sudo-mode', (done) ->
  270. @req.session.destroy = sinon.stub().callsArgWith(0)
  271. @SudoModeHandler.clearSudoMode = sinon.stub()
  272. @res.redirect = (url)=>
  273. url.should.equal "/login"
  274. @SudoModeHandler.clearSudoMode.callCount.should.equal 1
  275. @SudoModeHandler.clearSudoMode.calledWith(@user._id).should.equal true
  276. done()
  277. @UserController.logout @req, @res
  278. describe "register", ->
  279. beforeEach ->
  280. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  281. @req.body.email = @user.email = @email = "email@example.com"
  282. @UserController.register @req, @res
  283. it "should register the user and send them an email", ->
  284. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  285. .calledWith(@email)
  286. .should.equal true
  287. it "should return the user and activation url", ->
  288. @res.json
  289. .calledWith({
  290. email: @email,
  291. setNewPasswordUrl: @url
  292. })
  293. .should.equal true
  294. describe 'clearSessions', ->
  295. it 'should call revokeAllUserSessions', (done) ->
  296. @UserController.clearSessions @req, @res
  297. @UserSessionsManager.revokeAllUserSessions.callCount.should.equal 1
  298. done()
  299. it 'send a 201 response', (done) ->
  300. @res.sendStatus = (status) =>
  301. status.should.equal 201
  302. done()
  303. @UserController.clearSessions @req, @res
  304. describe 'when revokeAllUserSessions produces an error', ->
  305. it 'should call next with an error', (done) ->
  306. @UserSessionsManager.revokeAllUserSessions.callsArgWith(2, new Error('woops'))
  307. next = (err) =>
  308. expect(err).to.not.equal null
  309. expect(err).to.be.instanceof Error
  310. done()
  311. @UserController.clearSessions @req, @res, next
  312. describe "changePassword", ->
  313. it "should check the old password is the current one at the moment", (done)->
  314. @AuthenticationManager.authenticate.callsArgWith(2)
  315. @req.body =
  316. currentPassword: "oldpasshere"
  317. @res.send = =>
  318. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  319. @AuthenticationManager.setUserPassword.called.should.equal false
  320. done()
  321. @UserController.changePassword @req, @res
  322. it "it should not set the new password if they do not match", (done)->
  323. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  324. @req.body =
  325. newPassword1: "1"
  326. newPassword2: "2"
  327. @res.send = =>
  328. @AuthenticationManager.setUserPassword.called.should.equal false
  329. done()
  330. @UserController.changePassword @req, @res
  331. it "should set the new password if they do match", (done)->
  332. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  333. @AuthenticationManager.setUserPassword.callsArgWith(2)
  334. @req.body =
  335. newPassword1: "newpass"
  336. newPassword2: "newpass"
  337. @res.send = =>
  338. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  339. done()
  340. @UserController.changePassword @req, @res