UserActivateControllerTests.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Path from 'node:path'
  2. import { fileURLToPath } from 'node:url'
  3. import { strict as esmock } from 'esmock'
  4. import sinon from 'sinon'
  5. const __dirname = Path.dirname(fileURLToPath(import.meta.url))
  6. const MODULE_PATH = '../../../app/src/UserActivateController.mjs'
  7. const VIEW_PATH = Path.join(__dirname, '../../../app/views/user/activate')
  8. describe('UserActivateController', function () {
  9. beforeEach(async function () {
  10. this.user = {
  11. _id: (this.user_id = 'kwjewkl'),
  12. features: {},
  13. email: 'joe@example.com',
  14. }
  15. this.UserGetter = { getUser: sinon.stub() }
  16. this.UserRegistrationHandler = {}
  17. this.ErrorController = { notFound: sinon.stub() }
  18. this.UserActivateController = await esmock(MODULE_PATH, {
  19. '../../../../../app/src/Features/User/UserGetter.js': this.UserGetter,
  20. '../../../../../app/src/Features/User/UserRegistrationHandler.js':
  21. this.UserRegistrationHandler,
  22. '../../../../../app/src/Features/Errors/ErrorController.js':
  23. this.ErrorController,
  24. })
  25. this.req = {
  26. body: {},
  27. query: {},
  28. session: {
  29. user: this.user,
  30. },
  31. }
  32. this.res = {
  33. json: sinon.stub(),
  34. }
  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. describe('register', function () {
  83. beforeEach(function () {
  84. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon
  85. .stub()
  86. .callsArgWith(1, null, this.user, (this.url = 'mock/url'))
  87. this.req.body.email = this.user.email = this.email = 'email@example.com'
  88. this.UserActivateController.register(this.req, this.res)
  89. })
  90. it('should register the user and send them an email', function () {
  91. sinon.assert.calledWith(
  92. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail,
  93. this.email
  94. )
  95. })
  96. it('should return the user and activation url', function () {
  97. this.res.json
  98. .calledWith({
  99. email: this.email,
  100. setNewPasswordUrl: this.url,
  101. })
  102. .should.equal(true)
  103. })
  104. })
  105. })