swaf/test/_app.ts

98 lines
3.2 KiB
TypeScript
Raw Normal View History

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";
import RedirectBackComponent from "../src/components/RedirectBackComponent";
import ServeStaticDirectoryComponent from "../src/components/ServeStaticDirectoryComponent";
export default function useApp(appSupplier?: (port: number) => TestApp) {
let app: Application;
beforeAll(async (done) => {
await setupMailServer();
app = appSupplier ? appSupplier(8966) : new TestApp(8966);
await app.start();
done();
});
afterAll(async (done) => {
await app.stop();
await teardownMailServer();
done();
});
}
export class TestApp extends Application {
private readonly port: number;
constructor(port: number) {
super(require('../package.json').version, true);
this.port = port;
}
protected getMigrations(): Type<Migration>[] {
return MIGRATIONS;
}
protected async init(): Promise<void> {
this.registerComponents();
this.registerWebSocketListeners();
this.registerControllers();
}
protected registerComponents() {
const expressAppComponent = new ExpressAppComponent(this.port);
const redisComponent = new RedisComponent();
const mysqlComponent = new MysqlComponent();
// Base
this.use(expressAppComponent);
this.use(new LogRequestsComponent());
// Static files
this.use(new ServeStaticDirectoryComponent('public'));
// Dynamic views and routes
this.use(new NunjucksComponent('test/views'));
this.use(new RedirectBackComponent());
// 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() {
}
}
export const DEFAULT_ADDR = 'http://localhost:8966';