UserActivateControllerTests.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const Path = require('path')
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const MODULE_PATH = Path.join(
  5. __dirname,
  6. '../../../app/src/UserActivateController.js'
  7. )
  8. const VIEW_PATH = Path.join(__dirname, '../../../app/views/user/activate')
  9. describe('UserActivateController', function() {
  10. beforeEach(function() {
  11. this.user = {
  12. _id: (this.user_id = 'kwjewkl'),
  13. features: {},
  14. email: 'joe@example.com'
  15. }
  16. this.UserGetter = { getUser: sinon.stub() }
  17. this.ErrorController = { notFound: sinon.stub() }
  18. this.UserActivateController = SandboxedModule.require(MODULE_PATH, {
  19. globals: {
  20. console: console
  21. },
  22. requires: {
  23. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  24. '../../../../app/src/Features/Errors/ErrorController': this
  25. .ErrorController
  26. }
  27. })
  28. this.req = {
  29. query: {},
  30. session: {
  31. user: this.user
  32. }
  33. }
  34. this.res = {}
  35. })
  36. describe('activateAccountPage', function() {
  37. beforeEach(function() {
  38. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  39. this.req.query.user_id = this.user_id
  40. this.req.query.token = this.token = 'mock-token-123'
  41. })
  42. it('should 404 without a user_id', function(done) {
  43. delete this.req.query.user_id
  44. this.ErrorController.notFound = () => done()
  45. this.UserActivateController.activateAccountPage(this.req, this.res)
  46. })
  47. it('should 404 without a token', function(done) {
  48. delete this.req.query.token
  49. this.ErrorController.notFound = () => done()
  50. this.UserActivateController.activateAccountPage(this.req, this.res)
  51. })
  52. it('should 404 without a valid user_id', function(done) {
  53. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  54. this.ErrorController.notFound = () => done()
  55. this.UserActivateController.activateAccountPage(this.req, this.res)
  56. })
  57. it('should 403 for complex user_id', function(done) {
  58. this.ErrorController.forbidden = () => done()
  59. this.req.query.user_id = { first_name: 'X' }
  60. this.UserActivateController.activateAccountPage(this.req, this.res)
  61. })
  62. it('should redirect activated users to login', function(done) {
  63. this.user.loginCount = 1
  64. this.res.redirect = url => {
  65. this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
  66. url.should.equal('/login')
  67. return done()
  68. }
  69. this.UserActivateController.activateAccountPage(this.req, this.res)
  70. })
  71. it('render the activation page if the user has not logged in before', function(done) {
  72. this.user.loginCount = 0
  73. this.res.render = (page, opts) => {
  74. page.should.equal(VIEW_PATH)
  75. opts.email.should.equal(this.user.email)
  76. opts.token.should.equal(this.token)
  77. return done()
  78. }
  79. this.UserActivateController.activateAccountPage(this.req, this.res)
  80. })
  81. })
  82. })