SessionStoreManagerTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/infrastructure/SessionStoreManager.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. describe('SessionStoreManager', function () {
  6. beforeEach(function () {
  7. this.SessionStoreManager = SandboxedModule.require(modulePath, {
  8. requires: {
  9. '@overleaf/metrics': (this.Metrics = { inc: sinon.stub() }),
  10. },
  11. })
  12. this.sessionStore = {
  13. generate: sinon.spy(req => {
  14. req.session = {}
  15. req.session.destroy = sinon.stub().yields()
  16. }),
  17. }
  18. })
  19. describe('enableValidationToken', function () {
  20. beforeEach(function () {
  21. this.originalGenerate = this.sessionStore.generate
  22. this.SessionStoreManager.enableValidationToken(this.sessionStore)
  23. })
  24. it('should set up a wrapper around the generate function', function () {
  25. expect(this.sessionStore.generate).to.not.equal(this.originalGenerate)
  26. })
  27. it('should add a validationToken when the generate function is called', function () {
  28. this.req = { sessionID: '123456789' }
  29. this.sessionStore.generate(this.req)
  30. expect(this.req.session.validationToken).to.equal('v1:6789')
  31. })
  32. it('should not allow the token to be overwritten', function () {
  33. this.req = { sessionID: '123456789' }
  34. this.sessionStore.generate(this.req)
  35. this.req.session.validationToken = 'try-to-overwrite-token'
  36. expect(this.req.session.validationToken).to.equal('v1:6789')
  37. })
  38. })
  39. describe('validationMiddleware', function () {
  40. this.beforeEach(function () {
  41. this.SessionStoreManager.enableValidationToken(this.sessionStore)
  42. this.req = { sessionID: '123456789' }
  43. this.next = sinon.stub()
  44. this.sessionStore.generate(this.req)
  45. })
  46. it('should accept the request when the session id matches the validation token', function () {
  47. this.SessionStoreManager.validationMiddleware(
  48. this.req,
  49. this.res,
  50. this.next
  51. )
  52. expect(this.next).to.be.calledWithExactly()
  53. })
  54. it('should destroy the session and return an error when the session id does not match the validation token', function () {
  55. this.req.sessionID = 'abcdefghijklmnopqrstuvwxyz'
  56. this.next = sinon.stub()
  57. this.SessionStoreManager.validationMiddleware(
  58. this.req,
  59. this.res,
  60. this.next
  61. )
  62. expect(this.req.session.destroy).to.be.called
  63. expect(this.next).to.be.calledWithExactly(
  64. sinon.match
  65. .instanceOf(Error)
  66. .and(sinon.match.has('message', 'invalid session'))
  67. )
  68. })
  69. it('should accept the request when the session does not have a validation token', function () {
  70. this.req = { sessionID: '123456789', session: {} }
  71. this.next = sinon.stub()
  72. this.SessionStoreManager.validationMiddleware(
  73. this.req,
  74. this.res,
  75. this.next
  76. )
  77. expect(this.next).to.be.calledWithExactly()
  78. })
  79. })
  80. describe('hasValidationToken', function () {
  81. this.beforeEach(function () {
  82. this.req = { sessionID: '123456789' }
  83. })
  84. it('should return true when the session is valid', function () {
  85. this.req.session = { validationToken: 'v1:6789' }
  86. const result = this.SessionStoreManager.hasValidationToken(this.req)
  87. expect(result).to.equal(true)
  88. })
  89. it('should return false when the session is valid', function () {
  90. this.req.session = { validationToken: 'v1:abcd' }
  91. const result = this.SessionStoreManager.hasValidationToken(this.req)
  92. expect(result).to.equal(true)
  93. })
  94. it('should return false when the validation token is missing', function () {
  95. this.req.session = {}
  96. const result = this.SessionStoreManager.hasValidationToken(this.req)
  97. expect(result).to.equal(false)
  98. })
  99. })
  100. })