OneTimeTokenHandlerTests.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Security/OneTimeTokenHandler"
  7. expect = require("chai").expect
  8. Errors = require "../../../../app/js/Features/Errors/Errors"
  9. tk = require("timekeeper")
  10. describe "OneTimeTokenHandler", ->
  11. beforeEach ->
  12. tk.freeze Date.now() # freeze the time for these tests
  13. @stubbedToken = "mock-token"
  14. @callback = sinon.stub()
  15. @OneTimeTokenHandler = SandboxedModule.require modulePath, requires:
  16. "settings-sharelatex":@settings
  17. "logger-sharelatex": log:->
  18. "crypto": randomBytes: () => @stubbedToken
  19. "../Errors/Errors": Errors
  20. "../../infrastructure/mongojs": db: @db = tokens: {}
  21. afterEach ->
  22. tk.reset()
  23. describe "getNewToken", ->
  24. beforeEach ->
  25. @db.tokens.insert = sinon.stub().yields()
  26. describe 'normally', ->
  27. beforeEach ->
  28. @OneTimeTokenHandler.getNewToken 'password', 'mock-data-to-store', @callback
  29. it "should insert a generated token with a 1 hour expiry", ->
  30. @db.tokens.insert
  31. .calledWith({
  32. use: 'password'
  33. token: @stubbedToken,
  34. createdAt: new Date(),
  35. expiresAt: new Date(Date.now() + 60 * 60 * 1000)
  36. data: 'mock-data-to-store'
  37. })
  38. .should.equal true
  39. it 'should call the callback with the token', ->
  40. @callback.calledWith(null, @stubbedToken).should.equal true
  41. describe 'with an optional expiresIn parameter', ->
  42. beforeEach ->
  43. @OneTimeTokenHandler.getNewToken 'password', 'mock-data-to-store', { expiresIn: 42 }, @callback
  44. it "should insert a generated token with a custom expiry", ->
  45. @db.tokens.insert
  46. .calledWith({
  47. use: 'password'
  48. token: @stubbedToken,
  49. createdAt: new Date(),
  50. expiresAt: new Date(Date.now() + 42 * 1000)
  51. data: 'mock-data-to-store'
  52. })
  53. .should.equal true
  54. it 'should call the callback with the token', ->
  55. @callback.calledWith(null, @stubbedToken).should.equal true
  56. describe "getValueFromTokenAndExpire", ->
  57. describe 'successfully', ->
  58. beforeEach ->
  59. @db.tokens.findAndModify = sinon.stub().yields(null, { data: 'mock-data' })
  60. @OneTimeTokenHandler.getValueFromTokenAndExpire 'password', 'mock-token', @callback
  61. it 'should expire the token', ->
  62. @db.tokens.findAndModify
  63. .calledWith({
  64. query: {
  65. use: 'password'
  66. token: 'mock-token',
  67. expiresAt: { $gt: new Date() },
  68. usedAt: { $exists: false }
  69. },
  70. update: {
  71. $set: { usedAt: new Date() }
  72. }
  73. })
  74. .should.equal true
  75. it 'should return the data', ->
  76. @callback.calledWith(null, 'mock-data').should.equal true
  77. describe 'when a valid token is not found', ->
  78. beforeEach ->
  79. @db.tokens.findAndModify = sinon.stub().yields(null, null)
  80. @OneTimeTokenHandler.getValueFromTokenAndExpire 'password', 'mock-token', @callback
  81. it 'should return a NotFoundError', ->
  82. @callback
  83. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  84. .should.equal true