|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27728, #27729] Both import doors answer for the object exactly as the data |
| 5 | + * door does (`POST /data/:object`), before any row or job is written: |
| 6 | + * |
| 7 | + * - the synchronous door (`POST /data/:object/import`) refuses an unregistered |
| 8 | + * object with the data door's 404 `OBJECT_NOT_FOUND`, where it used to answer |
| 9 | + * 200 with the refusal repeated inside every row's result (#27728); |
| 10 | + * - both doors refuse an object of a DISABLED package with the data door's |
| 11 | + * 404 `OBJECT_PACKAGE_DISABLED`, status and body, and write no row and no |
| 12 | + * `sys_import_job` row (#27729 item 1); |
| 13 | + * - a legacy `ns__short` object addressed by its short name is the object |
| 14 | + * behind that name at every door: the existence read and the API-exposure |
| 15 | + * gate both take the item the by-name read resolves, as the data door's |
| 16 | + * `registry.getObject` does, so the job door no longer refuses a name the |
| 17 | + * data door serves, and the gate judges that object's `enable` instead of |
| 18 | + * passing the short name through (#27729 item 2). |
| 19 | + * |
| 20 | + * A REAL `ObjectQL` engine + `ObjectStackProtocolImplementation` on a REAL |
| 21 | + * sqlite `:memory:` driver; nothing below the routes is a double. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 25 | +import { ObjectQL } from '@objectstack/objectql'; |
| 26 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 27 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 28 | +import { SysMetadataAuditObject, SysMetadataCommitObject, SysMetadataHistoryObject, SysMetadataObject } from '@objectstack/metadata-core'; |
| 29 | +import { SysImportJob } from '@objectstack/platform-objects/audit'; |
| 30 | +import { RestServer } from './rest-server'; |
| 31 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 32 | + |
| 33 | +const liveEngines: ObjectQL[] = []; |
| 34 | +afterEach(async () => { |
| 35 | + while (liveEngines.length) { |
| 36 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +const fields = { |
| 41 | + id: { name: 'id', type: 'text' as const, primaryKey: true, label: 'ID' }, |
| 42 | + title: { name: 'title', type: 'text' as const, label: 'Title' }, |
| 43 | +}; |
| 44 | + |
| 45 | +/** A plain object, the control. */ |
| 46 | +const TASK = { name: 'ex_task', label: 'Task', systemFields: false, fields }; |
| 47 | +/** An object of a package the test disables. */ |
| 48 | +const OFF_PACKAGE = 'app.ex_off'; |
| 49 | +const GADGET = { name: 'ex_gadget', label: 'Gadget', systemFields: false, fields }; |
| 50 | +/** Legacy `ns__short` objects, addressed below by their short names. */ |
| 51 | +const LEGACY_PACKAGE = 'app.ex_legacy'; |
| 52 | +const WIDGET = { name: 'exl__widget', label: 'Widget', systemFields: false, fields }; |
| 53 | +const VAULT = { name: 'exl__vault', label: 'Vault', systemFields: false, enable: { apiEnabled: false }, fields }; |
| 54 | + |
| 55 | +function makeRes() { |
| 56 | + const res: any = { |
| 57 | + write: () => true, end: () => {}, |
| 58 | + header: () => res, |
| 59 | + status: (code: number) => { res._status = code; return res; }, |
| 60 | + json: (body: any) => { res._json = body; return res; }, |
| 61 | + }; |
| 62 | + return res; |
| 63 | +} |
| 64 | + |
| 65 | +async function boot() { |
| 66 | + const engine = new ObjectQL(); |
| 67 | + liveEngines.push(engine); |
| 68 | + engine.registerDriver(new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), true); |
| 69 | + await engine.init(); |
| 70 | + engine.registry.installPackage({ id: OFF_PACKAGE, name: OFF_PACKAGE, version: '1.0.0' } as any, 'code'); |
| 71 | + engine.registry.installPackage({ id: LEGACY_PACKAGE, name: LEGACY_PACKAGE, version: '1.0.0' } as any, 'code'); |
| 72 | + engine.registry.registerObject(TASK as any); |
| 73 | + engine.registry.registerObject(GADGET as any, OFF_PACKAGE); |
| 74 | + engine.registry.registerObject(WIDGET as any, LEGACY_PACKAGE); |
| 75 | + engine.registry.registerObject(VAULT as any, LEGACY_PACKAGE); |
| 76 | + engine.registry.registerObject(SysImportJob as any); |
| 77 | + await engine.syncSchemas(); |
| 78 | + // A real `single` install's Default Organization (ADR-0131 D9). |
| 79 | + await installDefaultOrganization(engine); |
| 80 | + for (const o of [SysMetadataObject, SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject]) { |
| 81 | + if (!engine.registry.getObject(o.name)) engine.registry.registerObject(o as any); |
| 82 | + } |
| 83 | + |
| 84 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 85 | + const noop = () => {}; |
| 86 | + const server = { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 87 | + const rest = new RestServer(server as any, protocol as any, { api: { requireAuth: false } } as any); |
| 88 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 89 | + rest.registerRoutes(); |
| 90 | + const routes = rest.getRoutes(); |
| 91 | + const route = (path: string): any => { |
| 92 | + const found = routes.find((r: any) => r.method === 'POST' && r.path === path); |
| 93 | + if (!found) throw new Error(`route POST ${path} is not registered`); |
| 94 | + return found; |
| 95 | + }; |
| 96 | + const doors = { |
| 97 | + data: route('/api/v1/data/:object'), |
| 98 | + sync: route('/api/v1/data/:object/import'), |
| 99 | + jobs: route('/api/v1/data/:object/import/jobs'), |
| 100 | + }; |
| 101 | + return { |
| 102 | + engine, |
| 103 | + async call(door: keyof typeof doors, object: string, body: any) { |
| 104 | + const res = makeRes(); |
| 105 | + await doors[door].handler({ params: { object }, body, query: {} } as any, res); |
| 106 | + // The synchronous door answers with `res.json` alone on success. |
| 107 | + return { status: res._status ?? 200, body: res._json }; |
| 108 | + }, |
| 109 | + /** Rows of a table, counted at the engine. */ |
| 110 | + async count(table: string) { |
| 111 | + const rows = await engine.find(table, { where: {} }); |
| 112 | + return Array.isArray(rows) ? rows.length : 0; |
| 113 | + }, |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +const rowsBody = (id: string) => ({ format: 'json', rows: [{ id, title: 'x' }] }); |
| 118 | + |
| 119 | +describe('[#27728] the synchronous import door refuses an unregistered object as the data door does', () => { |
| 120 | + let h: Awaited<ReturnType<typeof boot>>; |
| 121 | + beforeEach(async () => { h = await boot(); }); |
| 122 | + |
| 123 | + it('answers 404 OBJECT_NOT_FOUND, the data door\'s envelope byte for byte, not 200', async () => { |
| 124 | + const door = await h.call('data', 'ex_no_such_object', { id: 'n1', title: 'x' }); |
| 125 | + expect(door.status).toBe(404); |
| 126 | + expect(door.body?.code).toBe('OBJECT_NOT_FOUND'); |
| 127 | + |
| 128 | + const sync = await h.call('sync', 'ex_no_such_object', rowsBody('n2')); |
| 129 | + expect(sync.status).toBe(404); |
| 130 | + expect(sync.body).toEqual(door.body); |
| 131 | + }); |
| 132 | + |
| 133 | + it('a dry run and a CSV body get the same refusal, before the body is parsed', async () => { |
| 134 | + const door = await h.call('data', 'ex_no_such_object', { id: 'n1', title: 'x' }); |
| 135 | + const dry = await h.call('sync', 'ex_no_such_object', { ...rowsBody('n3'), dryRun: true }); |
| 136 | + expect(dry).toEqual({ status: 404, body: door.body }); |
| 137 | + // Not even a parseable body is needed: the object is asked first. |
| 138 | + const csv = await h.call('sync', 'ex_no_such_object', { format: 'csv', csv: 'not,a\n"broken' }); |
| 139 | + expect(csv).toEqual({ status: 404, body: door.body }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('control: a registered object still imports (200, one row created)', async () => { |
| 143 | + const sync = await h.call('sync', 'ex_task', rowsBody('t1')); |
| 144 | + expect(sync.status).toBe(200); |
| 145 | + expect(sync.body).toMatchObject({ object: 'ex_task', total: 1, ok: 1, created: 1, errors: 0 }); |
| 146 | + expect(await h.count('ex_task')).toBe(1); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe('[#27729 item 1] both import doors refuse a disabled package\'s object with the data door\'s answer', () => { |
| 151 | + let h: Awaited<ReturnType<typeof boot>>; |
| 152 | + beforeEach(async () => { |
| 153 | + h = await boot(); |
| 154 | + h.engine.registry.disablePackage(OFF_PACKAGE); |
| 155 | + }); |
| 156 | + |
| 157 | + it('the data door\'s answer is 404 OBJECT_PACKAGE_DISABLED (the reference)', async () => { |
| 158 | + const door = await h.call('data', 'ex_gadget', { id: 'g0', title: 'x' }); |
| 159 | + expect(door.status).toBe(404); |
| 160 | + expect(door.body?.code).toBe('OBJECT_PACKAGE_DISABLED'); |
| 161 | + }); |
| 162 | + |
| 163 | + for (const kind of ['sync', 'jobs'] as const) { |
| 164 | + it(`${kind}: status and body equal the data door's, and no row and no job row is written`, async () => { |
| 165 | + const door = await h.call('data', 'ex_gadget', { id: 'g0', title: 'x' }); |
| 166 | + const answer = await h.call(kind, 'ex_gadget', rowsBody('g1')); |
| 167 | + expect(answer.status).toBe(door.status); |
| 168 | + expect(answer.body).toEqual(door.body); |
| 169 | + expect(await h.count('sys_import_job')).toBe(0); |
| 170 | + expect(await h.count('ex_gadget')).toBe(0); |
| 171 | + }); |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +describe('[#27729 item 2] a legacy ns__short object is the object behind its short name at every door', () => { |
| 176 | + let h: Awaited<ReturnType<typeof boot>>; |
| 177 | + beforeEach(async () => { h = await boot(); }); |
| 178 | + |
| 179 | + it('premise: the data door serves the short name (registry.getObject resolves it)', async () => { |
| 180 | + expect((h.engine.registry.getObject('widget') as any)?.name).toBe('exl__widget'); |
| 181 | + const door = await h.call('data', 'widget', { id: 'w0', title: 'x' }); |
| 182 | + expect(door.status).toBe(201); |
| 183 | + }); |
| 184 | + |
| 185 | + it('the job door creates the job, where it used to answer OBJECT_NOT_FOUND', async () => { |
| 186 | + const jobs = await h.call('jobs', 'widget', rowsBody('w1')); |
| 187 | + expect(jobs.status).toBe(201); |
| 188 | + expect(jobs.body).toMatchObject({ object: 'widget', status: 'pending', total: 1 }); |
| 189 | + expect(await h.count('sys_import_job')).toBe(1); |
| 190 | + }); |
| 191 | + |
| 192 | + it('the synchronous door imports under the short name', async () => { |
| 193 | + const sync = await h.call('sync', 'widget', rowsBody('w2')); |
| 194 | + expect(sync.status).toBe(200); |
| 195 | + expect(sync.body).toMatchObject({ object: 'widget', ok: 1, created: 1 }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('an unregistered short name is still OBJECT_NOT_FOUND at both import doors', async () => { |
| 199 | + const door = await h.call('data', 'gizmo', { id: 'z0', title: 'x' }); |
| 200 | + expect(door.status).toBe(404); |
| 201 | + expect(await h.call('sync', 'gizmo', rowsBody('z1'))).toEqual(door); |
| 202 | + expect(await h.call('jobs', 'gizmo', rowsBody('z2'))).toEqual(door); |
| 203 | + expect(await h.count('sys_import_job')).toBe(0); |
| 204 | + }); |
| 205 | + |
| 206 | + it('the exposure gate judges the resolved object: apiEnabled:false hides it under its short name too', async () => { |
| 207 | + const full = await h.call('data', 'exl__vault', { id: 'v0', title: 'x' }); |
| 208 | + expect(full.status).toBe(404); |
| 209 | + expect(full.body?.code).toBe('OBJECT_API_DISABLED'); |
| 210 | + for (const kind of ['data', 'sync', 'jobs'] as const) { |
| 211 | + const short = await h.call(kind, 'vault', kind === 'data' ? { id: 'v1', title: 'x' } : rowsBody('v1')); |
| 212 | + expect(short.status).toBe(404); |
| 213 | + expect(short.body?.code).toBe('OBJECT_API_DISABLED'); |
| 214 | + } |
| 215 | + expect(await h.count('exl__vault')).toBe(0); |
| 216 | + expect(await h.count('sys_import_job')).toBe(0); |
| 217 | + }); |
| 218 | +}); |
0 commit comments