UserActivateControllerTests.mjs 4.3 KB

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