SessionAutostartMiddlewareTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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,
  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 for the excluded route with ?autostartSession=true set', function () {
  44. req.query = { autostartSession: 'true' }
  45. middleware.middleware(req, {}, next)
  46. expect(req.session).not.to.exist
  47. })
  48. it('does not execute the callback if the method is not excluded', function () {
  49. req.method = 'GET'
  50. middleware.middleware(req, {}, next)
  51. expect(req.session).not.to.exist
  52. })
  53. it('does not execute the callback if the method is not excluded and ?autostartSession=true is set', function () {
  54. req.method = 'GET'
  55. req.query = { autostartSession: 'true' }
  56. middleware.middleware(req, {}, next)
  57. expect(req.session).not.to.exist
  58. })
  59. it('does not execute the callback if the path is not excluded', function () {
  60. req.path = '/giraffe'
  61. middleware.middleware(req, {}, next)
  62. expect(req.session).not.to.exist
  63. })
  64. it('does not execute the callback if there is a cookie', function () {
  65. req.signedCookies[cookieName] = 'a very useful session cookie'
  66. middleware.middleware(req, {}, next)
  67. expect(req.session).not.to.exist
  68. })
  69. })
  70. describe('bot middlewear', function () {
  71. let req, next
  72. beforeEach(function () {
  73. req = {
  74. signedCookies: {},
  75. headers: {},
  76. }
  77. next = sinon.stub()
  78. })
  79. it('GoogleHC user agent should have an empty session', function () {
  80. req.headers['user-agent'] = 'GoogleHC'
  81. middleware.middleware(req, {}, next)
  82. expect(req.session.noSessionCallback).to.deep.exist
  83. })
  84. it('should not add empty session with a firefox useragent', function () {
  85. req.headers['user-agent'] = 'firefox'
  86. middleware.middleware(req, {}, next)
  87. expect(req.session).not.to.exist
  88. })
  89. it('should not add empty session with a empty useragent', function () {
  90. middleware.middleware(req, {}, next)
  91. expect(req.session).not.to.exist
  92. })
  93. })
  94. })