From d7fe2afad968e786575d7458d513b62f8e82eb2f Mon Sep 17 00:00:00 2001 From: John McLear Date: Fri, 5 Mar 2021 08:23:01 +0000 Subject: [PATCH] sessions: drop query from database and try to extend sessionstore This didn't work, need to investigate correct logic. --- src/node/hooks/express/webaccess.js | 8 ++-- .../specs/api/httpRouteSessionCreation.js | 37 ++++++++----------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/node/hooks/express/webaccess.js b/src/node/hooks/express/webaccess.js index 53573f2d6..26a93beaf 100644 --- a/src/node/hooks/express/webaccess.js +++ b/src/node/hooks/express/webaccess.js @@ -9,7 +9,7 @@ const readOnlyManager = require('../../db/ReadOnlyManager'); hooks.deprecationNotices.authFailure = 'use the authnFailure and authzFailure hooks instead'; -const staticPathsRE = new RegExp(`^/(?:${[ +const staticPaths = [ 'api(?:/.*)?', 'favicon\\.ico', 'ep/pad/connection-diagnostic-info', @@ -24,7 +24,9 @@ const staticPathsRE = new RegExp(`^/(?:${[ 'static/.*', 'stats/?', 'tests/frontend(?:/.*)?', -].join('|')})$`); +]; + +const staticPathsRE = new RegExp(`^/(?:${staticPaths.join('|')})$`); exports.normalizeAuthzLevel = (level) => { if (!level) return false; @@ -199,4 +201,4 @@ exports.expressConfigure = (hookName, args, cb) => { return cb(); }; -exports.staticPathsRE = staticPathsRE; +exports.staticPaths = staticPaths; diff --git a/src/tests/backend/specs/api/httpRouteSessionCreation.js b/src/tests/backend/specs/api/httpRouteSessionCreation.js index d2da1d336..db2bd7e7a 100644 --- a/src/tests/backend/specs/api/httpRouteSessionCreation.js +++ b/src/tests/backend/specs/api/httpRouteSessionCreation.js @@ -3,8 +3,11 @@ const assert = require('assert').strict; const common = require('../../common'); const settings = require('../../../../node/utils/Settings'); -const staticPathsRE = require('../../../../node/hooks/express/webaccess').staticPathsRE; +const shouldNotCreateExpressSession = + require('../../../../node/hooks/express/webaccess').staticPaths; const fs = require('fs'); +const SessionStore = require('../../../../node/db/SessionStore'); +const store = new SessionStore; let agent; const shouldCreateExpressSession = [ @@ -15,22 +18,6 @@ const shouldCreateExpressSession = [ '/admin', ]; -const shouldNotCreateExpressSession = [ - '/', - '/api/', - '/favicon.ico', - '/locales.json', - '/pluginfw/plugin-definitions.json', - '/static/js/pad.js', - '/stats/', -]; - -const getDatabaseSize = () => { - const dbFile = settings.dbSettings.filename; - const database = fs.readFileSync(settings.dbSettings.filename, 'utf8'); - return database.split('\n').length; -} - describe(__filename, function () { before(async function () { agent = await common.init(); }); @@ -41,23 +28,29 @@ describe(__filename, function () { for (const endpoint of shouldNotCreateExpressSession) { it(endpoint, async function () { - const previousCount = getDatabaseSize(); + const previousCount = store.length(); await agent.get(endpoint) .expect(200) .expect(() => { - const newCount = getDatabaseSize(); + const newCount = store.length(); assert(newCount === previousCount); }) }); } - for (const endpoint of shouldCreateExpressSession) { - const previousCount = getDatabaseSize(); + for (let endpoint of shouldCreateExpressSession) { + // clean up endpoint as it's designed for use in regex + endpoint = endpoint.split('(')[0]; + endpoint = endpoint.replace('\\', ''); + endpoint = endpoint.replace('.*', ''); + endpoint = endpoint.replace('?', ''); + const previousCount = store.length(); it(endpoint, async function () { await agent.get(endpoint) .expect(200) .expect(() => { - const newCount = getDatabaseSize(); + const newCount = store.length(); + console.log(newCount); assert(newCount > previousCount); }) });