UserActivateControllerTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.UserRegistrationHandler = {}
  18. this.ErrorController = { notFound: sinon.stub() }
  19. this.UserActivateController = SandboxedModule.require(MODULE_PATH, {
  20. requires: {
  21. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  22. '../../../../app/src/Features/User/UserRegistrationHandler':
  23. this.UserRegistrationHandler,
  24. '../../../../app/src/Features/Errors/ErrorController':
  25. this.ErrorController,
  26. },
  27. })
  28. this.req = {
  29. body: {},
  30. query: {},
  31. session: {
  32. user: this.user,
  33. },
  34. }
  35. this.res = {
  36. json: sinon.stub(),
  37. }
  38. })
  39. describe('activateAccountPage', function () {
  40. beforeEach(function () {
  41. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  42. this.req.query.user_id = this.user_id
  43. this.req.query.token = this.token = 'mock-token-123'
  44. })
  45. it('should 404 without a user_id', function (done) {
  46. delete this.req.query.user_id
  47. this.ErrorController.notFound = () => done()
  48. this.UserActivateController.activateAccountPage(this.req, this.res)
  49. })
  50. it('should 404 without a token', function (done) {
  51. delete this.req.query.token
  52. this.ErrorController.notFound = () => done()
  53. this.UserActivateController.activateAccountPage(this.req, this.res)
  54. })
  55. it('should 404 without a valid user_id', function (done) {
  56. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  57. this.ErrorController.notFound = () => done()
  58. this.UserActivateController.activateAccountPage(this.req, this.res)
  59. })
  60. it('should 403 for complex user_id', function (done) {
  61. this.ErrorController.forbidden = () => done()
  62. this.req.query.user_id = { first_name: 'X' }
  63. this.UserActivateController.activateAccountPage(this.req, this.res)
  64. })
  65. it('should redirect activated users to login', function (done) {
  66. this.user.loginCount = 1
  67. this.res.redirect = url => {
  68. this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
  69. url.should.equal('/login')
  70. return done()
  71. }
  72. this.UserActivateController.activateAccountPage(this.req, this.res)
  73. })
  74. it('render the activation page if the user has not logged in before', function (done) {
  75. this.user.loginCount = 0
  76. this.res.render = (page, opts) => {
  77. page.should.equal(VIEW_PATH)
  78. opts.email.should.equal(this.user.email)
  79. opts.token.should.equal(this.token)
  80. return done()
  81. }
  82. this.UserActivateController.activateAccountPage(this.req, this.res)
  83. })
  84. })
  85. describe('register', function () {
  86. beforeEach(function () {
  87. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon
  88. .stub()
  89. .callsArgWith(1, null, this.user, (this.url = 'mock/url'))
  90. this.req.body.email = this.user.email = this.email = 'email@example.com'
  91. this.UserActivateController.register(this.req, this.res)
  92. })
  93. it('should register the user and send them an email', function () {
  94. sinon.assert.calledWith(
  95. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail,
  96. this.email
  97. )
  98. })
  99. it('should return the user and activation url', function () {
  100. this.res.json
  101. .calledWith({
  102. email: this.email,
  103. setNewPasswordUrl: this.url,
  104. })
  105. .should.equal(true)
  106. })
  107. })
  108. })