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"; 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[] { return MIGRATIONS; } protected async init(): Promise { this.registerComponents(); this.registerWebSocketListeners(); this.registerControllers(); } protected registerComponents() { const redisComponent = new RedisComponent(); const mysqlComponent = new MysqlComponent(); const expressAppComponent = new ExpressAppComponent(this.port); this.use(expressAppComponent); // Base this.use(new NunjucksComponent('test/views')); this.use(new LogRequestsComponent()); // 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 { public async getProofForSession(session: Express.Session): Promise { return await MagicLink.bySessionID(session.id, ['login', 'register']); } })); // Utils this.use(new FormHelperComponent()); } protected registerWebSocketListeners() { } protected registerControllers() { } } export const DEFAULT_ADDR = 'http://localhost:8966';