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