UserControllerTests.coffee 11 KB

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