etherpad-lite/src/node/hooks/express/tests.js

95 lines
3 KiB
JavaScript
Raw Normal View History

2021-01-21 21:06:52 +00:00
'use strict';
2020-11-23 13:24:19 -05:00
const path = require('path');
const fs = require('fs');
const fsp = fs.promises;
const settings = require('../../utils/Settings');
2021-01-21 21:06:52 +00:00
exports.expressCreateServer = (hookName, args, cb) => {
2020-11-23 13:24:19 -05:00
args.app.get('/tests/frontend/specs_list.js', async (req, res) => {
const [coreTests, pluginTests] = await Promise.all([
exports.getCoreTests(),
2020-11-23 13:24:19 -05:00
exports.getPluginTests(),
]);
// merge the two sets of results
let files = [].concat(coreTests, pluginTests).sort();
// Keep only *.js files
files = files.filter((f) => f.endsWith('.js'));
// remove admin tests if the setting to enable them isn't in settings.json
if (!settings.enableAdminUITests) {
files = files.filter((file) => file.indexOf('admin') !== 0);
}
2020-11-23 13:24:19 -05:00
console.debug('Sent browser the following test specs:', files);
res.setHeader('content-type', 'application/javascript');
2020-11-23 13:24:19 -05:00
res.end(`var specs_list = ${JSON.stringify(files)};\n`);
});
const rootTestFolder = path.join(settings.root, 'src/tests/frontend/');
2021-01-21 21:06:52 +00:00
const url2FilePath = (url) => {
2020-11-23 13:24:19 -05:00
let subPath = url.substr('/tests/frontend'.length);
2021-01-21 21:06:52 +00:00
if (subPath === '') {
2020-11-23 13:24:19 -05:00
subPath = 'index.html';
}
2020-11-23 13:24:19 -05:00
subPath = subPath.split('?')[0];
let filePath = path.join(rootTestFolder, subPath);
// make sure we jail the paths to the test folder, otherwise serve index
if (filePath.indexOf(rootTestFolder) !== 0) {
2020-11-23 13:24:19 -05:00
filePath = path.join(rootTestFolder, 'index.html');
}
2012-10-27 17:29:17 +01:00
return filePath;
2020-11-23 13:24:19 -05:00
};
2012-10-27 17:29:17 +01:00
args.app.get('/tests/frontend/specs/*', (req, res, next) => {
(async () => {
const specFilePath = url2FilePath(req.url);
const specFileName = path.basename(specFilePath);
let content = await fsp.readFile(specFilePath);
content = `describe(${JSON.stringify(specFileName)}, function(){${content}});`;
2021-01-21 21:06:52 +00:00
if (!specFilePath.endsWith('index.html')) {
res.setHeader('content-type', 'application/javascript');
}
2012-10-27 17:29:17 +01:00
res.send(content);
})().catch((err) => next(err || new Error(err)));
2012-10-27 17:29:17 +01:00
});
2020-11-23 13:24:19 -05:00
args.app.get('/tests/frontend/*', (req, res) => {
const filePath = url2FilePath(req.url);
res.sendFile(filePath);
});
2012-10-27 16:41:17 +01:00
2020-11-23 13:24:19 -05:00
args.app.get('/tests/frontend', (req, res) => {
res.redirect('/tests/frontend/index.html');
});
return cb();
2020-11-23 13:24:19 -05:00
};
2013-02-04 00:00:39 +00:00
2021-01-21 21:06:52 +00:00
exports.getPluginTests = async (callback) => {
2020-11-23 13:24:19 -05:00
const moduleDir = 'node_modules/';
const specPath = '/static/tests/frontend/specs/';
const staticDir = '/static/plugins/';
const pluginSpecs = [];
const plugins = await fsp.readdir(moduleDir);
await Promise.all(plugins
2020-11-23 13:24:19 -05:00
.map((plugin) => [plugin, moduleDir + plugin + specPath])
.filter(([plugin, specDir]) => fs.existsSync(specDir)) // check plugin exists
.map(async ([plugin, specDir]) => {
const specFiles = await fsp.readdir(specDir);
return specFiles.map((spec) => {
pluginSpecs.push(staticDir + plugin + specPath + spec);
});
}));
return pluginSpecs;
2020-11-23 13:24:19 -05:00
};
2013-02-04 00:00:39 +00:00
exports.getCoreTests = async () => await fsp.readdir('src/tests/frontend/specs');