UserControllerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "deleteUser", ->
  83. it "should delete the user", (done)->
  84. @res.sendStatus = (code)=>
  85. @UserDeleter.deleteUser.calledWith(@user_id)
  86. code.should.equal 200
  87. done()
  88. @UserController.deleteUser @req, @res
  89. describe 'tryDeleteUser', ->
  90. beforeEach ->
  91. @req.body.password = 'wat'
  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 "unsubscribe", ->
  147. it "should send the user to unsubscribe", (done)->
  148. @res.send = (code)=>
  149. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  150. done()
  151. @UserController.unsubscribe @req, @res
  152. describe "updateUserSettings", ->
  153. beforeEach ->
  154. @newEmail = "hello@world.com"
  155. it "should call save", (done)->
  156. @req.body = {}
  157. @res.sendStatus = (code)=>
  158. @user.save.called.should.equal true
  159. done()
  160. @UserController.updateUserSettings @req, @res
  161. it "should set the first name", (done)->
  162. @req.body =
  163. first_name: "bobby "
  164. @res.sendStatus = (code)=>
  165. @user.first_name.should.equal "bobby"
  166. done()
  167. @UserController.updateUserSettings @req, @res
  168. it "should set the role", (done)->
  169. @req.body =
  170. role: "student"
  171. @res.sendStatus = (code)=>
  172. @user.role.should.equal "student"
  173. done()
  174. @UserController.updateUserSettings @req, @res
  175. it "should set the institution", (done)->
  176. @req.body =
  177. institution: "MIT"
  178. @res.sendStatus = (code)=>
  179. @user.institution.should.equal "MIT"
  180. done()
  181. @UserController.updateUserSettings @req, @res
  182. it "should set some props on ace", (done)->
  183. @req.body =
  184. theme: "something"
  185. @res.sendStatus = (code)=>
  186. @user.ace.theme.should.equal "something"
  187. done()
  188. @UserController.updateUserSettings @req, @res
  189. it "should send an error if the email is 0 len", (done)->
  190. @req.body.email = ""
  191. @res.sendStatus = (code)->
  192. code.should.equal 400
  193. done()
  194. @UserController.updateUserSettings @req, @res
  195. it "should send an error if the email does not contain an @", (done)->
  196. @req.body.email = "bob at something dot com"
  197. @res.sendStatus = (code)->
  198. code.should.equal 400
  199. done()
  200. @UserController.updateUserSettings @req, @res
  201. it "should call the user updater with the new email and user _id", (done)->
  202. @req.body.email = @newEmail.toUpperCase()
  203. @UserUpdater.changeEmailAddress.callsArgWith(2)
  204. @res.sendStatus = (code)=>
  205. code.should.equal 200
  206. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  207. done()
  208. @UserController.updateUserSettings @req, @res
  209. it "should update the email on the session", (done)->
  210. @req.body.email = @newEmail.toUpperCase()
  211. @UserUpdater.changeEmailAddress.callsArgWith(2)
  212. callcount = 0
  213. @User.findById = (id, cb)=>
  214. if ++callcount == 2
  215. @user.email = @newEmail
  216. cb(null, @user)
  217. @res.sendStatus = (code)=>
  218. code.should.equal 200
  219. @AuthenticationController.setInSessionUser.calledWith(
  220. @req, {email: @newEmail, first_name: undefined, last_name: undefined}
  221. ).should.equal true
  222. done()
  223. @UserController.updateUserSettings @req, @res
  224. it "should call populateGroupLicenceInvite", (done)->
  225. @req.body.email = @newEmail.toUpperCase()
  226. @UserUpdater.changeEmailAddress.callsArgWith(2)
  227. @res.sendStatus = (code)=>
  228. code.should.equal 200
  229. @UserHandler.populateGroupLicenceInvite.calledWith(@user).should.equal true
  230. done()
  231. @UserController.updateUserSettings @req, @res
  232. describe "logout", ->
  233. it "should destroy the session", (done)->
  234. @req.session.destroy = sinon.stub().callsArgWith(0)
  235. @res.redirect = (url)=>
  236. url.should.equal "/login"
  237. @req.session.destroy.called.should.equal true
  238. done()
  239. @UserController.logout @req, @res
  240. describe "register", ->
  241. beforeEach ->
  242. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  243. @req.body.email = @user.email = @email = "email@example.com"
  244. @UserController.register @req, @res
  245. it "should register the user and send them an email", ->
  246. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  247. .calledWith(@email)
  248. .should.equal true
  249. it "should return the user and activation url", ->
  250. @res.json
  251. .calledWith({
  252. email: @email,
  253. setNewPasswordUrl: @url
  254. })
  255. .should.equal true
  256. describe 'clearSessions', ->
  257. it 'should call revokeAllUserSessions', (done) ->
  258. @UserController.clearSessions @req, @res
  259. @UserSessionsManager.revokeAllUserSessions.callCount.should.equal 1
  260. done()
  261. it 'send a 201 response', (done) ->
  262. @res.sendStatus = (status) =>
  263. status.should.equal 201
  264. done()
  265. @UserController.clearSessions @req, @res
  266. describe 'when revokeAllUserSessions produces an error', ->
  267. it 'should call next with an error', (done) ->
  268. @UserSessionsManager.revokeAllUserSessions.callsArgWith(2, new Error('woops'))
  269. next = (err) =>
  270. expect(err).to.not.equal null
  271. expect(err).to.be.instanceof Error
  272. done()
  273. @UserController.clearSessions @req, @res, next
  274. describe "changePassword", ->
  275. it "should check the old password is the current one at the moment", (done)->
  276. @AuthenticationManager.authenticate.callsArgWith(2)
  277. @req.body =
  278. currentPassword: "oldpasshere"
  279. @res.send = =>
  280. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  281. @AuthenticationManager.setUserPassword.called.should.equal false
  282. done()
  283. @UserController.changePassword @req, @res
  284. it "it should not set the new password if they do not match", (done)->
  285. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  286. @req.body =
  287. newPassword1: "1"
  288. newPassword2: "2"
  289. @res.send = =>
  290. @AuthenticationManager.setUserPassword.called.should.equal false
  291. done()
  292. @UserController.changePassword @req, @res
  293. it "should set the new password if they do match", (done)->
  294. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  295. @AuthenticationManager.setUserPassword.callsArgWith(2)
  296. @req.body =
  297. newPassword1: "newpass"
  298. newPassword2: "newpass"
  299. @res.send = =>
  300. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  301. done()
  302. @UserController.changePassword @req, @res