|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { externalBindingDatasourceRefusal } from '@objectstack/spec/data'; |
| 5 | +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; |
| 6 | +import { registerDatasourceAdminRoutes } from '../admin-routes.js'; |
| 7 | +import { |
| 8 | + DatasourceAdminService, |
| 9 | + type DatasourceAdminServiceConfig, |
| 10 | + type StoredDatasource, |
| 11 | +} from '../datasource-admin-service.js'; |
| 12 | +import { |
| 13 | + ENTITLED_CREDENTIAL, |
| 14 | + createSessionAuthService, |
| 15 | + createGrantsEngine, |
| 16 | +} from './entitled-caller.fixture.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * #25277 — ADR-0015 §4.2 at the datasource admin door. |
| 20 | + * |
| 21 | + * The metadata save door refuses a datasource saved or changed to `managed` |
| 22 | + * (or with no `schemaMode`, the spec default) while objects bind it with |
| 23 | + * `external`. `POST /api/v1/datasources` and `PATCH /api/v1/datasources/:name` |
| 24 | + * write the datasource without entering that door, so they stored the pair |
| 25 | + * the save door refuses: the runtime then skips the bound objects' DDL as |
| 26 | + * federated while the boot validation and the write gate judge them managed. |
| 27 | + * |
| 28 | + * Driven through the REAL routes over the REAL service. Each refusal pin reads |
| 29 | + * the envelope a caller sees (status + `error.code`), the bound objects the |
| 30 | + * message names, and the store behind it: nothing stored, registered or |
| 31 | + * minted. The expected message is the spec predicate's own for the same pair, |
| 32 | + * derived here, never restated. |
| 33 | + */ |
| 34 | + |
| 35 | +const authService = createSessionAuthService(); |
| 36 | +const grantsEngine = createGrantsEngine(); |
| 37 | + |
| 38 | +interface BoundObject { |
| 39 | + name: string; |
| 40 | + datasource?: string; |
| 41 | + external?: unknown; |
| 42 | +} |
| 43 | + |
| 44 | +/** In-memory datasource store, secret store and object set behind a real {@link DatasourceAdminService}. */ |
| 45 | +function makeDoor(opts: { seed?: StoredDatasource[]; objects?: BoundObject[] } = {}) { |
| 46 | + const records = new Map<string, StoredDatasource>((opts.seed ?? []).map((r) => [r.name, structuredClone(r)])); |
| 47 | + const secrets = new Map<string, string>(); |
| 48 | + const objects = opts.objects ?? []; |
| 49 | + let seq = 0; |
| 50 | + const ops: string[] = []; |
| 51 | + |
| 52 | + const config: DatasourceAdminServiceConfig = { |
| 53 | + probe: async () => ({ ok: true, latencyMs: 1 }), |
| 54 | + listDatasourceRecords: async () => [...records.values()].map((r) => structuredClone(r)), |
| 55 | + getDatasourceRecord: async (name) => { |
| 56 | + const r = records.get(name); |
| 57 | + return r ? structuredClone(r) : undefined; |
| 58 | + }, |
| 59 | + putDatasourceRecord: async (record) => { |
| 60 | + ops.push(`put:${record.name}`); |
| 61 | + records.set(record.name, structuredClone(record)); |
| 62 | + }, |
| 63 | + deleteDatasourceRecord: async (name) => { |
| 64 | + records.delete(name); |
| 65 | + }, |
| 66 | + writeSecret: async (input, hint) => { |
| 67 | + ops.push('writeSecret'); |
| 68 | + const ref = `sys_secret://datasource/${hint.name}#${++seq}`; |
| 69 | + secrets.set(ref, input.value); |
| 70 | + return ref; |
| 71 | + }, |
| 72 | + removeSecret: async (ref) => { |
| 73 | + ops.push('removeSecret'); |
| 74 | + secrets.delete(ref); |
| 75 | + }, |
| 76 | + countBoundObjects: async (name) => objects.filter((o) => o.datasource === name).length, |
| 77 | + listObjectsBoundTo: async (name) => |
| 78 | + objects.filter((o) => o.datasource === name).map((o) => ({ name: o.name, external: o.external })), |
| 79 | + registerPool: () => { |
| 80 | + ops.push('registerPool'); |
| 81 | + }, |
| 82 | + unregisterPool: () => { |
| 83 | + ops.push('unregisterPool'); |
| 84 | + }, |
| 85 | + reregisterPool: () => { |
| 86 | + ops.push('reregisterPool'); |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + const service = new DatasourceAdminService(config); |
| 91 | + const server = new HonoHttpServer(0); |
| 92 | + const ctx = { |
| 93 | + getService: vi.fn((name: string) => |
| 94 | + name === 'auth' |
| 95 | + ? authService |
| 96 | + : name === 'objectql' || name === 'data' |
| 97 | + ? grantsEngine |
| 98 | + : name === 'datasource-admin' |
| 99 | + ? service |
| 100 | + : undefined, |
| 101 | + ), |
| 102 | + } as any; |
| 103 | + registerDatasourceAdminRoutes(server, ctx, '/api/v1'); |
| 104 | + const app = server.getRawApp(); |
| 105 | + |
| 106 | + const send = (method: string, path: string, body: unknown) => |
| 107 | + app.fetch( |
| 108 | + new Request(`http://local/api/v1${path}`, { |
| 109 | + method, |
| 110 | + headers: { 'content-type': 'application/json', authorization: ENTITLED_CREDENTIAL }, |
| 111 | + body: JSON.stringify(body), |
| 112 | + }), |
| 113 | + ); |
| 114 | + |
| 115 | + return { send, records, secrets, ops }; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * The spec predicate's message for `object` bound with `external` to a |
| 120 | + * datasource declared `declared`. Throws when the predicate makes no `managed` |
| 121 | + * refusal, so a pin can never pass on a premise the spec no longer holds. |
| 122 | + */ |
| 123 | +function predicateMessage(object: BoundObject, declared: { schemaMode?: unknown }): string { |
| 124 | + const refusal = externalBindingDatasourceRefusal(object.name, object.external, object.datasource, declared); |
| 125 | + if (refusal?.shape !== 'managed') throw new Error(`fixture premise broken: no managed refusal for '${object.name}'`); |
| 126 | + return `schemaMode: ${refusal.message}`; |
| 127 | +} |
| 128 | + |
| 129 | +async function expectRefusal(res: Response, expected: string[]): Promise<string> { |
| 130 | + expect(res.status).toBe(400); |
| 131 | + const body = (await res.json()) as { success: boolean; error: { code: string; message: string } }; |
| 132 | + expect(body.success).toBe(false); |
| 133 | + expect(body.error.code).toBe('DATASOURCE_ADMIN_ERROR'); |
| 134 | + for (const line of expected) expect(body.error.message).toContain(line); |
| 135 | + return body.error.message; |
| 136 | +} |
| 137 | + |
| 138 | +const PG = { database: 'analytics' }; |
| 139 | +const ORDERS: BoundObject = { name: 'wh_orders', datasource: 'wh', external: { remoteName: 'orders' } }; |
| 140 | +const ITEMS: BoundObject = { name: 'wh_items', datasource: 'wh', external: { remoteName: 'items' } }; |
| 141 | +/** Names `wh` as its datasource with no `external` binding: a managed object on it. */ |
| 142 | +const LOCAL: BoundObject = { name: 'wh_local', datasource: 'wh' }; |
| 143 | +/** Bound `external` to ANOTHER datasource: never part of `wh`'s verdict. */ |
| 144 | +const ELSEWHERE: BoundObject = { name: 'lake_events', datasource: 'lake', external: { remoteName: 'events' } }; |
| 145 | + |
| 146 | +describe('POST /api/v1/datasources refuses a managed datasource under objects bound external (#25277)', () => { |
| 147 | + it("refuses schemaMode 'managed' — nothing stored, registered or minted", async () => { |
| 148 | + const door = makeDoor({ objects: [ORDERS] }); |
| 149 | + const res = await door.send('POST', '/datasources', { |
| 150 | + name: 'wh', |
| 151 | + driver: 'postgres', |
| 152 | + schemaMode: 'managed', |
| 153 | + config: PG, |
| 154 | + secret: 'any-secret', |
| 155 | + }); |
| 156 | + |
| 157 | + const message = await expectRefusal(res, [predicateMessage(ORDERS, { schemaMode: 'managed' })]); |
| 158 | + expect(message.startsWith("Invalid datasource 'wh'.")).toBe(true); |
| 159 | + expect(door.records.size).toBe(0); |
| 160 | + expect(door.secrets.size).toBe(0); |
| 161 | + expect(door.ops).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('refuses no schemaMode (managed by the spec default)', async () => { |
| 165 | + const door = makeDoor({ objects: [ORDERS] }); |
| 166 | + const res = await door.send('POST', '/datasources', { name: 'wh', driver: 'postgres', config: PG }); |
| 167 | + |
| 168 | + await expectRefusal(res, [predicateMessage(ORDERS, {})]); |
| 169 | + expect(door.records.size).toBe(0); |
| 170 | + expect(door.ops).toEqual([]); |
| 171 | + }); |
| 172 | + |
| 173 | + it('names every object bound external, and only those', async () => { |
| 174 | + const door = makeDoor({ objects: [ORDERS, ITEMS, LOCAL, ELSEWHERE] }); |
| 175 | + const res = await door.send('POST', '/datasources', { name: 'wh', driver: 'postgres', config: PG }); |
| 176 | + |
| 177 | + const message = await expectRefusal(res, [ |
| 178 | + predicateMessage(ORDERS, {}), |
| 179 | + predicateMessage(ITEMS, {}), |
| 180 | + ]); |
| 181 | + expect(message).not.toContain("'wh_local'"); |
| 182 | + expect(message).not.toContain("'lake_events'"); |
| 183 | + expect(door.records.size).toBe(0); |
| 184 | + }); |
| 185 | + |
| 186 | + it('control: the same POST with no object bound external is created', async () => { |
| 187 | + const door = makeDoor({ objects: [LOCAL, ELSEWHERE] }); |
| 188 | + const res = await door.send('POST', '/datasources', { name: 'wh', driver: 'postgres', config: PG }); |
| 189 | + |
| 190 | + expect(res.status).toBe(201); |
| 191 | + expect(door.records.get('wh')?.schemaMode).toBeUndefined(); |
| 192 | + expect(door.ops).toEqual(['put:wh', 'registerPool']); |
| 193 | + }); |
| 194 | + |
| 195 | + it.each(['external', 'validate-only'] as const)( |
| 196 | + "control: a federated datasource (schemaMode '%s') under objects bound external is created", |
| 197 | + async (schemaMode) => { |
| 198 | + const door = makeDoor({ objects: [ORDERS] }); |
| 199 | + const res = await door.send('POST', '/datasources', { |
| 200 | + name: 'wh', |
| 201 | + driver: 'postgres', |
| 202 | + schemaMode, |
| 203 | + config: PG, |
| 204 | + external: {}, |
| 205 | + }); |
| 206 | + |
| 207 | + expect(res.status).toBe(201); |
| 208 | + expect(door.records.get('wh')?.schemaMode).toBe(schemaMode); |
| 209 | + }, |
| 210 | + ); |
| 211 | +}); |
| 212 | + |
| 213 | +describe('PATCH /api/v1/datasources/:name refuses making a bound datasource managed (#25277)', () => { |
| 214 | + const federated = (): StoredDatasource => ({ |
| 215 | + name: 'wh', |
| 216 | + driver: 'postgres', |
| 217 | + origin: 'runtime', |
| 218 | + schemaMode: 'external', |
| 219 | + config: PG, |
| 220 | + external: {}, |
| 221 | + }); |
| 222 | + |
| 223 | + it("refuses schemaMode 'managed' — record, pool and secrets untouched", async () => { |
| 224 | + const door = makeDoor({ seed: [federated()], objects: [ORDERS, ITEMS] }); |
| 225 | + const before = structuredClone(door.records.get('wh')); |
| 226 | + |
| 227 | + const res = await door.send('PATCH', '/datasources/wh', { schemaMode: 'managed', secret: 'rotated' }); |
| 228 | + |
| 229 | + await expectRefusal(res, [ |
| 230 | + predicateMessage(ORDERS, { schemaMode: 'managed' }), |
| 231 | + predicateMessage(ITEMS, { schemaMode: 'managed' }), |
| 232 | + ]); |
| 233 | + expect(door.records.get('wh')).toEqual(before); |
| 234 | + expect(door.secrets.size).toBe(0); |
| 235 | + expect(door.ops).toEqual([]); |
| 236 | + }); |
| 237 | + |
| 238 | + it('control: the same PATCH with no object bound external is applied', async () => { |
| 239 | + const door = makeDoor({ seed: [federated()], objects: [LOCAL, ELSEWHERE] }); |
| 240 | + const res = await door.send('PATCH', '/datasources/wh', { schemaMode: 'managed' }); |
| 241 | + |
| 242 | + expect(res.status).toBe(200); |
| 243 | + expect(door.records.get('wh')?.schemaMode).toBe('managed'); |
| 244 | + }); |
| 245 | + |
| 246 | + it("control: a federated PATCH (schemaMode 'validate-only') under objects bound external is applied", async () => { |
| 247 | + const door = makeDoor({ seed: [federated()], objects: [ORDERS] }); |
| 248 | + const res = await door.send('PATCH', '/datasources/wh', { schemaMode: 'validate-only' }); |
| 249 | + |
| 250 | + expect(res.status).toBe(200); |
| 251 | + expect(door.records.get('wh')?.schemaMode).toBe('validate-only'); |
| 252 | + }); |
| 253 | + |
| 254 | + it('refuses a config edit on a managed row that objects already bind external', async () => { |
| 255 | + const managed: StoredDatasource = { name: 'wh', driver: 'postgres', origin: 'runtime', config: PG }; |
| 256 | + const door = makeDoor({ seed: [managed], objects: [ORDERS] }); |
| 257 | + |
| 258 | + const res = await door.send('PATCH', '/datasources/wh', { config: { database: 'warehouse' } }); |
| 259 | + |
| 260 | + await expectRefusal(res, [predicateMessage(ORDERS, {})]); |
| 261 | + expect(door.records.get('wh')).toEqual(managed); |
| 262 | + expect(door.ops).toEqual([]); |
| 263 | + }); |
| 264 | + |
| 265 | + it('control: a label/active-only PATCH on that row is applied, so it can still be taken out of service', async () => { |
| 266 | + const managed: StoredDatasource = { name: 'wh', driver: 'postgres', origin: 'runtime', config: PG }; |
| 267 | + const door = makeDoor({ seed: [managed], objects: [ORDERS] }); |
| 268 | + |
| 269 | + const res = await door.send('PATCH', '/datasources/wh', { label: 'Warehouse', active: false }); |
| 270 | + |
| 271 | + expect(res.status).toBe(200); |
| 272 | + expect(door.records.get('wh')).toMatchObject({ label: 'Warehouse', active: false }); |
| 273 | + }); |
| 274 | +}); |
0 commit comments