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');
|
2021-05-09 16:33:09 -04:00
|
|
|
const fsp = fs.promises;
|
2021-02-07 11:32:57 +00:00
|
|
|
const settings = require('../../utils/Settings');
|
2012-10-02 00:35:43 +01:00
|
|
|
|
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([
|
2019-01-23 16:21:40 +00:00
|
|
|
exports.getCoreTests(),
|
2020-11-23 13:24:19 -05:00
|
|
|
exports.getPluginTests(),
|
2019-01-23 16:21:40 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// merge the two sets of results
|
|
|
|
let files = [].concat(coreTests, pluginTests).sort();
|
2020-03-22 17:41:57 +00:00
|
|
|
|
2020-10-11 19:56:01 -04:00
|
|
|
// Keep only *.js files
|
|
|
|
files = files.filter((f) => f.endsWith('.js'));
|
2020-03-22 17:41:57 +00:00
|
|
|
|
2021-02-07 11:32:57 +00:00
|
|
|
// 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);
|
2020-12-27 23:32:09 +01:00
|
|
|
res.setHeader('content-type', 'application/javascript');
|
2020-11-23 13:24:19 -05:00
|
|
|
res.end(`var specs_list = ${JSON.stringify(files)};\n`);
|
2012-10-27 17:05:26 +01:00
|
|
|
});
|
|
|
|
|
2021-02-10 01:21:29 -05:00
|
|
|
const rootTestFolder = path.join(settings.root, 'src/tests/frontend/');
|
2015-04-10 20:19:26 -05:00
|
|
|
|
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';
|
2012-10-02 00:35:43 +01:00
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
subPath = subPath.split('?')[0];
|
2012-10-02 00:35:43 +01:00
|
|
|
|
2021-02-10 01:12:43 -05:00
|
|
|
let filePath = path.join(rootTestFolder, subPath);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2015-04-10 19:25:52 -05:00
|
|
|
// 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');
|
2015-04-10 19:25:52 -05:00
|
|
|
}
|
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
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
args.app.get('/tests/frontend/specs/*', (req, res) => {
|
|
|
|
const specFilePath = url2FilePath(req.url);
|
|
|
|
const specFileName = path.basename(specFilePath);
|
2012-10-27 17:29:17 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
fs.readFile(specFilePath, (err, content) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
if (err) { return res.send(500); }
|
|
|
|
|
2021-02-07 11:32:57 +00:00
|
|
|
content = `describe(${JSON.stringify(specFileName)}, function(){${content}});`;
|
2012-10-02 00:35:43 +01:00
|
|
|
|
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);
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
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);
|
2015-04-10 05:52:58 -05:00
|
|
|
res.sendFile(filePath);
|
2012-10-02 00:35:43 +01:00
|
|
|
});
|
2012-10-27 16:41:17 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
args.app.get('/tests/frontend', (req, res) => {
|
2020-04-06 14:15:59 -04:00
|
|
|
res.redirect('/tests/frontend/index.html');
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2020-10-10 22:51:26 -04:00
|
|
|
|
|
|
|
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 = [];
|
|
|
|
|
2021-05-09 16:33:09 -04:00
|
|
|
const plugins = await fsp.readdir(moduleDir);
|
2021-05-09 16:30:53 -04:00
|
|
|
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
|
2021-05-09 16:30:53 -04:00
|
|
|
.map(async ([plugin, specDir]) => {
|
2021-05-09 16:33:09 -04:00
|
|
|
const specFiles = await fsp.readdir(specDir);
|
2021-05-09 16:30:53 -04:00
|
|
|
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
|
|
|
|
2021-05-09 16:33:09 -04:00
|
|
|
exports.getCoreTests = async () => await fsp.readdir('src/tests/frontend/specs');
|