2020-12-08 08:20:59 +00:00
|
|
|
'use strict';
|
2020-10-08 05:42:49 +00:00
|
|
|
|
2020-12-08 08:20:59 +00:00
|
|
|
const apiHandler = require('ep_etherpad-lite/node/handler/APIHandler');
|
|
|
|
const log4js = require('ep_etherpad-lite/node_modules/log4js');
|
2020-10-08 05:42:49 +00:00
|
|
|
const process = require('process');
|
2020-12-08 08:20:59 +00:00
|
|
|
const server = require('ep_etherpad-lite/node/server');
|
|
|
|
const settings = require('ep_etherpad-lite/node/utils/Settings');
|
|
|
|
const supertest = require('ep_etherpad-lite/node_modules/supertest');
|
|
|
|
const webaccess = require('ep_etherpad-lite/node/hooks/express/webaccess');
|
2020-10-01 20:23:32 +00:00
|
|
|
|
|
|
|
const backups = {};
|
|
|
|
let inited = false;
|
|
|
|
|
2020-10-08 05:37:17 +00:00
|
|
|
exports.apiKey = apiHandler.exportedForTestingOnly.apiKey;
|
2020-10-01 20:23:32 +00:00
|
|
|
exports.agent = null;
|
|
|
|
exports.baseUrl = null;
|
|
|
|
exports.httpServer = null;
|
|
|
|
exports.logger = log4js.getLogger('test');
|
|
|
|
|
2020-10-04 21:29:45 +00:00
|
|
|
const logLevel = exports.logger.level;
|
|
|
|
|
2020-10-08 05:42:49 +00:00
|
|
|
// Mocha doesn't monitor unhandled Promise rejections, so convert them to uncaught exceptions.
|
|
|
|
// https://github.com/mochajs/mocha/issues/2640
|
|
|
|
process.on('unhandledRejection', (reason, promise) => { throw reason; });
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
exports.init = async function () {
|
2020-10-01 20:23:32 +00:00
|
|
|
if (inited) return exports.agent;
|
|
|
|
inited = true;
|
|
|
|
|
2020-10-04 21:29:45 +00:00
|
|
|
if (!logLevel.isLessThanOrEqualTo(log4js.levels.DEBUG)) {
|
|
|
|
exports.logger.warn('Disabling non-test logging for the duration of the test. ' +
|
|
|
|
'To enable non-test logging, change the loglevel setting to DEBUG.');
|
|
|
|
log4js.setGlobalLogLevel(log4js.levels.OFF);
|
|
|
|
exports.logger.setLevel(logLevel);
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:23:32 +00:00
|
|
|
// 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;
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
after(async function () {
|
2020-10-01 20:23:32 +00:00
|
|
|
webaccess.authnFailureDelayMs = backups.authnFailureDelayMs;
|
|
|
|
await server.stop();
|
|
|
|
// Note: This does not unset settings that were added.
|
|
|
|
Object.assign(settings, backups.settings);
|
2020-10-04 21:29:45 +00:00
|
|
|
log4js.setGlobalLogLevel(logLevel);
|
2020-10-01 20:23:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return exports.agent;
|
|
|
|
};
|