UserControllerTests.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 =
  15. _id:"!@£!23123"
  16. save:sinon.stub().callsArgWith(0)
  17. ace:{}
  18. @UserDeleter =
  19. deleteUser: sinon.stub().callsArgWith(1)
  20. @UserLocator =
  21. findById: sinon.stub().callsArgWith(1, null, @user)
  22. @User =
  23. findById: sinon.stub().callsArgWith(1, null, @user)
  24. @NewsLetterManager =
  25. unsubscribe: sinon.stub().callsArgWith(1)
  26. @UserRegistrationHandler =
  27. registerNewUser: sinon.stub()
  28. @AuthenticationController = {}
  29. @UserController = SandboxedModule.require modulePath, requires:
  30. "./UserLocator": @UserLocator
  31. "./UserDeleter": @UserDeleter
  32. "../../models/User": User:@User
  33. '../Newsletter/NewsletterManager':@NewsLetterManager
  34. "./UserRegistrationHandler":@UserRegistrationHandler
  35. "../Authentication/AuthenticationController": @AuthenticationController
  36. "logger-sharelatex": {log:->}
  37. @req =
  38. session: destroy:->
  39. body:{}
  40. @res = {}
  41. @next = sinon.stub()
  42. @user_id = "323123"
  43. @req.session.user =
  44. _id = @user_id
  45. describe "deleteUser", ->
  46. it "should delete the user", (done)->
  47. @res.send = (code)=>
  48. @UserDeleter.deleteUser.calledWith(@user_id)
  49. code.should.equal 200
  50. done()
  51. @UserController.deleteUser @req, @res
  52. describe "unsubscribe", ->
  53. it "should send the user to unsubscribe", (done)->
  54. @res.send = (code)=>
  55. @NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
  56. done()
  57. @UserController.unsubscribe @req, @res
  58. describe "updateUserSettings", ->
  59. it "should call save", (done)->
  60. @req.body = {}
  61. @res.send = (code)=>
  62. @user.save.called.should.equal true
  63. done()
  64. @UserController.updateUserSettings @req, @res
  65. it "should set the first name", (done)->
  66. @req.body =
  67. first_name: "bobby "
  68. @res.send = (code)=>
  69. @user.first_name.should.equal "bobby"
  70. done()
  71. @UserController.updateUserSettings @req, @res
  72. it "should set some props on ace", (done)->
  73. @req.body =
  74. theme: "something "
  75. @res.send = (code)=>
  76. @user.ace.theme.should.equal "something"
  77. done()
  78. @UserController.updateUserSettings @req, @res
  79. describe "logout", ->
  80. it "should destroy the session", (done)->
  81. @req.session.destroy = sinon.stub().callsArgWith(0)
  82. @res.redirect = (url)=>
  83. url.should.equal "/login"
  84. @req.session.destroy.called.should.equal true
  85. done()
  86. @UserController.logout @req, @res
  87. describe "register", ->
  88. it "should ask the UserRegistrationHandler to register user", (done)->
  89. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  90. @res.send = =>
  91. @UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
  92. done()
  93. @UserController.register @req, @res
  94. it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
  95. @UserRegistrationHandler.registerNewUser.callsArgWith(1, message:"EmailAlreadyRegisterd")
  96. @AuthenticationController.login = (req, res)=>
  97. assert.deepEqual req, @req
  98. assert.deepEqual res, @res
  99. done()
  100. @UserController.register @req, @res
  101. it "should put the user on the session and mark them as justRegistered", (done)->
  102. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  103. @res.send = =>
  104. assert.deepEqual @user, @req.session.user
  105. assert.equal @req.session.justRegistered, true
  106. done()
  107. @UserController.register @req, @res
  108. it "should redirect to project page", (done)->
  109. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  110. @res.send = (opts)=>
  111. opts.redir.should.equal "/project"
  112. done()
  113. @UserController.register @req, @res
  114. it "should redirect passed redir if it exists", (done)->
  115. @UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
  116. @req.body.redir = "/somewhere"
  117. @res.send = (opts)=>
  118. opts.redir.should.equal "/somewhere"
  119. done()
  120. @UserController.register @req, @res