mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
This commit is contained in:
@@ -10,16 +10,25 @@
|
||||
* converted code, without keeping a second, unused copy of the production function
|
||||
* around.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import crypto from 'node:crypto';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
import type { PluginCapabilityAuditRepository } from '../../../src/db/repositories/PluginCapabilityAudit.repository';
|
||||
import {
|
||||
appendAudit,
|
||||
readAudit,
|
||||
readAuditForUser,
|
||||
auditResource,
|
||||
isAuditable,
|
||||
pruneAudit,
|
||||
verifyChain,
|
||||
} from '../../../src/nest/plugins/host/plugin-audit';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { countRows, deleteRows, findRow, findRows, insertRow, insertRows } from '../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import type { PluginCapabilityAuditRepository } from '../../../src/db/repositories/PluginCapabilityAudit.repository';
|
||||
import { appendAudit, readAudit, readAuditForUser, auditResource, isAuditable, pruneAudit, verifyChain } from '../../../src/nest/plugins/host/plugin-audit';
|
||||
import { countRows, deleteRows, findRow, findRows, insertRow, insertRows } from '../../helpers/factories/rows';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
|
||||
import crypto from 'node:crypto';
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
@@ -29,8 +38,14 @@ beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
audit = t.repo(PluginCapabilityAudit);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
t.clear();
|
||||
});
|
||||
afterAll(async () => {
|
||||
await t.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
interface LegacyEntry {
|
||||
pluginId: string;
|
||||
@@ -44,10 +59,15 @@ interface LegacyEntry {
|
||||
function legacyAppendAudit(prevHash: string, e: LegacyEntry): { ts: string; hash: string } {
|
||||
const ts = new Date().toISOString();
|
||||
const row = JSON.stringify([e.pluginId, e.actingUserId ?? null, e.method, e.resource ?? null, e.code, ts]);
|
||||
const hash = crypto.createHash('sha256').update(prevHash + row).digest('hex');
|
||||
const hash = crypto
|
||||
.createHash('sha256')
|
||||
.update(prevHash + row)
|
||||
.digest('hex');
|
||||
testDb
|
||||
// test-sql-allow: the pre-conversion appendAudit wrote through this exact raw statement; reproducing it verbatim is the legacy side of the parity check.
|
||||
.prepare('INSERT INTO plugin_capability_audit (plugin_id, acting_user_id, method, resource, code, ts, prev_hash, hash) VALUES (?,?,?,?,?,?,?,?)')
|
||||
.prepare(
|
||||
'INSERT INTO plugin_capability_audit (plugin_id, acting_user_id, method, resource, code, ts, prev_hash, hash) VALUES (?,?,?,?,?,?,?,?)',
|
||||
)
|
||||
.run(e.pluginId, e.actingUserId ?? null, e.method, e.resource ?? null, e.code, ts, prevHash || null, hash);
|
||||
return { ts, hash };
|
||||
}
|
||||
@@ -94,8 +114,20 @@ describe('auditResource + isAuditable', () => {
|
||||
|
||||
describe('appendAudit hash chain (converted, through PluginCapabilityAuditRepository)', () => {
|
||||
it('chains each entry off the previous hash (per plugin)', async () => {
|
||||
await appendAudit(audit, { pluginId: 'p', actingUserId: 42, method: 'trips.getById', resource: 'trip:1', code: 'ok' });
|
||||
await appendAudit(audit, { pluginId: 'p', actingUserId: 42, method: 'trips.getById', resource: 'trip:2', code: 'ok' });
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 42,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:1',
|
||||
code: 'ok',
|
||||
});
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 42,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:2',
|
||||
code: 'ok',
|
||||
});
|
||||
const rows = await findRows(t, PluginCapabilityAudit, {}, { id: 'asc' });
|
||||
expect(rows).toHaveLength(2);
|
||||
expect(rows[0].prev_hash).toBeNull();
|
||||
@@ -111,40 +143,98 @@ describe('appendAudit hash chain (converted, through PluginCapabilityAuditReposi
|
||||
});
|
||||
|
||||
it('records denials too (code is the error code)', async () => {
|
||||
await appendAudit(audit, { pluginId: 'p', actingUserId: 99, method: 'trips.getById', resource: 'trip:1', code: 'RESOURCE_FORBIDDEN' });
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 99,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:1',
|
||||
code: 'RESOURCE_FORBIDDEN',
|
||||
});
|
||||
const row = (await readAudit(audit, 'p'))[0];
|
||||
expect(row.code).toBe('RESOURCE_FORBIDDEN');
|
||||
});
|
||||
|
||||
it("readAuditForUser returns one user's actions across ALL plugins, newest first, with the plugin name", async () => {
|
||||
await insertRows(t, Plugins, [{ id: 'koffi', name: 'Koffi' }, { id: 'flight', name: 'Flight Tracker' }]);
|
||||
await appendAudit(audit, { pluginId: 'koffi', actingUserId: 42, method: 'trips.getById', resource: 'trip:1', code: 'ok' });
|
||||
await appendAudit(audit, { pluginId: 'flight', actingUserId: 42, method: 'reservations.create', resource: 'trip:1', code: 'ok' });
|
||||
await appendAudit(audit, { pluginId: 'koffi', actingUserId: 99, method: 'trips.getById', resource: 'trip:2', code: 'ok' }); // another user
|
||||
await insertRows(t, Plugins, [
|
||||
{ id: 'koffi', name: 'Koffi' },
|
||||
{ id: 'flight', name: 'Flight Tracker' },
|
||||
]);
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'koffi',
|
||||
actingUserId: 42,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:1',
|
||||
code: 'ok',
|
||||
});
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'flight',
|
||||
actingUserId: 42,
|
||||
method: 'reservations.create',
|
||||
resource: 'trip:1',
|
||||
code: 'ok',
|
||||
});
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'koffi',
|
||||
actingUserId: 99,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:2',
|
||||
code: 'ok',
|
||||
}); // another user
|
||||
const mine = await readAuditForUser(audit, 42);
|
||||
expect(mine).toHaveLength(2); // only user 42's rows, across both plugins
|
||||
expect(mine[0]).toMatchObject({ plugin_id: 'flight', plugin_name: 'Flight Tracker', method: 'reservations.create' }); // newest first
|
||||
expect(mine[0]).toMatchObject({
|
||||
plugin_id: 'flight',
|
||||
plugin_name: 'Flight Tracker',
|
||||
method: 'reservations.create',
|
||||
}); // newest first
|
||||
expect(mine[1]).toMatchObject({ plugin_id: 'koffi', plugin_name: 'Koffi' });
|
||||
expect(mine.some((r) => r.plugin_id === 'koffi' && r.method === 'trips.getById' && r.resource === 'trip:2')).toBe(false); // never another user's
|
||||
expect(mine.some((r) => r.plugin_id === 'koffi' && r.method === 'trips.getById' && r.resource === 'trip:2')).toBe(
|
||||
false,
|
||||
); // never another user's
|
||||
});
|
||||
|
||||
it('readAudit returns newest first with the projected fields only', async () => {
|
||||
await appendAudit(audit, { pluginId: 'p', actingUserId: 42, method: 'trips.getById', resource: 'trip:1', code: 'ok' });
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 42,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:1',
|
||||
code: 'ok',
|
||||
});
|
||||
const rows = await readAudit(audit, 'p');
|
||||
expect(rows[0]).toHaveProperty('method', 'trips.getById');
|
||||
expect(rows[0]).not.toHaveProperty('prev_hash'); // internal chain fields not exposed
|
||||
});
|
||||
|
||||
it('pruneAudit keeps only the newest N rows per plugin, leaving the retained window chain-consistent', async () => {
|
||||
for (let i = 0; i < 50; i++) await appendAudit(audit, { pluginId: 'p', actingUserId: 1, method: 'trips.getById', resource: `trip:${i}`, code: 'ok' });
|
||||
await appendAudit(audit, { pluginId: 'other', actingUserId: 1, method: 'trips.getById', resource: 'trip:x', code: 'ok' });
|
||||
for (let i = 0; i < 50; i++)
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 1,
|
||||
method: 'trips.getById',
|
||||
resource: `trip:${i}`,
|
||||
code: 'ok',
|
||||
});
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'other',
|
||||
actingUserId: 1,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:x',
|
||||
code: 'ok',
|
||||
});
|
||||
await pruneAudit(audit, 'p', 10);
|
||||
const rows = await findRows(t, PluginCapabilityAudit, { plugin_id: 'p' }, { id: 'asc' });
|
||||
expect(rows).toHaveLength(10);
|
||||
expect(rows[rows.length - 1].resource).toBe('trip:49'); // newest kept
|
||||
// each retained row is still self-consistent: hash === sha256(prev_hash + row-content)
|
||||
// (proven by re-appending on top — the chain continues from the surviving tip)
|
||||
await appendAudit(audit, { pluginId: 'p', actingUserId: 1, method: 'trips.getById', resource: 'trip:new', code: 'ok' });
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'p',
|
||||
actingUserId: 1,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:new',
|
||||
code: 'ok',
|
||||
});
|
||||
expect(await countRows(t, PluginCapabilityAudit, { plugin_id: 'p' })).toBe(11);
|
||||
// pruning one plugin never touches another's rows
|
||||
expect(await countRows(t, PluginCapabilityAudit, { plugin_id: 'other' })).toBe(1);
|
||||
@@ -161,7 +251,13 @@ describe('R-hash-chain: concurrency (Plan 3j Task 7 fix wave, must-land 1)', ()
|
||||
// earlier, in-process 10-way version of this same test.
|
||||
await Promise.all(
|
||||
Array.from({ length: 10 }, (_, i) =>
|
||||
appendAudit(audit, { pluginId: 'burst', actingUserId: 1, method: 'trips.getById', resource: `trip:${i}`, code: 'ok' }),
|
||||
appendAudit(audit, {
|
||||
pluginId: 'burst',
|
||||
actingUserId: 1,
|
||||
method: 'trips.getById',
|
||||
resource: `trip:${i}`,
|
||||
code: 'ok',
|
||||
}),
|
||||
),
|
||||
);
|
||||
const rows = await chainRows('burst');
|
||||
@@ -181,7 +277,13 @@ describe('R-hash-chain: replay, extension, mutation', () => {
|
||||
// 1. Write three rows the way the PRE-CONVERSION code did: raw SQL, no repository.
|
||||
let prev = '';
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const { hash } = legacyAppendAudit(prev, { pluginId: 'legacy-p', actingUserId: 7, method: 'trips.getById', resource: `trip:${i}`, code: 'ok' });
|
||||
const { hash } = legacyAppendAudit(prev, {
|
||||
pluginId: 'legacy-p',
|
||||
actingUserId: 7,
|
||||
method: 'trips.getById',
|
||||
resource: `trip:${i}`,
|
||||
code: 'ok',
|
||||
});
|
||||
prev = hash;
|
||||
}
|
||||
// 2. REPLAY: the converted verification logic (verifyChain) accepts the legacy-written rows.
|
||||
@@ -189,8 +291,20 @@ describe('R-hash-chain: replay, extension, mutation', () => {
|
||||
|
||||
// 3. EXTENSION: append two more rows through the CONVERTED appendAudit, on top of the
|
||||
// legacy tip — the repository reads the legacy row's hash as its own prev_hash.
|
||||
await appendAudit(audit, { pluginId: 'legacy-p', actingUserId: 7, method: 'trips.getById', resource: 'trip:3', code: 'ok' });
|
||||
await appendAudit(audit, { pluginId: 'legacy-p', actingUserId: 7, method: 'trips.getById', resource: 'trip:4', code: 'ok' });
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'legacy-p',
|
||||
actingUserId: 7,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:3',
|
||||
code: 'ok',
|
||||
});
|
||||
await appendAudit(audit, {
|
||||
pluginId: 'legacy-p',
|
||||
actingUserId: 7,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:4',
|
||||
code: 'ok',
|
||||
});
|
||||
|
||||
const rows = await chainRows('legacy-p');
|
||||
expect(rows).toHaveLength(5);
|
||||
@@ -210,9 +324,19 @@ describe('R-hash-chain: replay, extension, mutation', () => {
|
||||
const code = 'ok';
|
||||
const ts = new Date().toISOString();
|
||||
const mutatedRow = JSON.stringify([pluginId, actingUserId, method, code, ts]); // resource OMITTED
|
||||
const mutatedHash = crypto.createHash('sha256').update('' + mutatedRow).digest('hex');
|
||||
const mutatedHash = crypto
|
||||
.createHash('sha256')
|
||||
.update('' + mutatedRow)
|
||||
.digest('hex');
|
||||
await insertRow(t, PluginCapabilityAudit, {
|
||||
plugin_id: pluginId, acting_user_id: actingUserId, method, resource, code, ts, prev_hash: null, hash: mutatedHash,
|
||||
plugin_id: pluginId,
|
||||
acting_user_id: actingUserId,
|
||||
method,
|
||||
resource,
|
||||
code,
|
||||
ts,
|
||||
prev_hash: null,
|
||||
hash: mutatedHash,
|
||||
});
|
||||
|
||||
expect(verifyChain(await chainRows(pluginId))).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user