Skip to content

Commit bae425c

Browse files
committed
test(dogfood): an export through the real door, gate and ledger leaves one row and no cell value
Composes the #30955 halves on a real boot (SecurityPlugin + AuditPlugin): an ordinary export, the ledger's own export, a truncated export, a filtered and a searched export each leave one run-level row; the seeded value is in the file and in no row; EXPORT_NOT_PERMITTED leaves none. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cxfxbKV82MKKQzizpP
1 parent b2acde5 commit bae425c

1 file changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// [#30955] Every data export the REST door streams leaves ONE run-level row in
4+
// the compliance ledger — on a real boot, both halves composed: the door in
5+
// `@objectstack/rest`'s `data-action-routes.ts` hands an export EVENT to the
6+
// `audit` slot, and plugin-audit's `data-export-audit.ts` turns it into the
7+
// row. Each half is pinned on its own (`export-integration.test.ts`,
8+
// `data-export-audit.test.ts`); this file is the one place they meet with the
9+
// real security gate, the real driver and the real ledger.
10+
//
11+
// ## The composition
12+
//
13+
// `bootStack` with the real `SecurityPlugin` and `AuditPlugin`. One synthetic
14+
// object; one row of it carries a distinctive cell value. Two users: an
15+
// EXPORTER whose set grants read + export on the object, and a READER whose
16+
// set grants read only, so the export permission gate refuses it. The
17+
// exporter's set also grants read + export on the ledger, for its own export.
18+
//
19+
// ## What is asserted
20+
//
21+
// - An ordinary export writes one row: `action: 'export'`, the object, no
22+
// record id, the exporter, the format and the row count.
23+
// - An export of `sys_audit_log` itself writes one row about the ledger.
24+
// - A truncated export (`limit` below the matching rows) records the truncation.
25+
// - The row never carries an exported cell value: the value is IN the file
26+
// (filtered on, and searched for), and in no export row.
27+
// - A refused export (`EXPORT_NOT_PERMITTED`) writes no row.
28+
//
29+
// The row is written after the file is finished, so a case reads the ledger
30+
// until the row lands (bounded) instead of right after the response.
31+
//
32+
// Fixtures are synthetic. ⚠️ No test title states a value.
33+
// `@objectstack/rest` and `@objectstack/plugin-audit` resolve through their
34+
// BUILT output here, so a verdict on a change to either is a verdict on its
35+
// last build.
36+
37+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
38+
import { bootStack, type VerifyStack } from '@objectstack/verify';
39+
import { AuditPlugin } from '@objectstack/plugin-audit';
40+
import { defineStack } from '@objectstack/spec';
41+
import { ObjectSchema, Field } from '@objectstack/spec/data';
42+
import { PermissionSetSchema } from '@objectstack/spec/security';
43+
import { SecurityPlugin, securityDefaultPermissionSets } from '@objectstack/plugin-security';
44+
import { catalogPermissionSet } from './catalog-permission-set.js';
45+
46+
const OBJ = 'dex_note';
47+
const LEDGER = 'sys_audit_log';
48+
const SYS = { context: { isSystem: true } } as const;
49+
50+
/** A cell value no field name, operator or code in this file spells. */
51+
const SECRET = 'Dx9-export-cell-value-7316';
52+
53+
const Note = ObjectSchema.create({
54+
name: OBJ,
55+
label: 'DEX Note',
56+
pluralLabel: 'DEX Notes',
57+
sharingModel: 'public_read_write',
58+
fields: {
59+
name: Field.text({ label: 'Name', required: true, searchable: true }),
60+
body: Field.text({ label: 'Body' }),
61+
},
62+
});
63+
64+
const fixtureStack = defineStack({
65+
manifest: {
66+
id: 'com.dogfood.data-export-audit-row',
67+
namespace: 'dex',
68+
version: '0.0.0',
69+
type: 'app',
70+
name: 'Data Export Audit Row Fixture',
71+
description: 'One object exported through the REST door, recorded on the compliance ledger.',
72+
},
73+
objects: [Note],
74+
});
75+
76+
/** Read only: the export permission gate refuses this user. */
77+
const readerSet = PermissionSetSchema.parse({
78+
name: 'dex_reader',
79+
label: 'DEX reader',
80+
objects: { [OBJ]: { allowRead: true } },
81+
});
82+
/** Read and export on the fixture object, and on the ledger. */
83+
const exporterSet = PermissionSetSchema.parse({
84+
name: 'dex_exporter',
85+
label: 'DEX exporter',
86+
objects: {
87+
[OBJ]: { allowRead: true, allowExport: true },
88+
[LEDGER]: { allowRead: true, allowExport: true },
89+
},
90+
});
91+
92+
type Row = Record<string, any>;
93+
const metadataOf = (row: Row): Record<string, any> => JSON.parse(String(row.metadata));
94+
const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
95+
96+
describe('[#30955] every export the REST door streams leaves one run-level ledger row', () => {
97+
let stack: VerifyStack;
98+
let ql: any;
99+
const token: Record<'exporter' | 'reader', string> = { exporter: '', reader: '' };
100+
let exporterId = '';
101+
102+
const exportRows = async (): Promise<Row[]> =>
103+
ql.find(LEDGER, { where: { action: 'export' }, ...SYS });
104+
105+
/**
106+
* Run one export and return the export rows it left. The row lands after the
107+
* file is finished, so the ledger is read until `expect` rows are new (or
108+
* the bound passes), then once more after a settle, so a second row would
109+
* be counted too.
110+
*/
111+
const exportAndRead = async (who: keyof typeof token, path: string, expectNew: number) => {
112+
const before = new Set((await exportRows()).map((r) => String(r.id)));
113+
const res = await stack.apiAs(token[who], 'GET', path);
114+
const text = await res.text();
115+
const fresh = async () => (await exportRows()).filter((r) => !before.has(String(r.id)));
116+
for (let i = 0; i < 100 && (await fresh()).length < expectNew; i++) await sleep(20);
117+
await sleep(150);
118+
return { status: res.status, text, rows: await fresh() };
119+
};
120+
121+
beforeAll(async () => {
122+
stack = await bootStack(fixtureStack as unknown as Parameters<typeof bootStack>[0], {
123+
security: new SecurityPlugin({
124+
defaultPermissionSets: [...securityDefaultPermissionSets, readerSet, exporterSet],
125+
fallbackPermissionSet: readerSet.name,
126+
}),
127+
extraPlugins: [new AuditPlugin()],
128+
});
129+
ql = await stack.kernel.getServiceAsync('objectql');
130+
token.reader = await stack.signUp('dex-reader@verify.test');
131+
token.exporter = await stack.signUp('dex-exporter@verify.test');
132+
exporterId = String((await ql.findOne('sys_user', { where: { email: 'dex-exporter@verify.test' }, ...SYS }))?.id ?? '');
133+
expect(exporterId).not.toBe('');
134+
expect(await catalogPermissionSet(ql, exporterSet.name), 'fixture set served by the security catalog').toBeTruthy();
135+
await ql.insert('sys_user_permission_set', { user_id: exporterId, permission_set: exporterSet.name }, SYS);
136+
137+
await ql.insert(OBJ, { name: 'Note one', body: 'plain' }, SYS);
138+
await ql.insert(OBJ, { name: 'Note two', body: 'plain' }, SYS);
139+
await ql.insert(OBJ, { name: SECRET, body: 'carries the value' }, SYS);
140+
}, 180_000);
141+
142+
afterAll(async () => {
143+
if (stack) await stack.stop();
144+
});
145+
146+
it('an export of an ordinary object writes ONE row naming the object, the exporter, the format and the row count', async () => {
147+
const { status, text, rows } = await exportAndRead('exporter', `/data/${OBJ}/export?format=json`, 1);
148+
expect(status, text).toBe(200);
149+
expect(JSON.parse(text)).toHaveLength(3);
150+
expect(rows).toHaveLength(1);
151+
expect(rows[0]).toMatchObject({ action: 'export', object_name: OBJ, user_id: exporterId });
152+
expect(rows[0].record_id ?? null).toBeNull();
153+
expect(metadataOf(rows[0])).toMatchObject({ format: 'json', row_count: 3, truncated: false, outcome: 'succeeded' });
154+
});
155+
156+
it('a csv export writes ONE row with its own format', async () => {
157+
const { status, rows } = await exportAndRead('exporter', `/data/${OBJ}/export?format=csv`, 1);
158+
expect(status).toBe(200);
159+
expect(rows).toHaveLength(1);
160+
expect(metadataOf(rows[0])).toMatchObject({ format: 'csv', row_count: 3 });
161+
});
162+
163+
it('an export of the ledger itself writes ONE row about the ledger', async () => {
164+
const { status, text, rows } = await exportAndRead('exporter', `/data/${LEDGER}/export?format=json`, 1);
165+
expect(status, text).toBe(200);
166+
// The file is the ledger, and it already holds the earlier cases' export
167+
// rows (the json export renders the action by its option label).
168+
const actions = (JSON.parse(text) as Row[]).map((r) => String(r.action).toLowerCase());
169+
expect(actions.filter((a) => a === 'export').length).toBeGreaterThanOrEqual(2);
170+
expect(rows).toHaveLength(1);
171+
expect(rows[0]).toMatchObject({ action: 'export', object_name: LEDGER });
172+
expect(rows[0].record_id ?? null).toBeNull();
173+
expect(metadataOf(rows[0])).toMatchObject({ format: 'json', outcome: 'succeeded' });
174+
});
175+
176+
it('a truncated export records the truncation', async () => {
177+
const { status, text, rows } = await exportAndRead('exporter', `/data/${OBJ}/export?format=json&limit=1`, 1);
178+
expect(status).toBe(200);
179+
expect(JSON.parse(text)).toHaveLength(1);
180+
expect(rows).toHaveLength(1);
181+
expect(metadataOf(rows[0])).toMatchObject({ row_count: 1, limit: 1, truncated: true });
182+
});
183+
184+
it('the row never carries an exported cell value: filtered on, the value is in the file and in no row', async () => {
185+
const filter = encodeURIComponent(JSON.stringify({ name: SECRET }));
186+
const { status, text, rows } = await exportAndRead('exporter', `/data/${OBJ}/export?format=json&filter=${filter}`, 1);
187+
expect(status, text).toBe(200);
188+
expect(text, 'armed: the value was exported').toContain(SECRET);
189+
expect(rows).toHaveLength(1);
190+
expect(JSON.stringify(rows[0])).not.toContain(SECRET);
191+
expect(metadataOf(rows[0])).toMatchObject({ row_count: 1, filter: [{ field: 'name', op: '$eq' }], search: false });
192+
});
193+
194+
it('the row never carries the search term: searched for, the value is in the file and the row says only that a search ran', async () => {
195+
const { status, text, rows } = await exportAndRead('exporter', `/data/${OBJ}/export?format=json&search=${encodeURIComponent(SECRET)}`, 1);
196+
expect(status, text).toBe(200);
197+
expect(text, 'armed: the value was exported').toContain(SECRET);
198+
expect(rows).toHaveLength(1);
199+
expect(JSON.stringify(rows[0])).not.toContain(SECRET);
200+
expect(metadataOf(rows[0])).toMatchObject({ row_count: 1, search: true });
201+
});
202+
203+
it('a refused export (no export permission) writes no row', async () => {
204+
const { status, text, rows } = await exportAndRead('reader', `/data/${OBJ}/export?format=json`, 0);
205+
expect(status, text).toBe(403);
206+
expect(text).toContain('EXPORT_NOT_PERMITTED');
207+
expect(rows).toEqual([]);
208+
// Control: the same door, the same object, an exporter — one row.
209+
const control = await exportAndRead('exporter', `/data/${OBJ}/export?format=json`, 1);
210+
expect(control.rows).toHaveLength(1);
211+
});
212+
213+
it('no export row anywhere in the ledger carries the cell value', async () => {
214+
expect(JSON.stringify(await exportRows())).not.toContain(SECRET);
215+
});
216+
});

0 commit comments

Comments
 (0)