mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
refactor(db): TrekRepository base — every repository path validates the request context and reads bypass the identity map by default
This commit is contained in:
@@ -207,6 +207,31 @@ describe('CronRegistrarService', () => {
|
||||
expect(caught).toBeUndefined();
|
||||
expect(Array.isArray(result)).toBe(true);
|
||||
});
|
||||
|
||||
// Plan 3b interlude B mutation proof (b): the SAME wrapper, but through a
|
||||
// real TrekRepository-backed call rather than a bare `em.find` — proving
|
||||
// the wrapper is load-bearing for the base class's write paths too
|
||||
// (`count`/`nativeUpdate`/`nativeDelete`/`insert`/`upsert`), which never
|
||||
// validated their own context before this class existed
|
||||
// (task-1-review.md F7 INFO) and so would NOT have failed this way on
|
||||
// the pre-interlude-B tree.
|
||||
it('CRONREG-011B — with MikroORM injected, a TrekRepository-backed count() inside onTick succeeds; dropping the wrapper (mutation, not committed) makes it throw cannotUseGlobalContext', async () => {
|
||||
const registry = new SchedulerRegistry();
|
||||
const registrar = new CronRegistrarService(registry, { isTest: () => false } as RuntimeEnvService, t.orm);
|
||||
const users = t.repo(Users);
|
||||
let result: unknown;
|
||||
let caught: unknown;
|
||||
registrar.register('job', '0 2 * * *', async () => {
|
||||
try {
|
||||
result = await users.count({});
|
||||
} catch (e) {
|
||||
caught = e;
|
||||
}
|
||||
});
|
||||
await h.jobs[0].onTick();
|
||||
expect(caught).toBeUndefined();
|
||||
expect(typeof result).toBe('number');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runOnBoot (task-6-fix-brief.md item 7 — the boot-sweep choke point)', () => {
|
||||
|
||||
@@ -75,13 +75,20 @@ import type { TripFile, User } from '../../../src/types';
|
||||
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
||||
import { EphemeralTokenService } from '../../../src/nest/auth/ephemeral-token.service';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
|
||||
const storageDelete = vi.fn();
|
||||
const storageStub = { delete: storageDelete } as unknown as import('../../../src/nest/storage/storage.service').StorageService;
|
||||
// EntityManager stub (Plan 3b Task 1 RULING): verifyJwtAndLoadUser is
|
||||
// fully mocked above, so `em.getRepository` never needs to return
|
||||
// anything meaningful; it just has to not throw when the service calls it.
|
||||
const emStub = { getRepository: () => ({}) } as unknown as EntityManager;
|
||||
// `getRepository` is a spy, not a plain arrow (task-1-rereview.md F-R4): a
|
||||
// fixed `() => ({})` pinned only the RETURN value, not which entity class
|
||||
// `files.service.ts` asks for — the assertion below would hold even if the
|
||||
// production code asked for `Trips`. The spy lets FILE-SVC-032/033 pin
|
||||
// `Users` specifically.
|
||||
const getRepository = vi.fn(() => ({}));
|
||||
const emStub = { getRepository } as unknown as EntityManager;
|
||||
const svc = new FilesService(new DatabaseService(testDb), permissionsStub, new RealtimeService(), new EphemeralTokenService(), storageStub, emStub);
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -564,13 +571,18 @@ describe('authenticateDownload', () => {
|
||||
// always returns a fresh `{}` (task-1-review.md F6) — deep-equal, not a
|
||||
// reference match, so this pins WHICH repository (an empty object, this
|
||||
// suite's stand-in for UsersRepository), not merely "a repository".
|
||||
// `getRepository` itself is a spy (task-1-rereview.md F-R4), so this ALSO
|
||||
// pins which entity class was asked for: the deep-equal alone would still
|
||||
// hold if the production code called `em.getRepository(Trips)`.
|
||||
expect(verifyJwtAndLoadUser).toHaveBeenCalledWith('cookie-jwt', {});
|
||||
expect(getRepository).toHaveBeenCalledWith(Users);
|
||||
});
|
||||
|
||||
it('FILE-SVC-033: a bearer token is used when no cookie is present; invalid JWTs 401', async () => {
|
||||
verifyJwtAndLoadUser.mockReturnValue({ id: 7 });
|
||||
expect(await svc.authenticateDownload(req({ bearer: 'bearer-jwt' }))).toEqual({ userId: 7 });
|
||||
expect(verifyJwtAndLoadUser).toHaveBeenCalledWith('bearer-jwt', {}); // see FILE-SVC-032's comment
|
||||
expect(getRepository).toHaveBeenCalledWith(Users);
|
||||
|
||||
verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
expect(await svc.authenticateDownload(req({ bearer: 'stale' }))).toEqual({ error: 'Invalid or expired token', status: 401 });
|
||||
|
||||
@@ -726,10 +726,19 @@ describe('renamePasskey', () => {
|
||||
expect(await svc.renamePasskey(user.id, '999999', 'Ghost')).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
});
|
||||
|
||||
it('PASSKEY-SVC-042: 404s (not a 500) on a non-numeric or prefixed-numeric id — Plan 3b Task 3 review, F1', async () => {
|
||||
it('PASSKEY-SVC-042: 404s (not a 500) on a non-numeric id — Plan 3b Task 3 review, F1', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await svc.renamePasskey(user.id, 'abc', 'Ghost')).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
expect(await svc.renamePasskey(user.id, '1abc', 'Ghost')).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
});
|
||||
|
||||
// task-3-rereview.md R1: the legacy `renamePasskey` bound `Number(id)`
|
||||
// (unlike the invite route's raw-string bind), so `Number('0x10') === 16`
|
||||
// — the legacy statement would have ACTED on credential 16, not 404'd.
|
||||
// This pins the deliberate `toRowId` narrowing the fix round chose
|
||||
// (accepted deviation, not parity), not "the legacy 404".
|
||||
it('PASSKEY-SVC-042B: refuses a prefixed-numeric id (0x10) via the toRowId narrowing', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await svc.renamePasskey(user.id, '0x10', 'Ghost')).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
});
|
||||
});
|
||||
@@ -759,6 +768,13 @@ describe('deletePasskey', () => {
|
||||
it('PASSKEY-SVC-043: 404s (not a 500) on a non-numeric id once the password check passes — Plan 3b Task 3 review, F1', async () => {
|
||||
const { user, password } = createUser(testDb);
|
||||
expect(await svc.deletePasskey(user.id, 'abc', password)).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
});
|
||||
|
||||
// task-3-rereview.md R1: same distinction as PASSKEY-SVC-042B —
|
||||
// `deletePasskey`'s legacy bind was `Number(id)`, so `'0x10'` would have
|
||||
// acted on credential 16, not 404'd. This pins the `toRowId` narrowing.
|
||||
it('PASSKEY-SVC-043B: refuses a prefixed-numeric id (0x10) via the toRowId narrowing, once the password check passes', async () => {
|
||||
const { user, password } = createUser(testDb);
|
||||
expect(await svc.deletePasskey(user.id, '0x10', password)).toEqual({ error: 'Passkey not found', status: 404 });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user