OneTimeTokenHandlerTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const path = require('path')
  16. const sinon = require('sinon')
  17. const modulePath = path.join(
  18. __dirname,
  19. '../../../../app/src/Features/Security/OneTimeTokenHandler'
  20. )
  21. const { expect } = require('chai')
  22. const Errors = require('../../../../app/src/Features/Errors/Errors')
  23. const tk = require('timekeeper')
  24. describe('OneTimeTokenHandler', function () {
  25. beforeEach(function () {
  26. tk.freeze(Date.now()) // freeze the time for these tests
  27. this.stubbedToken = 'mock-token'
  28. this.callback = sinon.stub()
  29. return (this.OneTimeTokenHandler = SandboxedModule.require(modulePath, {
  30. requires: {
  31. '@overleaf/settings': this.settings,
  32. crypto: {
  33. randomBytes: () => this.stubbedToken,
  34. },
  35. '../../infrastructure/mongodb': {
  36. db: (this.db = { tokens: {} }),
  37. },
  38. },
  39. }))
  40. })
  41. afterEach(function () {
  42. return tk.reset()
  43. })
  44. describe('getNewToken', function () {
  45. beforeEach(function () {
  46. return (this.db.tokens.insertOne = sinon.stub().yields())
  47. })
  48. describe('normally', function () {
  49. beforeEach(function () {
  50. return this.OneTimeTokenHandler.getNewToken(
  51. 'password',
  52. 'mock-data-to-store',
  53. this.callback
  54. )
  55. })
  56. it('should insert a generated token with a 1 hour expiry', function () {
  57. return this.db.tokens.insertOne
  58. .calledWith({
  59. use: 'password',
  60. token: this.stubbedToken,
  61. createdAt: new Date(),
  62. expiresAt: new Date(Date.now() + 60 * 60 * 1000),
  63. data: 'mock-data-to-store',
  64. })
  65. .should.equal(true)
  66. })
  67. it('should call the callback with the token', function () {
  68. return this.callback
  69. .calledWith(null, this.stubbedToken)
  70. .should.equal(true)
  71. })
  72. })
  73. describe('with an optional expiresIn parameter', function () {
  74. beforeEach(function () {
  75. return this.OneTimeTokenHandler.getNewToken(
  76. 'password',
  77. 'mock-data-to-store',
  78. { expiresIn: 42 },
  79. this.callback
  80. )
  81. })
  82. it('should insert a generated token with a custom expiry', function () {
  83. return this.db.tokens.insertOne
  84. .calledWith({
  85. use: 'password',
  86. token: this.stubbedToken,
  87. createdAt: new Date(),
  88. expiresAt: new Date(Date.now() + 42 * 1000),
  89. data: 'mock-data-to-store',
  90. })
  91. .should.equal(true)
  92. })
  93. it('should call the callback with the token', function () {
  94. return this.callback
  95. .calledWith(null, this.stubbedToken)
  96. .should.equal(true)
  97. })
  98. })
  99. })
  100. describe('getValueFromTokenAndExpire', function () {
  101. describe('successfully', function () {
  102. beforeEach(function () {
  103. this.db.tokens.findOneAndUpdate = sinon
  104. .stub()
  105. .yields(null, { value: { data: 'mock-data' } })
  106. return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
  107. 'password',
  108. 'mock-token',
  109. this.callback
  110. )
  111. })
  112. it('should expire the token', function () {
  113. return this.db.tokens.findOneAndUpdate
  114. .calledWith(
  115. {
  116. use: 'password',
  117. token: 'mock-token',
  118. expiresAt: { $gt: new Date() },
  119. usedAt: { $exists: false },
  120. },
  121. {
  122. $set: { usedAt: new Date() },
  123. }
  124. )
  125. .should.equal(true)
  126. })
  127. it('should return the data', function () {
  128. return this.callback.calledWith(null, 'mock-data').should.equal(true)
  129. })
  130. })
  131. describe('when a valid token is not found', function () {
  132. beforeEach(function () {
  133. this.db.tokens.findOneAndUpdate = sinon
  134. .stub()
  135. .yields(null, { value: null })
  136. return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
  137. 'password',
  138. 'mock-token',
  139. this.callback
  140. )
  141. })
  142. it('should return a NotFoundError', function () {
  143. return this.callback
  144. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  145. .should.equal(true)
  146. })
  147. })
  148. })
  149. })