SessionStoreManagerTests.js 4.0 KB

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