OneTimeTokenHandlerTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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('peekValueFromToken', function () {
  101. describe('successfully', function () {
  102. const data = 'some-mock-data'
  103. beforeEach(function () {
  104. this.db.tokens.findOneAndUpdate = sinon
  105. .stub()
  106. .yields(null, { value: { data } })
  107. return this.OneTimeTokenHandler.peekValueFromToken(
  108. 'password',
  109. 'mock-token',
  110. this.callback
  111. )
  112. })
  113. it('should increment the peekCount', function () {
  114. return this.db.tokens.findOneAndUpdate
  115. .calledWith(
  116. {
  117. use: 'password',
  118. token: 'mock-token',
  119. expiresAt: { $gt: new Date() },
  120. usedAt: { $exists: false },
  121. peekCount: { $not: { $gte: this.OneTimeTokenHandler.MAX_PEEKS } },
  122. },
  123. {
  124. $inc: { peekCount: 1 },
  125. }
  126. )
  127. .should.equal(true)
  128. })
  129. it('should return the data', function () {
  130. return this.callback.calledWith(null, data).should.equal(true)
  131. })
  132. })
  133. describe('when a valid token is not found', function () {
  134. beforeEach(function () {
  135. this.db.tokens.findOneAndUpdate = sinon
  136. .stub()
  137. .yields(null, { value: null })
  138. return this.OneTimeTokenHandler.peekValueFromToken(
  139. 'password',
  140. 'mock-token',
  141. this.callback
  142. )
  143. })
  144. it('should return a NotFoundError', function () {
  145. return this.callback
  146. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  147. .should.equal(true)
  148. })
  149. })
  150. })
  151. describe('expireToken', function () {
  152. beforeEach(function () {
  153. this.db.tokens.updateOne = sinon.stub().yields(null)
  154. this.OneTimeTokenHandler.expireToken(
  155. 'password',
  156. 'mock-token',
  157. this.callback
  158. )
  159. })
  160. it('should expire the token', function () {
  161. this.db.tokens.updateOne
  162. .calledWith(
  163. {
  164. use: 'password',
  165. token: 'mock-token',
  166. },
  167. {
  168. $set: {
  169. usedAt: new Date(),
  170. },
  171. }
  172. )
  173. .should.equal(true)
  174. this.callback.calledWith(null).should.equal(true)
  175. })
  176. })
  177. describe('getValueFromTokenAndExpire', function () {
  178. describe('successfully', function () {
  179. beforeEach(function () {
  180. this.db.tokens.findOneAndUpdate = sinon
  181. .stub()
  182. .yields(null, { value: { data: 'mock-data' } })
  183. return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
  184. 'password',
  185. 'mock-token',
  186. this.callback
  187. )
  188. })
  189. it('should expire the token', function () {
  190. return this.db.tokens.findOneAndUpdate
  191. .calledWith(
  192. {
  193. use: 'password',
  194. token: 'mock-token',
  195. expiresAt: { $gt: new Date() },
  196. usedAt: { $exists: false },
  197. peekCount: { $not: { $gte: this.OneTimeTokenHandler.MAX_PEEKS } },
  198. },
  199. {
  200. $set: { usedAt: new Date() },
  201. }
  202. )
  203. .should.equal(true)
  204. })
  205. it('should return the data', function () {
  206. return this.callback.calledWith(null, 'mock-data').should.equal(true)
  207. })
  208. })
  209. describe('when a valid token is not found', function () {
  210. beforeEach(function () {
  211. this.db.tokens.findOneAndUpdate = sinon
  212. .stub()
  213. .yields(null, { value: null })
  214. return this.OneTimeTokenHandler.getValueFromTokenAndExpire(
  215. 'password',
  216. 'mock-token',
  217. this.callback
  218. )
  219. })
  220. it('should return a NotFoundError', function () {
  221. return this.callback
  222. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  223. .should.equal(true)
  224. })
  225. })
  226. })
  227. })