2020-07-08 10:49:29 +02:00
|
|
|
import {setupMailServer, teardownMailServer} from "./_mail_server";
|
|
|
|
import Application from "../src/Application";
|
2020-09-25 23:42:15 +02:00
|
|
|
import Migration, {MigrationType} from "../src/db/Migration";
|
2020-07-08 10:49:29 +02:00
|
|
|
import {MIGRATIONS} from "./_migrations";
|
|
|
|
import ExpressAppComponent from "../src/components/ExpressAppComponent";
|
|
|
|
import RedisComponent from "../src/components/RedisComponent";
|
|
|
|
import MysqlComponent from "../src/components/MysqlComponent";
|
|
|
|
import NunjucksComponent from "../src/components/NunjucksComponent";
|
|
|
|
import LogRequestsComponent from "../src/components/LogRequestsComponent";
|
|
|
|
import MailComponent from "../src/components/MailComponent";
|
|
|
|
import SessionComponent from "../src/components/SessionComponent";
|
|
|
|
import AuthComponent from "../src/auth/AuthComponent";
|
|
|
|
import AuthGuard from "../src/auth/AuthGuard";
|
|
|
|
import MagicLink from "../src/auth/models/MagicLink";
|
|
|
|
import FormHelperComponent from "../src/components/FormHelperComponent";
|
2020-07-28 10:03:25 +02:00
|
|
|
import RedirectBackComponent from "../src/components/RedirectBackComponent";
|
2020-07-28 12:11:10 +02:00
|
|
|
import ServeStaticDirectoryComponent from "../src/components/ServeStaticDirectoryComponent";
|
2020-08-05 12:05:52 +02:00
|
|
|
import {Express} from "express";
|
2020-09-25 23:42:15 +02:00
|
|
|
import packageJson = require('../package.json');
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export default function useApp(appSupplier?: (addr: string, port: number) => TestApp): void {
|
2020-07-08 10:49:29 +02:00
|
|
|
let app: Application;
|
|
|
|
|
|
|
|
beforeAll(async (done) => {
|
|
|
|
await setupMailServer();
|
2020-09-17 21:15:37 +02:00
|
|
|
app = appSupplier ? appSupplier('127.0.0.1', 8966) : new TestApp('127.0.0.1', 8966);
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
await app.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async (done) => {
|
2020-09-25 23:42:15 +02:00
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await app.stop();
|
|
|
|
} catch (e) {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await teardownMailServer();
|
|
|
|
} catch (e) {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length > 0) throw errors;
|
2020-07-08 10:49:29 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TestApp extends Application {
|
2020-09-17 21:15:37 +02:00
|
|
|
private readonly addr: string;
|
2020-07-08 10:49:29 +02:00
|
|
|
private readonly port: number;
|
2020-08-05 12:05:52 +02:00
|
|
|
private expressAppComponent?: ExpressAppComponent;
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(addr: string, port: number) {
|
|
|
|
super(packageJson.version, true);
|
2020-09-17 21:15:37 +02:00
|
|
|
this.addr = addr;
|
2020-07-08 10:49:29 +02:00
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected getMigrations(): MigrationType<Migration>[] {
|
2020-07-08 10:49:29 +02:00
|
|
|
return MIGRATIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
2020-09-25 23:42:15 +02:00
|
|
|
this.registerWebSocketListeners?.();
|
|
|
|
this.registerControllers?.();
|
2020-07-08 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected registerComponents(): void {
|
2020-09-17 21:15:37 +02:00
|
|
|
this.expressAppComponent = new ExpressAppComponent(this.addr, this.port);
|
2020-07-08 10:49:29 +02:00
|
|
|
const redisComponent = new RedisComponent();
|
|
|
|
const mysqlComponent = new MysqlComponent();
|
|
|
|
|
2020-07-28 12:11:10 +02:00
|
|
|
// Base
|
2020-08-05 12:05:52 +02:00
|
|
|
this.use(this.expressAppComponent);
|
2020-07-28 12:11:10 +02:00
|
|
|
this.use(new LogRequestsComponent());
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-07-28 12:11:10 +02:00
|
|
|
// Static files
|
|
|
|
this.use(new ServeStaticDirectoryComponent('public'));
|
|
|
|
|
|
|
|
// Dynamic views and routes
|
2020-09-24 22:33:37 +02:00
|
|
|
this.use(new NunjucksComponent(['test/views', 'views']));
|
2020-07-28 10:03:25 +02:00
|
|
|
this.use(new RedirectBackComponent());
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
// Services
|
|
|
|
this.use(mysqlComponent);
|
|
|
|
this.use(new MailComponent());
|
|
|
|
|
|
|
|
// Session
|
|
|
|
this.use(redisComponent);
|
|
|
|
this.use(new SessionComponent(redisComponent));
|
|
|
|
|
|
|
|
// Auth
|
|
|
|
this.use(new AuthComponent(new class extends AuthGuard<MagicLink> {
|
|
|
|
public async getProofForSession(session: Express.Session): Promise<MagicLink | null> {
|
2020-09-25 23:42:15 +02:00
|
|
|
return await MagicLink.bySessionId(session.id, ['login', 'register']);
|
2020-07-08 10:49:29 +02:00
|
|
|
}
|
2020-11-03 10:29:36 +01:00
|
|
|
}(this)));
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected registerWebSocketListeners?(): void;
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected registerControllers?(): void;
|
2020-08-05 12:05:52 +02:00
|
|
|
|
|
|
|
public getExpressApp(): Express {
|
2020-09-25 23:42:15 +02:00
|
|
|
return this.as(ExpressAppComponent).getExpressApp();
|
2020-08-05 12:05:52 +02:00
|
|
|
}
|
2020-07-08 10:49:29 +02:00
|
|
|
}
|