UserActivateControllerTests.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. requires: {
  20. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  21. '../../../../app/src/Features/Errors/ErrorController': this
  22. .ErrorController
  23. }
  24. })
  25. this.req = {
  26. query: {},
  27. session: {
  28. user: this.user
  29. }
  30. }
  31. this.res = {}
  32. })
  33. describe('activateAccountPage', function() {
  34. beforeEach(function() {
  35. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  36. this.req.query.user_id = this.user_id
  37. this.req.query.token = this.token = 'mock-token-123'
  38. })
  39. it('should 404 without a user_id', function(done) {
  40. delete this.req.query.user_id
  41. this.ErrorController.notFound = () => done()
  42. this.UserActivateController.activateAccountPage(this.req, this.res)
  43. })
  44. it('should 404 without a token', function(done) {
  45. delete this.req.query.token
  46. this.ErrorController.notFound = () => done()
  47. this.UserActivateController.activateAccountPage(this.req, this.res)
  48. })
  49. it('should 404 without a valid user_id', function(done) {
  50. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  51. this.ErrorController.notFound = () => done()
  52. this.UserActivateController.activateAccountPage(this.req, this.res)
  53. })
  54. it('should 403 for complex user_id', function(done) {
  55. this.ErrorController.forbidden = () => done()
  56. this.req.query.user_id = { first_name: 'X' }
  57. this.UserActivateController.activateAccountPage(this.req, this.res)
  58. })
  59. it('should redirect activated users to login', function(done) {
  60. this.user.loginCount = 1
  61. this.res.redirect = url => {
  62. this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
  63. url.should.equal('/login')
  64. return done()
  65. }
  66. this.UserActivateController.activateAccountPage(this.req, this.res)
  67. })
  68. it('render the activation page if the user has not logged in before', function(done) {
  69. this.user.loginCount = 0
  70. this.res.render = (page, opts) => {
  71. page.should.equal(VIEW_PATH)
  72. opts.email.should.equal(this.user.email)
  73. opts.token.should.equal(this.token)
  74. return done()
  75. }
  76. this.UserActivateController.activateAccountPage(this.req, this.res)
  77. })
  78. })
  79. })