mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
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:
@@ -38,7 +38,6 @@
|
||||
"tests/e2e/reservations.e2e.test.ts": 1,
|
||||
"tests/e2e/roadtrip.e2e.test.ts": 1,
|
||||
"tests/e2e/school-holidays.e2e.test.ts": 1,
|
||||
"tests/e2e/session-revocation.e2e.test.ts": 1,
|
||||
"tests/e2e/sessions.e2e.test.ts": 1,
|
||||
"tests/e2e/settings.e2e.test.ts": 1,
|
||||
"tests/e2e/share.e2e.test.ts": 1,
|
||||
@@ -55,8 +54,6 @@
|
||||
"tests/e2e/weather.e2e.test.ts": 1,
|
||||
"tests/integration/admin.test.ts": 1,
|
||||
"tests/integration/airtrail-sync-request-context.test.ts": 1,
|
||||
"tests/integration/api-docs.test.ts": 1,
|
||||
"tests/integration/app-config.test.ts": 1,
|
||||
"tests/integration/assignments.test.ts": 1,
|
||||
"tests/integration/async-closure-truthiness-guards.test.ts": 1,
|
||||
"tests/integration/atlas-geocode-request-context.test.ts": 1,
|
||||
@@ -73,7 +70,6 @@
|
||||
"tests/integration/doc-sync-webhook-request-context.test.ts": 1,
|
||||
"tests/integration/feeds-request-context.test.ts": 1,
|
||||
"tests/integration/files.test.ts": 1,
|
||||
"tests/integration/help.test.ts": 1,
|
||||
"tests/integration/immich.test.ts": 1,
|
||||
"tests/integration/journey-place-order.test.ts": 1,
|
||||
"tests/integration/journey.test.ts": 1,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { UserSessions } from '../../src/db/entities/UserSessions.entity';
|
||||
import type { UserSessionsRepository } from '../../src/db/repositories/UserSessions.repository';
|
||||
import { SessionsService } from '../../src/nest/sessions/sessions.service';
|
||||
import type { FactoryOrm } from './factories/context';
|
||||
import { findRows } from './factories/rows';
|
||||
import { sharedTestOrm } from './test-uow';
|
||||
|
||||
@@ -26,12 +27,15 @@ export async function sessionRows(
|
||||
db: Database.Database,
|
||||
userId: number,
|
||||
): Promise<{ id: string; revoked_at: string | null; expires_at: string; user_agent: string | null }[]> {
|
||||
const rows = await findRows(
|
||||
await sharedTestOrm(db),
|
||||
UserSessions,
|
||||
{ user: userId },
|
||||
{ created_at: 'asc', id: 'asc' },
|
||||
);
|
||||
return sessionRowsOf(await sharedTestOrm(db), userId);
|
||||
}
|
||||
|
||||
/** The session rows of a user, oldest first, read through the given ORM (a booted app's, on either engine). */
|
||||
export async function sessionRowsOf(
|
||||
orm: FactoryOrm,
|
||||
userId: number,
|
||||
): Promise<{ id: string; revoked_at: string | null; expires_at: string; user_agent: string | null }[]> {
|
||||
const rows = await findRows(orm, UserSessions, { user: userId }, { created_at: 'asc', id: 'asc' });
|
||||
return rows.map(({ id, revoked_at, expires_at, user_agent }) => ({
|
||||
id,
|
||||
revoked_at: revoked_at ?? null,
|
||||
|
||||
@@ -5,18 +5,13 @@
|
||||
* Boots the real buildApp() like bootstrap.test.ts.
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import { apiDocsEnabled } from '../../src/nest/common/api-docs.kill-switch';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import { resetDatabase } from '../helpers/reset-database';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
import request from 'supertest';
|
||||
import { describe, it, expect, vi, beforeAll, 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, afterAll } from 'vitest';
|
||||
|
||||
describe('API-DOCS (#1412) — flag-gated OpenAPI surface', () => {
|
||||
let app: INestApplication;
|
||||
@@ -24,18 +19,17 @@ describe('API-DOCS (#1412) — flag-gated OpenAPI surface', () => {
|
||||
let prevFlag: string | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
resetTestDb(testDb);
|
||||
prevFlag = process.env.TREK_API_DOCS_ENABLED;
|
||||
process.env.TREK_API_DOCS_ENABLED = 'true';
|
||||
app = await buildApp();
|
||||
instance = app.getHttpAdapter().getInstance();
|
||||
await resetDatabase(app.get(MikroORM));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (prevFlag === undefined) delete process.env.TREK_API_DOCS_ENABLED;
|
||||
else process.env.TREK_API_DOCS_ENABLED = prevFlag;
|
||||
await app.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
it('DOCS-001 — the kill switch parses the boolean-like env family', () => {
|
||||
|
||||
@@ -5,18 +5,11 @@
|
||||
* live within a single app's lifetime.
|
||||
*/
|
||||
import { buildApp, getHttpServer } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import { httpConfig, RuntimeEnvService } from '../../src/nest/app-config';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import type { ConfigType } from '@nestjs/config';
|
||||
|
||||
import { describe, it, expect, vi, afterEach, beforeAll } from 'vitest';
|
||||
|
||||
vi.mock('../../src/db/database', async () => {
|
||||
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
|
||||
return buildDbMock(createSnapshotTestDb());
|
||||
});
|
||||
import { describe, it, expect, afterEach, beforeAll } from 'vitest';
|
||||
|
||||
describe('AppConfigModule in the real buildApp()', () => {
|
||||
let app: INestApplication | undefined;
|
||||
@@ -24,7 +17,6 @@ describe('AppConfigModule in the real buildApp()', () => {
|
||||
let prevDemo: string | undefined;
|
||||
|
||||
beforeAll(() => {
|
||||
resetTestDb(testDb);
|
||||
prevHsts = process.env.HSTS_INCLUDE_SUBDOMAINS;
|
||||
prevDemo = process.env.DEMO_MODE;
|
||||
});
|
||||
|
||||
@@ -6,18 +6,12 @@
|
||||
* what a broken path or a wiki missing from the image would silently cost us.
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { db as testDb } from '../../src/db/database';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
import type { Application } from 'express';
|
||||
import request from 'supertest';
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
|
||||
|
||||
vi.mock('../../src/db/database', async () => {
|
||||
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
|
||||
return buildDbMock(createSnapshotTestDb());
|
||||
});
|
||||
|
||||
let nestApp: INestApplication;
|
||||
let app: Application;
|
||||
const fetchSpy = vi.fn(async () => new Response('{}', { status: 200 }));
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
[
|
||||
"tests/e2e/session-revocation.e2e.test.ts",
|
||||
"tests/integration/admin.test.ts",
|
||||
"tests/integration/airtrail-sync-request-context.test.ts",
|
||||
"tests/integration/api-docs.test.ts",
|
||||
"tests/integration/app-config.test.ts",
|
||||
"tests/integration/assignments.test.ts",
|
||||
"tests/integration/async-closure-truthiness-guards.test.ts",
|
||||
"tests/integration/atlas-geocode-request-context.test.ts",
|
||||
@@ -19,7 +16,6 @@
|
||||
"tests/integration/doc-sync-webhook-request-context.test.ts",
|
||||
"tests/integration/feeds-request-context.test.ts",
|
||||
"tests/integration/files.test.ts",
|
||||
"tests/integration/help.test.ts",
|
||||
"tests/integration/immich.test.ts",
|
||||
"tests/integration/journey-place-order.test.ts",
|
||||
"tests/integration/journey.test.ts",
|
||||
|
||||
Reference in New Issue
Block a user