SessionAutostartMiddleware.test.mjs 3.1 KB

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