2020-07-08 10:49:29 +02:00
|
|
|
import {setupMailServer, teardownMailServer} from "./_mail_server";
|
|
|
|
import Application from "../src/Application";
|
|
|
|
import Migration from "../src/db/Migration";
|
|
|
|
import {Type} from "../src/Utils";
|
|
|
|
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-07-08 10:49:29 +02:00
|
|
|
|
2020-09-17 21:15:37 +02:00
|
|
|
export default function useApp(appSupplier?: (addr: string, port: number) => TestApp) {
|
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) => {
|
|
|
|
await app.stop();
|
|
|
|
await teardownMailServer();
|
|
|
|
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-17 21:15:37 +02:00
|
|
|
constructor(addr: string, port: number) {
|
2020-07-08 10:49:29 +02:00
|
|
|
super(require('../package.json').version, true);
|
2020-09-17 21:15:37 +02:00
|
|
|
this.addr = addr;
|
2020-07-08 10:49:29 +02:00
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMigrations(): Type<Migration>[] {
|
|
|
|
return MIGRATIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.registerComponents();
|
|
|
|
this.registerWebSocketListeners();
|
|
|
|
this.registerControllers();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected registerComponents() {
|
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> {
|
|
|
|
return await MagicLink.bySessionID(session.id, ['login', 'register']);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
this.use(new FormHelperComponent());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected registerWebSocketListeners() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected registerControllers() {
|
|
|
|
}
|
2020-08-05 12:05:52 +02:00
|
|
|
|
|
|
|
public getExpressApp(): Express {
|
|
|
|
return this.expressAppComponent!.getExpressApp();
|
|
|
|
}
|
2020-07-08 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DEFAULT_ADDR = 'http://localhost:8966';
|