test(db): run the session revocation, api docs, app config and help suites on the real database module

They boot the app without the SQLite snapshot mock, so they run in the
Postgres lane as well. sessionRowsOf reads a user's session rows through
a booted app's ORM, next to sessionRows for the suites that still hand in
a better-sqlite3 handle.
This commit is contained in:
Maurice
2026-11-17 15:50:49 +01:00
parent 53ab10afdc
commit b11d8d492c
7 changed files with 31 additions and 58 deletions
+16 -19
View File
@@ -5,16 +5,16 @@
* auth routes alone. An admin setting a password, clearing two-factor or
* deleting an account goes through AdminModule, which pulls in most of the
* app, so this suite boots the whole thing with `buildApp()` against the
* migrated snapshot, every global guard included. The member signs in twice
* real database module, every global guard included. The member signs in twice
* for real (two tracked sessions), the admin acts, and both of the member's
* cookies must stop working while the admin's own session carries on.
*/
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
import { Users } from '../../src/db/entities/Users.entity';
import { createAdmin, createUser } from '../helpers/factories';
import { findRow, updateRows } from '../helpers/factories/rows';
import { sessionRows } from '../helpers/sessions';
import { makeAdmin, makeUser } from '../helpers/factories/users';
import { resetDatabase } from '../helpers/reset-database';
import { sessionRowsOf } from '../helpers/sessions';
import { resetRateLimits } from '../helpers/test-db';
import { MikroORM } from '@mikro-orm/core';
import type { INestApplication } from '@nestjs/common';
@@ -22,22 +22,20 @@ import type { INestApplication } from '@nestjs/common';
import type { Application } from 'express';
import jwt from 'jsonwebtoken';
import request from 'supertest';
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
vi.mock('../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
let nestApp: INestApplication;
let app: Application;
let orm: MikroORM;
beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
orm = nestApp.get(MikroORM);
});
beforeEach(async () => {
await resetDatabase(orm);
await resetRateLimits(nestApp);
});
@@ -55,13 +53,12 @@ async function signIn(email: string, password: string, device: string): Promise<
const sessionIdOf = (cookie: string) => (jwt.decode(cookie.slice('trek_session='.length)) as { jti: string }).jti;
const me = (cookie: string) => request(app).get('/api/auth/me').set('Cookie', cookie);
const liveRows = async (userId: number) =>
(await sessionRows(testDb as never, userId)).filter((row) => row.revoked_at === null);
const liveRows = async (userId: number) => (await sessionRowsOf(orm, userId)).filter((row) => row.revoked_at === null);
/** An admin and a member, each signed in; the member on two devices. */
async function signedInPair(name: string) {
const admin = createAdmin(testDb as never, { username: `${name}-admin`, email: `${name}-admin@example.test` });
const member = createUser(testDb as never, { username: `${name}-member`, email: `${name}-member@example.test` });
const admin = await makeAdmin(orm, { username: `${name}-admin`, email: `${name}-admin@example.test` });
const member = await makeUser(orm, { username: `${name}-member`, email: `${name}-member@example.test` });
return {
adminCookie: await signIn(admin.user.email, admin.password, 'Admin browser'),
member: member.user,
@@ -83,7 +80,7 @@ describe("Admin actions that end another account's sessions", () => {
expect((await me(laptop)).status).toBe(401);
expect((await me(phone)).status).toBe(401);
expect((await sessionRows(testDb as never, member.id)).map((row) => row.id).sort()).toEqual(
expect((await sessionRowsOf(orm, member.id)).map((row) => row.id).sort()).toEqual(
[sessionIdOf(laptop), sessionIdOf(phone)].sort(),
);
expect(await liveRows(member.id)).toEqual([]);
@@ -92,7 +89,7 @@ describe("Admin actions that end another account's sessions", () => {
it("SESS-E2E-ADMIN-002: clearing a member's two-factor ends every session of that member", async () => {
const { adminCookie, member, laptop, phone } = await signedInPair('sess-admin-mfa');
await updateRows(nestApp.get(MikroORM), Users, { id: member.id }, { mfa_enabled: 1, mfa_secret: 'x' });
await updateRows(orm, Users, { id: member.id }, { mfa_enabled: 1, mfa_secret: 'x' });
const res = await request(app).delete(`/api/admin/users/${member.id}/mfa`).set('Cookie', adminCookie);
expect(res.status).toBe(200);
@@ -101,19 +98,19 @@ describe("Admin actions that end another account's sessions", () => {
expect((await me(laptop)).status).toBe(401);
expect((await me(phone)).status).toBe(401);
expect(await liveRows(member.id)).toEqual([]);
expect((await findRow(nestApp.get(MikroORM), Users, { id: member.id }))?.mfa_enabled).toBe(0);
expect((await findRow(orm, Users, { id: member.id }))?.mfa_enabled).toBe(0);
expect((await me(adminCookie)).status).toBe(200);
}, 30000);
it("SESS-E2E-ADMIN-003: deleting a member takes the member's session rows with the account", async () => {
const { adminCookie, member, laptop, phone } = await signedInPair('sess-admin-delete');
expect(await sessionRows(testDb as never, member.id)).toHaveLength(2);
expect(await sessionRowsOf(orm, member.id)).toHaveLength(2);
const res = await request(app).delete(`/api/admin/users/${member.id}`).set('Cookie', adminCookie);
expect(res.status).toBe(200);
expect(res.body).toEqual({ success: true });
expect(await sessionRows(testDb as never, member.id)).toEqual([]);
expect(await sessionRowsOf(orm, member.id)).toEqual([]);
expect((await me(laptop)).status).toBe(401);
expect((await me(phone)).status).toBe(401);
expect((await me(adminCookie)).status).toBe(200);