SessionAutostartMiddlewareTests.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath =
  4. '../../../../app/src/infrastructure/SessionAutostartMiddleware.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. describe('SessionAutostartMiddleware', function () {
  7. let SessionAutostartMiddleware, middleware, Settings
  8. const cookieName = 'coookieee'
  9. const excludedRoute = '/wombat/potato'
  10. const excludedMethod = 'POST'
  11. const excludedCallback = () => 'call me'
  12. beforeEach(function () {
  13. Settings = {
  14. cookieName: cookieName,
  15. }
  16. SessionAutostartMiddleware = SandboxedModule.require(modulePath, {
  17. requires: {
  18. '@overleaf/settings': Settings,
  19. },
  20. })
  21. middleware = new SessionAutostartMiddleware()
  22. middleware.disableSessionAutostartForRoute(
  23. excludedRoute,
  24. excludedMethod,
  25. excludedCallback
  26. )
  27. })
  28. describe('middleware', function () {
  29. let req, next
  30. beforeEach(function () {
  31. req = {
  32. path: excludedRoute,
  33. method: excludedMethod,
  34. signedCookies: {},
  35. headers: {},
  36. }
  37. next = sinon.stub()
  38. })
  39. it('executes the callback for the excluded route', function () {
  40. middleware.middleware(req, {}, next)
  41. expect(req.session.noSessionCallback).to.equal(excludedCallback)
  42. })
  43. it('does not execute the callback if the method is not excluded', function () {
  44. req.method = 'GET'
  45. middleware.middleware(req, {}, next)
  46. expect(req.session).not.to.exist
  47. })
  48. it('does not execute the callback if the path is not excluded', function () {
  49. req.path = '/giraffe'
  50. middleware.middleware(req, {}, next)
  51. expect(req.session).not.to.exist
  52. })
  53. it('does not execute the callback if there is a cookie', function () {
  54. req.signedCookies[cookieName] = 'a very useful session cookie'
  55. middleware.middleware(req, {}, next)
  56. expect(req.session).not.to.exist
  57. })
  58. })
  59. describe('bot middlewear', function () {
  60. let req, next
  61. beforeEach(function () {
  62. req = {
  63. signedCookies: {},
  64. headers: {},
  65. }
  66. next = sinon.stub()
  67. })
  68. it('GoogleHC user agent should have an empty session', function () {
  69. req.headers['user-agent'] = 'GoogleHC'
  70. middleware.middleware(req, {}, next)
  71. expect(req.session.noSessionCallback).to.deep.exist
  72. })
  73. it('should not add empty session with a firefox useragent', function () {
  74. req.headers['user-agent'] = 'firefox'
  75. middleware.middleware(req, {}, next)
  76. expect(req.session).not.to.exist
  77. })
  78. it('should not add empty session with a empty useragent', function () {
  79. middleware.middleware(req, {}, next)
  80. expect(req.session).not.to.exist
  81. })
  82. })
  83. })