2020-10-17 20:56:54 +02:00
|
|
|
import Mocha from 'mocha';
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-10-20 21:56:07 +02:00
|
|
|
import {config} from '../src/config';
|
|
|
|
import {createServer} from '../src/app';
|
|
|
|
import {createMockServer} from './mocks';
|
|
|
|
import {Logger} from '../src/utils/logger';
|
|
|
|
import {initDb} from '../src/databases/databases';
|
|
|
|
import {ImportMock} from 'ts-mock-imports';
|
|
|
|
import * as rateLimitMiddlewareModule from '../src/middleware/requestRateLimit';
|
|
|
|
import rateLimit from 'express-rate-limit';
|
|
|
|
|
2021-03-07 06:21:56 +01:00
|
|
|
async function init() {
|
|
|
|
ImportMock.mockFunction(rateLimitMiddlewareModule, 'rateLimitMiddleware', rateLimit({
|
|
|
|
skip: () => {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// delete old test database
|
2021-07-05 09:14:05 +02:00
|
|
|
if (fs.existsSync(config.db)) fs.unlinkSync(config.db);
|
2021-03-07 06:21:56 +01:00
|
|
|
if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB);
|
|
|
|
|
|
|
|
await initDb();
|
|
|
|
|
2021-06-23 20:48:47 +02:00
|
|
|
const dbMode = config.mysql ? 'mysql'
|
|
|
|
: config.postgres ? 'postgres'
|
2021-07-05 09:14:05 +02:00
|
|
|
: 'sqlite';
|
|
|
|
Logger.info('Database Mode: ' + dbMode);
|
2021-06-23 20:48:47 +02:00
|
|
|
|
2021-03-07 06:21:56 +01:00
|
|
|
// Instantiate a Mocha instance.
|
|
|
|
const mocha = new Mocha();
|
2020-10-17 20:56:54 +02:00
|
|
|
|
2021-03-07 06:21:56 +01:00
|
|
|
const testDir = './test/cases';
|
|
|
|
|
|
|
|
// Add each .ts file to the mocha instance
|
|
|
|
fs.readdirSync(testDir)
|
|
|
|
.filter(function(file) {
|
|
|
|
// Only keep the .ts files
|
|
|
|
return file.substr(-3) === '.ts';
|
|
|
|
})
|
|
|
|
.forEach(function(file) {
|
|
|
|
mocha.addFile(
|
|
|
|
path.join(testDir, file)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockServer = createMockServer(() => {
|
|
|
|
Logger.info("Started mock HTTP Server");
|
|
|
|
const server = createServer(() => {
|
|
|
|
Logger.info("Started main HTTP server");
|
|
|
|
// Run the tests.
|
|
|
|
mocha.run((failures) => {
|
|
|
|
mockServer.close();
|
|
|
|
server.close();
|
|
|
|
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
|
|
|
|
});
|
2020-10-17 20:56:54 +02:00
|
|
|
});
|
|
|
|
});
|
2021-03-07 06:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
init();
|