UserControllerTests.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. establishUserSession: sinon.stub().callsArg(2)
  31. @AuthenticationManager =
  32. authenticate: sinon.stub()
  33. setUserPassword: sinon.stub()
  34. @ReferalAllocator =
  35. allocate:sinon.stub()
  36. @SubscriptionDomainHandler =
  37. autoAllocate:sinon.stub()
  38. @UserUpdater =
  39. changeEmailAddress:sinon.stub()
  40. @settings =
  41. siteUrl: "sharelatex.example.com"
  42. @UserController = SandboxedModule.require modulePath, requires:
  43. "./UserLocator": @UserLocator
  44. "./UserDeleter": @UserDeleter
  45. "./UserUpdater":@UserUpdater
  46. "../../models/User": User:@User
  47. '../Newsletter/NewsletterManager':@NewsLetterManager
  48. "./UserRegistrationHandler":@UserRegistrationHandler
  49. "../Authentication/AuthenticationController": @AuthenticationController
  50. "../Authentication/AuthenticationManager": @AuthenticationManager
  51. "../Referal/ReferalAllocator":@ReferalAllocator
  52. "../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler
  53. "settings-sharelatex": @settings
  54. "logger-sharelatex": {log:->}
  55. @req =
  56. session:
  57. destroy:->
  58. user :
  59. _id : @user_id
  60. body:{}
  61. @res =
  62. send: sinon.stub()
  63. json: sinon.stub()
  64. @next = sinon.stub()
  65. describe "deleteUser", ->
  66. it "should delete the user", (done)->
  67. @res.sendStatus = (code)=>
  68. @UserDeleter.deleteUser.calledWith(@user_id)
  69. code.should.equal 200
  70. done()
  71. @UserController.deleteUser @req, @res
  72. describe "unsubscribe", ->
  73. it "should send the user to unsubscribe", (done)->
  74. @res.send = (code)=>
  75. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  76. done()
  77. @UserController.unsubscribe @req, @res
  78. describe "updateUserSettings", ->
  79. beforeEach ->
  80. @newEmail = "hello@world.com"
  81. it "should call save", (done)->
  82. @req.body = {}
  83. @res.sendStatus = (code)=>
  84. @user.save.called.should.equal true
  85. done()
  86. @UserController.updateUserSettings @req, @res
  87. it "should set the first name", (done)->
  88. @req.body =
  89. first_name: "bobby "
  90. @res.sendStatus = (code)=>
  91. @user.first_name.should.equal "bobby"
  92. done()
  93. @UserController.updateUserSettings @req, @res
  94. it "should set the role", (done)->
  95. @req.body =
  96. role: "student"
  97. @res.sendStatus = (code)=>
  98. @user.role.should.equal "student"
  99. done()
  100. @UserController.updateUserSettings @req, @res
  101. it "should set the institution", (done)->
  102. @req.body =
  103. institution: "MIT"
  104. @res.sendStatus = (code)=>
  105. @user.institution.should.equal "MIT"
  106. done()
  107. @UserController.updateUserSettings @req, @res
  108. it "should set some props on ace", (done)->
  109. @req.body =
  110. theme: "something"
  111. @res.sendStatus = (code)=>
  112. @user.ace.theme.should.equal "something"
  113. done()
  114. @UserController.updateUserSettings @req, @res
  115. it "should send an error if the email is 0 len", (done)->
  116. @req.body.email = ""
  117. @res.sendStatus = (code)->
  118. code.should.equal 400
  119. done()
  120. @UserController.updateUserSettings @req, @res
  121. it "should send an error if the email does not contain an @", (done)->
  122. @req.body.email = "bob at something dot com"
  123. @res.sendStatus = (code)->
  124. code.should.equal 400
  125. done()
  126. @UserController.updateUserSettings @req, @res
  127. it "should call the user updater with the new email and user _id", (done)->
  128. @req.body.email = @newEmail.toUpperCase()
  129. @UserUpdater.changeEmailAddress.callsArgWith(2)
  130. @res.sendStatus = (code)=>
  131. code.should.equal 200
  132. @UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
  133. done()
  134. @UserController.updateUserSettings @req, @res
  135. describe "logout", ->
  136. it "should destroy the session", (done)->
  137. @req.session.destroy = sinon.stub().callsArgWith(0)
  138. @res.redirect = (url)=>
  139. url.should.equal "/login"
  140. @req.session.destroy.called.should.equal true
  141. done()
  142. @UserController.logout @req, @res
  143. describe "register", ->
  144. beforeEach ->
  145. @UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
  146. @req.body.email = @user.email = @email = "email@example.com"
  147. @UserController.register @req, @res
  148. it "should register the user and send them an email", ->
  149. @UserRegistrationHandler.registerNewUserAndSendActivationEmail
  150. .calledWith(@email)
  151. .should.equal true
  152. it "should return the user and activation url", ->
  153. console.log @res.json.args
  154. @res.json
  155. .calledWith({
  156. email: @email,
  157. setNewPasswordUrl: @url
  158. })
  159. .should.equal true
  160. describe "changePassword", ->
  161. it "should check the old password is the current one at the moment", (done)->
  162. @AuthenticationManager.authenticate.callsArgWith(2)
  163. @req.body =
  164. currentPassword: "oldpasshere"
  165. @res.send = =>
  166. @AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
  167. @AuthenticationManager.setUserPassword.called.should.equal false
  168. done()
  169. @UserController.changePassword @req, @res
  170. it "it should not set the new password if they do not match", (done)->
  171. @AuthenticationManager.authenticate.callsArgWith(2, null, {})
  172. @req.body =
  173. newPassword1: "1"
  174. newPassword2: "2"
  175. @res.send = =>
  176. @AuthenticationManager.setUserPassword.called.should.equal false
  177. done()
  178. @UserController.changePassword @req, @res
  179. it "should set the new password if they do match", (done)->
  180. @AuthenticationManager.authenticate.callsArgWith(2, null, @user)
  181. @AuthenticationManager.setUserPassword.callsArgWith(2)
  182. @req.body =
  183. newPassword1: "newpass"
  184. newPassword2: "newpass"
  185. @res.send = =>
  186. @AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
  187. done()
  188. @UserController.changePassword @req, @res