2020-11-16 12:08:39 +01:00
|
|
|
import supertest from "supertest";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
import AuthComponent from "../src/auth/AuthComponent.js";
|
|
|
|
import AddNameToUsersMigration from "../src/auth/migrations/AddNameToUsersMigration.js";
|
|
|
|
import MagicLink from "../src/auth/models/MagicLink.js";
|
|
|
|
import PasswordAuthProof from "../src/auth/password/PasswordAuthProof.js";
|
|
|
|
import CsrfProtectionComponent from "../src/components/CsrfProtectionComponent.js";
|
|
|
|
import Controller from "../src/Controller.js";
|
|
|
|
import Migration, {MigrationType} from "../src/db/Migration.js";
|
|
|
|
import TestApp from "../src/TestApp.js";
|
|
|
|
import {AppSupplier} from "./_app.js";
|
|
|
|
import {popEmail} from "./_mail_server.js";
|
2020-11-16 12:08:39 +01:00
|
|
|
|
|
|
|
export async function followMagicLinkFromMail(
|
|
|
|
agent: supertest.SuperTest<supertest.Test>,
|
|
|
|
cookies: string[],
|
2021-01-21 15:58:03 +01:00
|
|
|
expectedRedirectUrl: string = '/',
|
2020-11-16 12:08:39 +01:00
|
|
|
): Promise<void> {
|
|
|
|
const mail: Record<string, unknown> | null = await popEmail();
|
|
|
|
expect(mail).not.toBeNull();
|
|
|
|
|
|
|
|
const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0];
|
|
|
|
expect(query).toBeDefined();
|
|
|
|
|
|
|
|
await agent.get('/magic/link?' + query)
|
|
|
|
.expect(200);
|
|
|
|
await agent.get('/magic/lobby')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.expect(302)
|
2021-01-21 15:58:03 +01:00
|
|
|
.expect('Location', expectedRedirectUrl);
|
2020-11-16 12:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function testLogout(
|
|
|
|
agent: supertest.SuperTest<supertest.Test>,
|
|
|
|
cookies: string[],
|
|
|
|
csrf: string,
|
|
|
|
): Promise<void> {
|
|
|
|
// Authenticated
|
|
|
|
await agent.get('/is-auth').set('Cookie', cookies).expect(200);
|
|
|
|
|
|
|
|
// Logout
|
|
|
|
await agent.post('/auth/logout')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.send({csrf: csrf})
|
|
|
|
.expect(302);
|
|
|
|
|
|
|
|
// Not authenticated
|
|
|
|
await agent.get('/is-auth').set('Cookie', cookies).expect(401);
|
|
|
|
}
|
2021-04-22 15:38:24 +02:00
|
|
|
|
|
|
|
export function authAppProvider(withUsername: boolean = true, approvalMode: boolean = false): AppSupplier<TestApp> {
|
|
|
|
return async (addr, port) => {
|
|
|
|
return new class extends TestApp {
|
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.use(new class extends Controller {
|
|
|
|
public routes(): void {
|
|
|
|
this.get('/', (req, res) => {
|
|
|
|
res.render('home');
|
|
|
|
}, 'home');
|
|
|
|
this.get('/csrf', (req, res) => {
|
2021-11-24 22:08:38 +01:00
|
|
|
res.send(this.getApp().as(CsrfProtectionComponent).getSessionCsrfToken(req.getSession()));
|
2021-04-22 15:38:24 +02:00
|
|
|
}, 'csrf');
|
|
|
|
this.get('/is-auth', async (req, res) => {
|
|
|
|
const proofs = await this.getApp().as(AuthComponent).getAuthGuard().getProofs(req);
|
|
|
|
if (proofs.length > 0) res.sendStatus(200);
|
|
|
|
else res.sendStatus(401);
|
|
|
|
}, 'is-auth');
|
|
|
|
this.get('/has-any-password-auth-proof', async (req, res) => {
|
|
|
|
const proof = await PasswordAuthProof.getProofForSession(req.getSession());
|
|
|
|
if (proof) res.sendStatus(200);
|
|
|
|
else res.sendStatus(404);
|
|
|
|
}, 'is-auth');
|
|
|
|
this.get('/has-any-magic-link', async (req, res) => {
|
|
|
|
const proofs = await MagicLink.select()
|
|
|
|
.where('session_id', req.getSession().id)
|
|
|
|
.get();
|
|
|
|
if (proofs.length > 0) res.sendStatus(200);
|
|
|
|
else res.sendStatus(404);
|
|
|
|
}, 'is-auth');
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
await super.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMigrations(): MigrationType<Migration>[] {
|
2021-11-08 00:52:33 +01:00
|
|
|
const migrations = withUsername ?
|
2021-04-22 15:38:24 +02:00
|
|
|
super.getMigrations() :
|
|
|
|
super.getMigrations().filter(m => m !== AddNameToUsersMigration);
|
|
|
|
|
|
|
|
return migrations;
|
|
|
|
}
|
2021-11-08 00:52:33 +01:00
|
|
|
}('test', addr, port, true, approvalMode);
|
2021-04-22 15:38:24 +02:00
|
|
|
};
|
|
|
|
}
|