Skip to content

Commit ec6f33e

Browse files
committed
test(rest): pin the batch door's per-op exposure answer to the data door's
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwgfUuNAvcUg3JzBiR
1 parent d4064cc commit ec6f33e

1 file changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#27899] The cross-object batch door (`POST {basePath}/batch`) judges each
5+
* op's API exposure on the object the data door would serve for the op's
6+
* name, and refuses it with the data door's own answer.
7+
*
8+
* The `/data/:object` exposure gate reads its object by name, through the read
9+
* that resolves a legacy `ns__short` object from its short name exactly as the
10+
* data door's `registry.getObject` does (#27729). The batch door's per-op gate
11+
* keyed the object LIST by each item's declared name instead, and the list
12+
* carries no such resolution: an op naming such an object by its short name
13+
* found "no object" there, passed as unknown, and was then written by the
14+
* engine, which does resolve the name. It now asks the `/data` gate's read, so
15+
* every data door gives one answer about whether an object is exposed.
16+
*
17+
* A REAL `ObjectQL` engine + `ObjectStackProtocolImplementation` on a REAL
18+
* sqlite `:memory:` driver, with `objectQLProvider` wired so the batch door is
19+
* reachable rather than answering 501; nothing below the routes is a double.
20+
*/
21+
22+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
23+
import { ObjectQL } from '@objectstack/objectql';
24+
import { SqlDriver } from '@objectstack/driver-sql';
25+
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
26+
import { RestServer } from './rest-server';
27+
28+
const liveEngines: ObjectQL[] = [];
29+
afterEach(async () => {
30+
while (liveEngines.length) {
31+
try { await liveEngines.pop()?.destroy(); } catch { /* noop */ }
32+
}
33+
});
34+
35+
const fields = {
36+
id: { name: 'id', type: 'text' as const, primaryKey: true, label: 'ID' },
37+
title: { name: 'title', type: 'text' as const, label: 'Title' },
38+
};
39+
40+
/** Legacy `ns__short` objects of one package, addressed below by short name. */
41+
const LEGACY_PACKAGE = 'app.ex_legacy';
42+
/** Exposed: no `enable` block. */
43+
const WIDGET = { name: 'exl__widget', label: 'Widget', systemFields: false, fields };
44+
/** Hidden from the API. */
45+
const VAULT = { name: 'exl__vault', label: 'Vault', systemFields: false, enable: { apiEnabled: false }, fields };
46+
/** Exposed for reads only. */
47+
const LEDGER = { name: 'exl__ledger', label: 'Ledger', systemFields: false, enable: { apiMethods: ['get', 'list'] }, fields };
48+
/** A plain-named object hidden from the API: the gate's answer before this change. */
49+
const SECRET = { name: 'ex_secret', label: 'Secret', systemFields: false, enable: { apiEnabled: false }, fields };
50+
51+
function makeRes() {
52+
const res: any = {
53+
write: () => true, end: () => {},
54+
header: () => res,
55+
status: (code: number) => { res._status = code; return res; },
56+
json: (body: any) => { res._json = body; return res; },
57+
};
58+
return res;
59+
}
60+
61+
async function boot() {
62+
const engine = new ObjectQL();
63+
liveEngines.push(engine);
64+
engine.registerDriver(new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), true);
65+
await engine.init();
66+
engine.registry.installPackage({ id: LEGACY_PACKAGE, name: LEGACY_PACKAGE, version: '1.0.0' } as any, 'code');
67+
for (const o of [WIDGET, VAULT, LEDGER]) engine.registry.registerObject(o as any, LEGACY_PACKAGE);
68+
engine.registry.registerObject(SECRET as any);
69+
await engine.syncSchemas();
70+
71+
const protocol = new ObjectStackProtocolImplementation(engine as any);
72+
const noop = () => {};
73+
const server = { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} };
74+
const rest = new RestServer(
75+
server as any, protocol as any, { api: { requireAuth: false } } as any,
76+
undefined, undefined, undefined, undefined, // kernelManager, envRegistry, defaultEnvIdProvider, authServiceProvider
77+
(async () => engine) as any, // objectQLProvider (positional arg #8)
78+
);
79+
(rest as any).resolveExecCtx = async () => ({ userId: 'test-user' });
80+
rest.registerRoutes();
81+
const routes = rest.getRoutes();
82+
const route = (path: string): any => {
83+
const found = routes.find((r: any) => r.method === 'POST' && r.path === path);
84+
if (!found) throw new Error(`route POST ${path} is not registered`);
85+
return found;
86+
};
87+
const doors = {
88+
batch: route('/api/v1/batch'),
89+
data: route('/api/v1/data/:object'),
90+
createMany: route('/api/v1/data/:object/createMany'),
91+
};
92+
const transaction = vi.spyOn(engine, 'transaction');
93+
const call = async (door: keyof typeof doors, object: string | undefined, body: unknown) => {
94+
const res = makeRes();
95+
await doors[door].handler({ method: 'POST', params: object ? { object } : {}, query: {}, headers: {}, body } as any, res);
96+
return { status: res._status ?? 200, body: res._json };
97+
};
98+
return {
99+
engine,
100+
transaction,
101+
batch: (operations: unknown[]) => call('batch', undefined, { operations }),
102+
data: (object: string, record: Record<string, unknown>) => call('data', object, record),
103+
createMany: (object: string, records: Array<Record<string, unknown>>) => call('createMany', object, records),
104+
/** Rows of a table, counted at the engine. */
105+
async count(table: string) {
106+
const rows = await engine.find(table, { where: {} });
107+
return Array.isArray(rows) ? rows.length : 0;
108+
},
109+
};
110+
}
111+
112+
describe('[#27899] the batch door judges each op on the object the data door serves for its name', () => {
113+
let h: Awaited<ReturnType<typeof boot>>;
114+
beforeEach(async () => { h = await boot(); });
115+
116+
it('premise: the data door resolves a legacy short name and refuses the hidden object behind it', async () => {
117+
expect((h.engine.registry.getObject('vault') as any)?.name).toBe('exl__vault');
118+
const door = await h.data('vault', { id: 'v0', title: 'x' });
119+
expect(door.status).toBe(404);
120+
expect(door.body?.code).toBe('OBJECT_API_DISABLED');
121+
});
122+
123+
for (const action of ['create', 'update', 'delete'] as const) {
124+
it(`a ${action} op on a hidden object is refused with the data door's answer, before the transaction opens`, async () => {
125+
const door = await h.data('vault', { id: 'v0', title: 'x' });
126+
const op = action === 'create'
127+
? { object: 'vault', action, data: { id: 'v1', title: 'x' } }
128+
: { object: 'vault', action, id: 'v1', ...(action === 'update' ? { data: { title: 'y' } } : {}) };
129+
const batch = await h.batch([op]);
130+
expect(batch).toEqual({ status: door.status, body: door.body });
131+
expect(h.transaction).not.toHaveBeenCalled();
132+
expect(await h.count('exl__vault')).toBe(0);
133+
});
134+
}
135+
136+
it('an op the object does not expose is refused with the bulk data door\'s 405, status and body', async () => {
137+
const bulk = await h.createMany('ledger', [{ id: 'l0', title: 'x' }]);
138+
expect(bulk.status).toBe(405);
139+
expect(bulk.body?.code).toBe('OBJECT_API_METHOD_NOT_ALLOWED');
140+
const batch = await h.batch([{ object: 'ledger', action: 'create', data: { id: 'l1', title: 'x' } }]);
141+
expect(batch).toEqual(bulk);
142+
expect(h.transaction).not.toHaveBeenCalled();
143+
expect(await h.count('exl__ledger')).toBe(0);
144+
});
145+
146+
it('one refused op refuses the whole batch: an exposed op before it is never written', async () => {
147+
const batch = await h.batch([
148+
{ object: 'widget', action: 'create', data: { id: 'w1', title: 'x' } },
149+
{ object: 'vault', action: 'create', data: { id: 'v1', title: 'x' } },
150+
]);
151+
expect(batch.status).toBe(404);
152+
expect(batch.body?.code).toBe('OBJECT_API_DISABLED');
153+
expect(h.transaction).not.toHaveBeenCalled();
154+
expect(await h.count('exl__widget')).toBe(0);
155+
expect(await h.count('exl__vault')).toBe(0);
156+
});
157+
158+
// ── controls: answers that do not move ────────────────────────────────────
159+
160+
it('control: the hidden object\'s full name is refused, as it already was', async () => {
161+
const door = await h.data('exl__vault', { id: 'v0', title: 'x' });
162+
expect(door.status).toBe(404);
163+
const batch = await h.batch([{ object: 'exl__vault', action: 'create', data: { id: 'v1', title: 'x' } }]);
164+
expect(batch).toEqual({ status: door.status, body: door.body });
165+
expect(await h.count('exl__vault')).toBe(0);
166+
});
167+
168+
it('control: an exposed object is written under its short name and under its full name', async () => {
169+
const batch = await h.batch([
170+
{ object: 'widget', action: 'create', data: { id: 'w1', title: 'x' } },
171+
{ object: 'exl__widget', action: 'create', data: { id: 'w2', title: 'x' } },
172+
]);
173+
expect(batch.status).toBe(200);
174+
expect(batch.body?.results).toHaveLength(2);
175+
expect(h.transaction).toHaveBeenCalledTimes(1);
176+
expect(await h.count('exl__widget')).toBe(2);
177+
});
178+
179+
it('control: a plain-named hidden object is refused, as it already was', async () => {
180+
const door = await h.data('ex_secret', { id: 's0', title: 'x' });
181+
expect(door.status).toBe(404);
182+
expect(door.body?.code).toBe('OBJECT_API_DISABLED');
183+
const batch = await h.batch([{ object: 'ex_secret', action: 'create', data: { id: 's1', title: 'x' } }]);
184+
expect(batch).toEqual({ status: door.status, body: door.body });
185+
expect(h.transaction).not.toHaveBeenCalled();
186+
expect(await h.count('ex_secret')).toBe(0);
187+
});
188+
});

0 commit comments

Comments
 (0)