UserControllerTests.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. @UserDeleter =
  20. deleteUser: sinon.stub().callsArgWith(1)
  21. @UserLocator =
  22. findById: sinon.stub().callsArgWith(1, null, @user)
  23. @User =
  24. findById: sinon.stub().callsArgWith(1, null, @user)
  25. @NewsLetterManager =
  26. unsubscribe: sinon.stub().callsArgWith(1)
  27. @UserRegistrationHandler =
  28. registerNewUser: sinon.stub()
  29. @AuthenticationController = {}
  30. @AuthenticationManager =
  31. authenticate: sinon.stub()
  32. setUserPassword: sinon.stub()
  33. @UserController = SandboxedModule.require modulePath, requires:
  34. "./UserLocator": @UserLocator
  35. "./UserDeleter": @UserDeleter
  36. "../../models/User": User:@User
  37. '../Newsletter/NewsletterManager':@NewsLetterManager
  38. "./UserRegistrationHandler":@UserRegistrationHandler
  39. "../Authentication/AuthenticationController": @AuthenticationController
  40. "../Authentication/AuthenticationManager": @AuthenticationManager
  41. "logger-sharelatex": {log:->}
  42. @req =
  43. session:
  44. destroy:->
  45. user :
  46. _id : @user_id
  47. body:{}
  48. @res = {}
  49. @next = sinon.stub()
  50. describe "deleteUser", ->
  51. it "should delete the user", (done)->
  52. @res.send = (code)=>
  53. @UserDeleter.deleteUser.calledWith(@user_id)
  54. code.should.equal 200
  55. done()
  56. @UserController.deleteUser @req, @res
  57. describe "unsubscribe", ->
  58. it "should send the user to unsubscribe", (done)->
  59. @res.send = (code)=>
  60. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  61. done()
  62. @UserController.unsubscribe @req, @res
  63. describe "updateUserSettings", ->
  64. it "should call save", (done)->
  65. @req.body = {}
  66. @res.send = (code)=>
  67. @user.save.called.should.equal true
  68. done()
  69. @UserController.updateUserSettings @req, @res
  70. it "should set the first name", (done)->
  71. @req.body =
  72. first_name: "bobby "
  73. @res.send = (code)=>
  74. @user.first_name.should.equal "bobby"
  75. done()
  76. @UserController.updateUserSettings @req, @res
  77. it "should set some props on ace", (done)->
  78. @req.body =
  79. theme: "something "
  80. @res.send = (code)=>
  81. @user.ace.theme.should.equal "something"
  82. done()
  83. @UserController.updateUserSettings @req, @res
  84. describe "logout", ->
  85. it "should destroy the session", (done)->
  86. @req.session.destroy = sinon.stub().callsArgWith(0)
  87. @res.redirect = (url)=>
  88. url.should.equal "/login"
  89. @req.session.destroy.called.should.equal true
  90. done()
  91. @UserController.logout @req, @res
  92. describe "register", ->
  93. it "should ask the UserRegistrationHandler to register user", (done)->
  94. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  95. @res.send = =>
  96. @UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
  97. done()
  98. @UserController.register @req, @res
  99. it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
  100. @UserRegistrationHandler.registerNewUser.callsArgWith(1, "EmailAlreadyRegisterd")
  101. @AuthenticationController.login = (req, res)=>
  102. assert.deepEqual req, @req
  103. assert.deepEqual res, @res
  104. done()
  105. @UserController.register @req, @res
  106. it "should put the user on the session and mark them as justRegistered", (done)->
  107. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  108. @res.send = =>
  109. assert.deepEqual @user, @req.session.user
  110. assert.equal @req.session.justRegistered, true
  111. done()
  112. @UserController.register @req, @res
  113. it "should redirect to project page", (done)->
  114. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  115. @res.send = (opts)=>
  116. opts.redir.should.equal "/project"
  117. done()
  118. @UserController.register @req, @res
  119. it "should redirect passed redir if it exists", (done)->
  120. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  121. @req.body.redir = "/somewhere"
  122. @res.send = (opts)=>
  123. opts.redir.should.equal "/somewhere"
  124. done()
  125. @UserController.register @req, @res
  126. describe "changePassword", ->
  127. it "should check the old password is the current one at the moment", (done)->
  128. @AuthenticationManager.authenticate.callsArgWith(2)
  129. @req.body =
  130. currentPassword: "oldpasshere"
  131. @res.send = =>
  132. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  133. @AuthenticationManager.setUserPassword.called.should.equal false
  134. done()
  135. @UserController.changePassword @req, @res
  136. it "it should not set the new password if they do not match", (done)->
  137. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  138. @req.body =
  139. newPassword1: "1"
  140. newPassword2: "2"
  141. @res.send = =>
  142. @AuthenticationManager.setUserPassword.called.should.equal false
  143. done()
  144. @UserController.changePassword @req, @res
  145. it "should set the new password if they do match", (done)->
  146. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  147. @AuthenticationManager.setUserPassword.callsArgWith(2)
  148. @req.body =
  149. newPassword1: "newpass"
  150. newPassword2: "newpass"
  151. @res.send = =>
  152. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  153. done()
  154. @UserController.changePassword @req, @res