diff --git a/tests/backend/common.js b/tests/backend/common.js new file mode 100644 index 000000000..666d69eca --- /dev/null +++ b/tests/backend/common.js @@ -0,0 +1,43 @@ +function m(mod) { return __dirname + '/../../src/' + mod; } + +const log4js = require(m('node_modules/log4js')); +const server = require(m('node/server')); +const settings = require(m('node/utils/Settings')); +const supertest = require(m('node_modules/supertest')); +const webaccess = require(m('node/hooks/express/webaccess')); + +const backups = {}; +let inited = false; + +exports.agent = null; +exports.baseUrl = null; +exports.httpServer = null; +exports.logger = log4js.getLogger('test'); + +exports.init = async function() { + if (inited) return exports.agent; + inited = true; + + // Note: This is only a shallow backup. + backups.settings = Object.assign({}, settings); + // Start the Etherpad server on a random unused port. + settings.port = 0; + settings.ip = 'localhost'; + exports.httpServer = await server.start(); + exports.baseUrl = `http://localhost:${exports.httpServer.address().port}`; + exports.logger.debug(`HTTP server at ${exports.baseUrl}`); + // Create a supertest user agent for the HTTP server. + exports.agent = supertest(exports.baseUrl); + // Speed up authn tests. + backups.authnFailureDelayMs = webaccess.authnFailureDelayMs; + webaccess.authnFailureDelayMs = 0; + + after(async function() { + webaccess.authnFailureDelayMs = backups.authnFailureDelayMs; + await server.stop(); + // Note: This does not unset settings that were added. + Object.assign(settings, backups.settings); + }); + + return exports.agent; +}; diff --git a/tests/backend/specs/socketio.js b/tests/backend/specs/socketio.js index 6a4541d74..e17af09fc 100644 --- a/tests/backend/specs/socketio.js +++ b/tests/backend/specs/socketio.js @@ -1,36 +1,17 @@ function m(mod) { return __dirname + '/../../../src/' + mod; } const assert = require('assert').strict; +const common = require('../common'); const io = require(m('node_modules/socket.io-client')); -const log4js = require(m('node_modules/log4js')); const padManager = require(m('node/db/PadManager')); const plugins = require(m('static/js/pluginfw/plugin_defs')); -const server = require(m('node/server')); const setCookieParser = require(m('node_modules/set-cookie-parser')); const settings = require(m('node/utils/Settings')); -const supertest = require(m('node_modules/supertest')); -const webaccess = require(m('node/hooks/express/webaccess')); -const logger = log4js.getLogger('test'); +const logger = common.logger; let agent; -let baseUrl; -let authnFailureDelayMsBackup; -before(async function() { - authnFailureDelayMsBackup = webaccess.authnFailureDelayMs; - webaccess.authnFailureDelayMs = 0; // Speed up tests. - settings.port = 0; - settings.ip = 'localhost'; - const httpServer = await server.start(); - baseUrl = `http://localhost:${httpServer.address().port}`; - logger.debug(`HTTP server at ${baseUrl}`); - agent = supertest(baseUrl); -}); - -after(async function() { - webaccess.authnFailureDelayMs = authnFailureDelayMsBackup; - await server.stop(); -}); +before(async function() { agent = await common.init(); }); // Waits for and returns the next named socket.io event. Rejects if there is any error while waiting // (unless waiting for that error event). @@ -75,7 +56,7 @@ const connect = async (res) => { }).join('; '); logger.debug('socket.io connecting...'); - const socket = io(`${baseUrl}/`, { + const socket = io(`${common.baseUrl}/`, { forceNew: true, // Different tests will have different query parameters. path: '/socket.io', // socketio.js-client on node.js doesn't support cookies (see https://git.io/JU8u9), so the diff --git a/tests/backend/specs/webaccess.js b/tests/backend/specs/webaccess.js index 7bce670c4..68d98a7c5 100644 --- a/tests/backend/specs/webaccess.js +++ b/tests/backend/specs/webaccess.js @@ -1,32 +1,13 @@ function m(mod) { return __dirname + '/../../../src/' + mod; } const assert = require('assert').strict; -const log4js = require(m('node_modules/log4js')); +const common = require('../common'); const plugins = require(m('static/js/pluginfw/plugin_defs')); -const server = require(m('node/server')); const settings = require(m('node/utils/Settings')); -const supertest = require(m('node_modules/supertest')); -const webaccess = require(m('node/hooks/express/webaccess')); let agent; -const logger = log4js.getLogger('test'); -let authnFailureDelayMsBackup; -before(async function() { - authnFailureDelayMsBackup = webaccess.authnFailureDelayMs; - webaccess.authnFailureDelayMs = 0; // Speed up tests. - settings.port = 0; - settings.ip = 'localhost'; - const httpServer = await server.start(); - const baseUrl = `http://localhost:${httpServer.address().port}`; - logger.debug(`HTTP server at ${baseUrl}`); - agent = supertest(baseUrl); -}); - -after(async function() { - webaccess.authnFailureDelayMs = authnFailureDelayMsBackup; - await server.stop(); -}); +before(async function() { agent = await common.init(); }); describe('webaccess: without plugins', function() { const backup = {};