|
@@ -1,26 +1,25 @@
|
|
|
-const sinon = require('sinon')
|
|
|
|
|
-const { expect } = require('chai')
|
|
|
|
|
|
|
+import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
+import sinon from 'sinon'
|
|
|
const modulePath =
|
|
const modulePath =
|
|
|
- '../../../../app/src/infrastructure/SessionAutostartMiddleware.js'
|
|
|
|
|
-const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
+ '../../../../app/src/infrastructure/SessionAutostartMiddleware.mjs'
|
|
|
|
|
|
|
|
-describe('SessionAutostartMiddleware', function () {
|
|
|
|
|
|
|
+describe('SessionAutostartMiddleware', () => {
|
|
|
let SessionAutostartMiddleware, middleware, Settings
|
|
let SessionAutostartMiddleware, middleware, Settings
|
|
|
const cookieName = 'coookieee'
|
|
const cookieName = 'coookieee'
|
|
|
const excludedRoute = '/wombat/potato'
|
|
const excludedRoute = '/wombat/potato'
|
|
|
const excludedMethod = 'POST'
|
|
const excludedMethod = 'POST'
|
|
|
const excludedCallback = () => 'call me'
|
|
const excludedCallback = () => 'call me'
|
|
|
|
|
|
|
|
- beforeEach(function () {
|
|
|
|
|
|
|
+ beforeEach(async () => {
|
|
|
Settings = {
|
|
Settings = {
|
|
|
cookieName,
|
|
cookieName,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- SessionAutostartMiddleware = SandboxedModule.require(modulePath, {
|
|
|
|
|
- requires: {
|
|
|
|
|
- '@overleaf/settings': Settings,
|
|
|
|
|
- },
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ vi.doMock('@overleaf/settings', () => ({
|
|
|
|
|
+ default: Settings,
|
|
|
|
|
+ }))
|
|
|
|
|
+
|
|
|
|
|
+ SessionAutostartMiddleware = (await import(modulePath)).default
|
|
|
|
|
|
|
|
middleware = new SessionAutostartMiddleware()
|
|
middleware = new SessionAutostartMiddleware()
|
|
|
middleware.disableSessionAutostartForRoute(
|
|
middleware.disableSessionAutostartForRoute(
|
|
@@ -30,10 +29,10 @@ describe('SessionAutostartMiddleware', function () {
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- describe('middleware', function () {
|
|
|
|
|
|
|
+ describe('middleware', () => {
|
|
|
let req, next
|
|
let req, next
|
|
|
|
|
|
|
|
- beforeEach(function () {
|
|
|
|
|
|
|
+ beforeEach(() => {
|
|
|
req = {
|
|
req = {
|
|
|
path: excludedRoute,
|
|
path: excludedRoute,
|
|
|
method: excludedMethod,
|
|
method: excludedMethod,
|
|
@@ -43,46 +42,46 @@ describe('SessionAutostartMiddleware', function () {
|
|
|
next = sinon.stub()
|
|
next = sinon.stub()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('executes the callback for the excluded route', function () {
|
|
|
|
|
|
|
+ it('executes the callback for the excluded route', () => {
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session.noSessionCallback).to.equal(excludedCallback)
|
|
expect(req.session.noSessionCallback).to.equal(excludedCallback)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('does not execute the callback for the excluded route with ?autostartSession=true set', function () {
|
|
|
|
|
|
|
+ it('does not execute the callback for the excluded route with ?autostartSession=true set', () => {
|
|
|
req.query = { autostartSession: 'true' }
|
|
req.query = { autostartSession: 'true' }
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('does not execute the callback if the method is not excluded', function () {
|
|
|
|
|
|
|
+ it('does not execute the callback if the method is not excluded', () => {
|
|
|
req.method = 'GET'
|
|
req.method = 'GET'
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('does not execute the callback if the method is not excluded and ?autostartSession=true is set', function () {
|
|
|
|
|
|
|
+ it('does not execute the callback if the method is not excluded and ?autostartSession=true is set', () => {
|
|
|
req.method = 'GET'
|
|
req.method = 'GET'
|
|
|
req.query = { autostartSession: 'true' }
|
|
req.query = { autostartSession: 'true' }
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('does not execute the callback if the path is not excluded', function () {
|
|
|
|
|
|
|
+ it('does not execute the callback if the path is not excluded', () => {
|
|
|
req.path = '/giraffe'
|
|
req.path = '/giraffe'
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('does not execute the callback if there is a cookie', function () {
|
|
|
|
|
|
|
+ it('does not execute the callback if there is a cookie', () => {
|
|
|
req.signedCookies[cookieName] = 'a very useful session cookie'
|
|
req.signedCookies[cookieName] = 'a very useful session cookie'
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
- describe('bot middlewear', function () {
|
|
|
|
|
|
|
+ describe('bot middlewear', () => {
|
|
|
let req, next
|
|
let req, next
|
|
|
|
|
|
|
|
- beforeEach(function () {
|
|
|
|
|
|
|
+ beforeEach(() => {
|
|
|
req = {
|
|
req = {
|
|
|
signedCookies: {},
|
|
signedCookies: {},
|
|
|
headers: {},
|
|
headers: {},
|
|
@@ -90,19 +89,19 @@ describe('SessionAutostartMiddleware', function () {
|
|
|
next = sinon.stub()
|
|
next = sinon.stub()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('GoogleHC user agent should have an empty session', function () {
|
|
|
|
|
|
|
+ it('GoogleHC user agent should have an empty session', () => {
|
|
|
req.headers['user-agent'] = 'GoogleHC'
|
|
req.headers['user-agent'] = 'GoogleHC'
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session.noSessionCallback).to.deep.exist
|
|
expect(req.session.noSessionCallback).to.deep.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should not add empty session with a firefox useragent', function () {
|
|
|
|
|
|
|
+ it('should not add empty session with a firefox useragent', () => {
|
|
|
req.headers['user-agent'] = 'firefox'
|
|
req.headers['user-agent'] = 'firefox'
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should not add empty session with a empty useragent', function () {
|
|
|
|
|
|
|
+ it('should not add empty session with a empty useragent', () => {
|
|
|
middleware.middleware(req, {}, next)
|
|
middleware.middleware(req, {}, next)
|
|
|
expect(req.session).not.to.exist
|
|
expect(req.session).not.to.exist
|
|
|
})
|
|
})
|