UserControllerTests.coffee 12 KB

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