OneTimeTokenHandler.sequential.test.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import sinon from 'sinon'
  2. import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  4. import {
  5. connectionPromise,
  6. cleanupTestDatabase,
  7. } from '../../../../app/src/infrastructure/mongodb.mjs'
  8. import OneTimeTokenHandler from '../../../../app/src/Features/Security/OneTimeTokenHandler.mjs'
  9. vi.mock('../../../../app/src/Features/Errors/Errors.js', () =>
  10. vi.importActual('../../../../app/src/Features/Errors/Errors.js')
  11. )
  12. describe('OneTimeTokenHandler', function () {
  13. beforeAll(async function () {
  14. await connectionPromise
  15. })
  16. beforeEach(cleanupTestDatabase)
  17. beforeEach(function (ctx) {
  18. ctx.clock = sinon.useFakeTimers()
  19. })
  20. afterEach(function (ctx) {
  21. ctx.clock.restore()
  22. })
  23. describe('getNewToken', function () {
  24. it('generates a token and stores it in the database', async function () {
  25. const token = await OneTimeTokenHandler.promises.getNewToken(
  26. 'password',
  27. 'mock-data-to-store'
  28. )
  29. const { data } = await OneTimeTokenHandler.promises.peekValueFromToken(
  30. 'password',
  31. token
  32. )
  33. expect(data).to.equal('mock-data-to-store')
  34. })
  35. it('expires the generated token after 1 hour', async function (ctx) {
  36. const token = await OneTimeTokenHandler.promises.getNewToken(
  37. 'password',
  38. 'mock-data-to-store'
  39. )
  40. ctx.clock.tick('25:00:00')
  41. await expect(
  42. OneTimeTokenHandler.promises.peekValueFromToken('password', token)
  43. ).to.be.rejectedWith(Errors.NotFoundError)
  44. })
  45. it('accepts an expiresIn parameter', async function (ctx) {
  46. const token = await OneTimeTokenHandler.promises.getNewToken(
  47. 'password',
  48. 'mock-data-to-store',
  49. { expiresIn: 42 }
  50. )
  51. ctx.clock.tick('00:30')
  52. const { data } = await OneTimeTokenHandler.promises.peekValueFromToken(
  53. 'password',
  54. token
  55. )
  56. expect(data).to.equal('mock-data-to-store')
  57. ctx.clock.tick('00:15')
  58. await expect(
  59. OneTimeTokenHandler.promises.peekValueFromToken('password', token)
  60. ).to.be.rejectedWith(Errors.NotFoundError)
  61. })
  62. })
  63. describe('peekValueFromToken', function () {
  64. it('should return the data and peek count', async function () {
  65. const data = { email: 'some-mock-data' }
  66. const token = await OneTimeTokenHandler.promises.getNewToken(
  67. 'password',
  68. data
  69. )
  70. const result = await OneTimeTokenHandler.promises.peekValueFromToken(
  71. 'password',
  72. token
  73. )
  74. expect(result).to.deep.equal({
  75. data,
  76. remainingPeeks: OneTimeTokenHandler.MAX_PEEKS - 1,
  77. })
  78. })
  79. it('should throw a NotFoundError if the token is not found', async function () {
  80. await expect(
  81. OneTimeTokenHandler.promises.peekValueFromToken('password', 'bad-token')
  82. ).to.be.rejectedWith(Errors.NotFoundError)
  83. })
  84. it('should stop returning the data after the peek count is exceeded', async function () {
  85. const data = { email: 'some-mock-data' }
  86. const token = await OneTimeTokenHandler.promises.getNewToken(
  87. 'password',
  88. data
  89. )
  90. for (let peeks = 1; peeks <= OneTimeTokenHandler.MAX_PEEKS; peeks++) {
  91. const result = await OneTimeTokenHandler.promises.peekValueFromToken(
  92. 'password',
  93. token
  94. )
  95. expect(result).to.deep.equal({
  96. data,
  97. remainingPeeks: OneTimeTokenHandler.MAX_PEEKS - peeks,
  98. })
  99. }
  100. await expect(
  101. OneTimeTokenHandler.promises.peekValueFromToken('password', token)
  102. ).to.be.rejectedWith(Errors.NotFoundError)
  103. })
  104. })
  105. describe('expireToken', function () {
  106. it('should expire the token immediately', async function () {
  107. const token = await OneTimeTokenHandler.promises.getNewToken(
  108. 'password',
  109. 'mock-data'
  110. )
  111. await OneTimeTokenHandler.promises.expireToken('password', token)
  112. await expect(
  113. OneTimeTokenHandler.promises.peekValueFromToken('password', token)
  114. ).to.be.rejectedWith(Errors.NotFoundError)
  115. })
  116. })
  117. describe('getValueFromTokenAndExpire', function () {
  118. it('should return the value and expire the token', async function () {
  119. const token = await OneTimeTokenHandler.promises.getNewToken(
  120. 'password',
  121. 'mock-data'
  122. )
  123. const data =
  124. await OneTimeTokenHandler.promises.getValueFromTokenAndExpire(
  125. 'password',
  126. token
  127. )
  128. expect(data).to.equal('mock-data')
  129. await expect(
  130. OneTimeTokenHandler.promises.peekValueFromToken('password', token)
  131. ).to.be.rejectedWith(Errors.NotFoundError)
  132. })
  133. it('should throw a NotFoundError if the token is not found', async function () {
  134. await expect(
  135. OneTimeTokenHandler.promises.getValueFromTokenAndExpire(
  136. 'password',
  137. 'bad-token'
  138. )
  139. ).to.be.rejectedWith(Errors.NotFoundError)
  140. })
  141. })
  142. })