2021-07-12 08:43:46 +02:00
|
|
|
import Mocha from "mocha";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2021-09-23 00:52:35 +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";
|
2021-07-12 08:43:46 +02:00
|
|
|
import * as rateLimitMiddlewareModule from "../src/middleware/requestRateLimit";
|
|
|
|
import rateLimit from "express-rate-limit";
|
2021-12-19 08:03:50 +01:00
|
|
|
import redis from "../src/utils/redis";
|
2020-10-20 21:56:07 +02:00
|
|
|
|
2021-03-07 06:21:56 +01:00
|
|
|
async function init() {
|
2021-07-12 08:43:46 +02:00
|
|
|
ImportMock.mockFunction(rateLimitMiddlewareModule, "rateLimitMiddleware", rateLimit({
|
2021-07-07 09:47:08 +02:00
|
|
|
skip: () => true
|
2021-03-07 06:21:56 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
// 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-07-12 08:43:46 +02:00
|
|
|
const dbMode = config.mysql ? "mysql"
|
|
|
|
: config.postgres ? "postgres"
|
|
|
|
: "sqlite";
|
|
|
|
Logger.info(`Database Mode: ${dbMode}`);
|
2021-06-23 20:48:47 +02:00
|
|
|
|
2021-09-01 22:52:41 +02:00
|
|
|
// set commit at headCommit
|
|
|
|
(global as any).HEADCOMMIT = "test";
|
|
|
|
|
2021-03-07 06:21:56 +01:00
|
|
|
// Instantiate a Mocha instance.
|
|
|
|
const mocha = new Mocha();
|
2020-10-17 20:56:54 +02:00
|
|
|
|
2022-08-22 17:07:30 +02:00
|
|
|
const testDirs = ["./test/cases"];
|
2021-03-07 06:21:56 +01:00
|
|
|
|
|
|
|
// Add each .ts file to the mocha instance
|
2021-11-15 05:09:12 +01:00
|
|
|
testDirs.forEach(testDir => {
|
|
|
|
fs.readdirSync(testDir)
|
|
|
|
.filter((file) =>
|
|
|
|
// Only keep the .ts files
|
2022-01-07 07:59:47 +01:00
|
|
|
file.slice(-3) === ".ts"
|
2021-11-15 05:09:12 +01:00
|
|
|
)
|
|
|
|
.forEach(function(file) {
|
|
|
|
mocha.addFile(
|
|
|
|
path.join(testDir, file)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-03-07 06:21:56 +01:00
|
|
|
|
|
|
|
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();
|
2022-04-13 23:36:07 +02:00
|
|
|
redis.quit();
|
2021-03-07 06:21:56 +01:00
|
|
|
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();
|