tests: Factor out common server setup/teardown

pull/4391/head
Richard Hansen 2020-10-01 16:23:32 -04:00 committed by John McLear
parent 377560eb51
commit 32b6d8e37f
3 changed files with 49 additions and 44 deletions

43
tests/backend/common.js Normal file
View File

@ -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;
};

View File

@ -1,36 +1,17 @@
function m(mod) { return __dirname + '/../../../src/' + mod; } function m(mod) { return __dirname + '/../../../src/' + mod; }
const assert = require('assert').strict; const assert = require('assert').strict;
const common = require('../common');
const io = require(m('node_modules/socket.io-client')); const io = require(m('node_modules/socket.io-client'));
const log4js = require(m('node_modules/log4js'));
const padManager = require(m('node/db/PadManager')); const padManager = require(m('node/db/PadManager'));
const plugins = require(m('static/js/pluginfw/plugin_defs')); 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 setCookieParser = require(m('node_modules/set-cookie-parser'));
const settings = require(m('node/utils/Settings')); 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 agent;
let baseUrl;
let authnFailureDelayMsBackup;
before(async function() { before(async function() { agent = await common.init(); });
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();
});
// Waits for and returns the next named socket.io event. Rejects if there is any error while waiting // Waits for and returns the next named socket.io event. Rejects if there is any error while waiting
// (unless waiting for that error event). // (unless waiting for that error event).
@ -75,7 +56,7 @@ const connect = async (res) => {
}).join('; '); }).join('; ');
logger.debug('socket.io connecting...'); logger.debug('socket.io connecting...');
const socket = io(`${baseUrl}/`, { const socket = io(`${common.baseUrl}/`, {
forceNew: true, // Different tests will have different query parameters. forceNew: true, // Different tests will have different query parameters.
path: '/socket.io', path: '/socket.io',
// socketio.js-client on node.js doesn't support cookies (see https://git.io/JU8u9), so the // socketio.js-client on node.js doesn't support cookies (see https://git.io/JU8u9), so the

View File

@ -1,32 +1,13 @@
function m(mod) { return __dirname + '/../../../src/' + mod; } function m(mod) { return __dirname + '/../../../src/' + mod; }
const assert = require('assert').strict; 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 plugins = require(m('static/js/pluginfw/plugin_defs'));
const server = require(m('node/server'));
const settings = require(m('node/utils/Settings')); const settings = require(m('node/utils/Settings'));
const supertest = require(m('node_modules/supertest'));
const webaccess = require(m('node/hooks/express/webaccess'));
let agent; let agent;
const logger = log4js.getLogger('test');
let authnFailureDelayMsBackup;
before(async function() { before(async function() { agent = await common.init(); });
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();
});
describe('webaccess: without plugins', function() { describe('webaccess: without plugins', function() {
const backup = {}; const backup = {};