mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
test(server): seed the service, MCP and route specs through the ORM factories
Service, MCP, integration and e2e suites with up to a dozen raw statements now seed and read through the factories. A buildApp() suite uses the app's own ORM, a partial-module e2e or a unit suite a createTestOrm() or sharedTestOrm() handle on its database. Reads that used to come back undefined now come back null, and the assertions say so; where a projected SELECT was compared whole, the same columns are picked off the row. Two reads inside the scheduler's delivery callback stay synchronous raw statements, marked, so they keep seeing the row at the moment of delivery. Two more database mocks lose a canAccessTrip/isOwner stand-in the module no longer exports.
This commit is contained in:
@@ -32,7 +32,11 @@ import { NotificationsModule } from '../../src/nest/notifications/notifications.
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { createTestMikroOrmModule } from '../helpers/test-orm';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { deleteRows, findRows } from '../helpers/factories/rows';
|
||||
import { readAppSetting, setAppSetting } from '../helpers/factories/settings';
|
||||
import { makeUser } from '../helpers/factories/users';
|
||||
import { PushSubscriptions } from '../../src/db/entities/PushSubscriptions.entity';
|
||||
|
||||
const ENDPOINT = 'https://fcm.googleapis.com/fcm/send/e2e-device';
|
||||
|
||||
@@ -46,15 +50,22 @@ function subscription(endpoint = ENDPOINT) {
|
||||
};
|
||||
}
|
||||
|
||||
function rows() {
|
||||
return db
|
||||
.prepare('SELECT user_id, endpoint, vapid_public_key, user_agent FROM push_subscriptions ORDER BY id')
|
||||
.all() as {
|
||||
user_id: number;
|
||||
endpoint: string;
|
||||
vapid_public_key: string;
|
||||
user_agent: string | null;
|
||||
}[];
|
||||
let orm: TestOrm;
|
||||
|
||||
async function rows() {
|
||||
return (await findRows(orm, PushSubscriptions, {}, { id: 'asc' })).map((r) => ({
|
||||
user_id: r.user_id,
|
||||
endpoint: r.endpoint,
|
||||
vapid_public_key: r.vapid_public_key,
|
||||
user_agent: r.user_agent,
|
||||
}));
|
||||
}
|
||||
|
||||
/** An app setting the push service keeps; fails the case when it is not there. */
|
||||
async function storedSetting(key: string): Promise<string> {
|
||||
const value = await readAppSetting(orm, key);
|
||||
if (value === null) throw new Error(`no app setting ${key}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
@@ -74,24 +85,22 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
// harness.ts's seedUser() omits password_hash, which the migrated schema
|
||||
// requires NOT NULL: a raw insert of the same SeededUser shape instead.
|
||||
const seed = db.prepare(
|
||||
"INSERT INTO users (id, username, email, password_hash, role, password_version) VALUES (?, ?, ?, 'x', 'user', 0)",
|
||||
);
|
||||
seed.run(1, 'e2e-user-1', 'one@example.test');
|
||||
seed.run(2, 'e2e-user-2', 'two@example.test');
|
||||
// The two users the session cookies are signed for, pinned to ids 1 and 2.
|
||||
orm = await createTestOrm(db);
|
||||
await makeUser(orm, { id: 1, username: 'e2e-user-1', email: 'one@example.test', role: 'user', password_version: 0 });
|
||||
await makeUser(orm, { id: 2, username: 'e2e-user-2', email: 'two@example.test', role: 'user', password_version: 0 });
|
||||
app = await build();
|
||||
server = app.getHttpServer();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.prepare('DELETE FROM push_subscriptions').run();
|
||||
beforeEach(async () => {
|
||||
await deleteRows(orm, PushSubscriptions);
|
||||
safeFetchFollow.mockReset();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await orm.close();
|
||||
});
|
||||
|
||||
it('401 on every push route without a session cookie', async () => {
|
||||
@@ -106,12 +115,10 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
});
|
||||
|
||||
it('GET public-key: the pair exists from bootstrap on, so the route only reads it', async () => {
|
||||
const stored = db.prepare("SELECT value FROM app_settings WHERE key = 'web_push_vapid_public_key'").get() as {
|
||||
value: string;
|
||||
};
|
||||
const stored = await storedSetting('web_push_vapid_public_key');
|
||||
const res = await request(server).get('/api/notifications/push/public-key').set('Cookie', sessionCookie(1));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual({ publicKey: stored.value });
|
||||
expect(res.body).toEqual({ publicKey: stored });
|
||||
const raw = Buffer.from(res.body.publicKey, 'base64url');
|
||||
expect(raw.length).toBe(65);
|
||||
expect(raw[0]).toBe(0x04);
|
||||
@@ -128,7 +135,7 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
.send({ subscription: subscription() });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual({ success: true, devices: 1 });
|
||||
expect(rows()).toEqual([
|
||||
expect(await rows()).toEqual([
|
||||
{ user_id: 1, endpoint: ENDPOINT, vapid_public_key: key.publicKey, user_agent: 'e2e-browser/1.0' },
|
||||
]);
|
||||
});
|
||||
@@ -143,7 +150,7 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
.set('Cookie', sessionCookie(2))
|
||||
.send({ subscription: subscription() });
|
||||
expect(res.body).toEqual({ success: true, devices: 1 });
|
||||
expect(rows().map((r) => r.user_id)).toEqual([2]);
|
||||
expect((await rows()).map((r) => r.user_id)).toEqual([2]);
|
||||
});
|
||||
|
||||
it('POST subscriptions: 400 with the bespoke message for a host that is not a push service', async () => {
|
||||
@@ -153,7 +160,7 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
.send({ subscription: subscription('https://example.com/collect') });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body).toEqual({ error: 'Push endpoint is not a known push service' });
|
||||
expect(rows()).toEqual([]);
|
||||
expect(await rows()).toEqual([]);
|
||||
});
|
||||
|
||||
it('POST subscriptions: 400 from the contract when the keys are missing', async () => {
|
||||
@@ -176,7 +183,7 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
.set('Cookie', sessionCookie(2))
|
||||
.send({ endpoint: ENDPOINT });
|
||||
expect(stranger.status).toBe(200);
|
||||
expect(rows()).toHaveLength(1);
|
||||
expect(await rows()).toHaveLength(1);
|
||||
|
||||
const owner = await request(server)
|
||||
.delete('/api/notifications/push/subscriptions')
|
||||
@@ -184,7 +191,7 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
.send({ endpoint: ENDPOINT });
|
||||
expect(owner.status).toBe(200);
|
||||
expect(owner.body).toEqual({ success: true });
|
||||
expect(rows()).toEqual([]);
|
||||
expect(await rows()).toEqual([]);
|
||||
|
||||
const again = await request(server)
|
||||
.delete('/api/notifications/push/subscriptions')
|
||||
@@ -225,16 +232,15 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
});
|
||||
|
||||
it('a stored private key that no longer decrypts turns push off with a 503 and is never replaced', async () => {
|
||||
const readKey = (key: string) =>
|
||||
(db.prepare('SELECT value FROM app_settings WHERE key = ?').get(key) as { value: string }).value;
|
||||
const readKey = storedSetting;
|
||||
const unreadable = 'enc:v1:bm90IGEgY2lwaGVydGV4dA';
|
||||
const publicKey = readKey('web_push_vapid_public_key');
|
||||
const ciphertext = readKey('web_push_vapid_private_key');
|
||||
const publicKey = await readKey('web_push_vapid_public_key');
|
||||
const ciphertext = await readKey('web_push_vapid_private_key');
|
||||
await request(server)
|
||||
.post('/api/notifications/push/subscriptions')
|
||||
.set('Cookie', sessionCookie(1))
|
||||
.send({ subscription: subscription() });
|
||||
db.prepare('UPDATE app_settings SET value = ? WHERE key = ?').run(unreadable, 'web_push_vapid_private_key');
|
||||
await setAppSetting(orm, 'web_push_vapid_private_key', unreadable);
|
||||
try {
|
||||
const key = await request(server).get('/api/notifications/push/public-key').set('Cookie', sessionCookie(1));
|
||||
expect(key.status).toBe(503);
|
||||
@@ -249,11 +255,11 @@ describe('Web Push e2e (real auth guard + temp SQLite)', () => {
|
||||
expect(test.body).toEqual({ success: false, error: 'Web Push is currently unavailable on this server' });
|
||||
expect(safeFetchFollow).not.toHaveBeenCalled();
|
||||
// The device registered before stays, and neither stored row was touched.
|
||||
expect(rows().map((r) => r.endpoint)).toEqual([ENDPOINT]);
|
||||
expect(readKey('web_push_vapid_public_key')).toBe(publicKey);
|
||||
expect(readKey('web_push_vapid_private_key')).toBe(unreadable);
|
||||
expect((await rows()).map((r) => r.endpoint)).toEqual([ENDPOINT]);
|
||||
expect(await readKey('web_push_vapid_public_key')).toBe(publicKey);
|
||||
expect(await readKey('web_push_vapid_private_key')).toBe(unreadable);
|
||||
} finally {
|
||||
db.prepare('UPDATE app_settings SET value = ? WHERE key = ?').run(ciphertext, 'web_push_vapid_private_key');
|
||||
await setAppSetting(orm, 'web_push_vapid_private_key', ciphertext);
|
||||
}
|
||||
const back = await request(server).get('/api/notifications/push/public-key').set('Cookie', sessionCookie(1));
|
||||
expect(back.body).toEqual({ publicKey });
|
||||
|
||||
Reference in New Issue
Block a user