SessionAutostartMiddlewareTests.js 2.7 KB

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