Skip to content

Commit 9cecea4

Browse files
committed
wip: refused targetNamespace answers INVALID_REQUEST; Q6 pins
1 parent 8ef4d30 commit 9cecea4

3 files changed

Lines changed: 77 additions & 6 deletions

File tree

‎packages/cloud-connection/src/marketplace-install-local-install-modes.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import '@objectstack/runtime';
3636
import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js';
3737
import { declareInstallerPlatformOwner, installerAuthService, withInstallerGrants } from './install-local-principal.fixtures.js';
3838
import { LocalManifestSource } from './local-manifest-source.js';
39+
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
3940

4041
const ROUTE = '/api/v1/marketplace/install-local';
4142
const INSTALLER = 'usr_installer';
@@ -567,3 +568,61 @@ describe('install-local — `targetNamespace` renames an unmanaged copy, and onl
567568
});
568569
}
569570
});
571+
572+
describe('install-local — the import\'s own `targetNamespace` refusal, as the door answers it (#30794)', () => {
573+
// The REAL copy-once import, not a double: the refusal is judged by its
574+
// plan before the engine is asked anything, so an empty engine suffices.
575+
const realImporter = () => {
576+
const protocol = new ObjectStackProtocolImplementation({ registry: {} } as any, () => new Map()) as any;
577+
return (request: any) => protocol.importUnmanagedPackage(request);
578+
};
579+
580+
for (const [label, targetNamespace, sentence] of [
581+
['a value the namespace declaration refuses', 'Acme', "Invalid package namespace 'Acme' on `targetNamespace`"],
582+
['a platform-reserved namespace', 'sys', "`targetNamespace` 'sys' is a platform-reserved namespace"],
583+
] as const) {
584+
it(`${label}: 400 INVALID_REQUEST, the reason in the body and \`details.targetNamespace\`; nothing written`, async () => {
585+
const h = await mount(dir, { importer: realImporter() });
586+
const res = await h.install({ manifest: crm({ installModes: BOTH }), installMode: 'unmanaged', targetNamespace });
587+
expect(res.status, JSON.stringify(res.payload)).toBe(400);
588+
expect(res.payload.error.code).toBe('INVALID_REQUEST');
589+
expect(res.payload.error.message).toContain(sentence);
590+
expect(res.payload.error.message).not.toContain('the reason is in the server log');
591+
expect(res.payload.error.details).toEqual({ targetNamespace });
592+
expect(h.register).not.toHaveBeenCalled();
593+
expect(h.ledgerHas('com.example.crm')).toBe(false);
594+
});
595+
}
596+
});
597+
598+
describe('install-local — Q6 after a RENAMED copy: the managed install is admitted; after an unrenamed one, refused (#30794)', () => {
599+
// The ruling (record 6709037798, Q6 = B): a copy renamed into a namespace
600+
// of its own does not hold the source namespace, so a later managed
601+
// install of the same source package is not refused. A copy that kept the
602+
// source namespace still holds it, and Q6 (step 1f) refuses as written.
603+
const renamedCopyHeld: RegistryState = {
604+
owners: { acme: ['com.example.crm-copy'] },
605+
packages: { 'com.example.crm-copy': { manifest: { id: 'com.example.crm-copy', namespace: 'acme' } } },
606+
};
607+
const unrenamedCopyHeld: RegistryState = {
608+
owners: { crm: ['com.example.crm-copy'] },
609+
packages: { 'com.example.crm-copy': { manifest: { id: 'com.example.crm-copy', namespace: 'crm' } } },
610+
};
611+
612+
it('beside a copy renamed to `acme`: the managed install of `com.example.crm` proceeds to its own steps', async () => {
613+
const h = await mount(dir, { registry: renamedCopyHeld });
614+
const res = await h.install({ manifest: crm({ installModes: BOTH }) });
615+
expect(res.payload?.error?.code, JSON.stringify(res.payload)).not.toBe('RESOURCE_CONFLICT');
616+
expect(h.register).toHaveBeenCalledTimes(1);
617+
});
618+
619+
it('TWIN: beside a copy that kept `crm`, the same install is refused 409 RESOURCE_CONFLICT, before any write', async () => {
620+
const h = await mount(dir, { registry: unrenamedCopyHeld });
621+
const res = await h.install({ manifest: crm({ installModes: BOTH }) });
622+
expect(res.status).toBe(409);
623+
expect(res.payload.error.code).toBe('RESOURCE_CONFLICT');
624+
expect(res.payload.error.details).toEqual({ namespace: 'crm', base: 'com.example.crm-copy' });
625+
expect(h.register).not.toHaveBeenCalled();
626+
expect(h.ledgerHas('com.example.crm')).toBe(false);
627+
});
628+
});

‎packages/metadata-protocol/src/package-unmanaged-install.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -954,20 +954,22 @@ describe('planUnmanagedImport — `targetNamespace`, the rename-on-install (#307
954954
}
955955
});
956956

957-
it('refuses a value the namespace declaration refuses — 400, naming `targetNamespace`, never sanitised', () => {
957+
it('refuses a value the namespace declaration refuses — INVALID_REQUEST / 400, naming `targetNamespace`, never sanitised', () => {
958958
for (const targetNamespace of ['Acme', 'a', 'my-ns', '1acme', 'acme_namespace_too_long']) {
959959
const err = (() => { try { plan(renamableBody(), { targetNamespace }); } catch (e) { return e as any; } })();
960960
const envelope = resolveThrownHttpError(err);
961-
expect([envelope.status, envelope.code]).toEqual([400, 'VALIDATION_ERROR']);
961+
expect([envelope.status, envelope.code]).toEqual([400, 'INVALID_REQUEST']);
962+
expect(err.details).toEqual({ targetNamespace });
962963
expect(err.message).toContain(`Invalid package namespace '${targetNamespace}' on \`targetNamespace\``);
963964
}
964965
});
965966

966-
it('refuses a platform-reserved namespace — `base`, `system`, `sys` — 400', () => {
967+
it('refuses a platform-reserved namespace — `base`, `system`, `sys` — INVALID_REQUEST / 400, naming `targetNamespace`', () => {
967968
for (const targetNamespace of ['base', 'system', 'sys']) {
968969
const err = (() => { try { plan(renamableBody(), { targetNamespace }); } catch (e) { return e as any; } })();
969970
const envelope = resolveThrownHttpError(err);
970-
expect([envelope.status, envelope.code]).toEqual([400, 'VALIDATION_ERROR']);
971+
expect([envelope.status, envelope.code]).toEqual([400, 'INVALID_REQUEST']);
972+
expect(err.details).toEqual({ targetNamespace });
971973
expect(err.message).toContain(`\`targetNamespace\` '${targetNamespace}' is a platform-reserved namespace`);
972974
}
973975
});

‎packages/metadata-protocol/src/package-unmanaged-install.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,26 @@ export function planUnmanagedImport(
480480
const declared = ManifestSchema.shape.namespace.safeParse(targetNamespace);
481481
if (!declared.success) {
482482
const rule = declared.error.issues[0]?.message ?? 'See `manifest.namespace`';
483-
throw invalidRequest(
483+
// `INVALID_REQUEST` / 400 with `details.targetNamespace`, as the
484+
// documented refusal row and the rename refusal below answer: a
485+
// code-less 400 reaches the boundary as the status-derived
486+
// `VALIDATION_ERROR`, and the install-local door withholds its reason.
487+
throw refusal(
488+
'INVALID_REQUEST',
489+
400,
484490
`Invalid package namespace '${targetNamespace}' on \`targetNamespace\`. ${rule}. It becomes the `
485491
+ 'copy\'s `manifest.namespace` and the prefix of every copied object name.',
492+
{ targetNamespace },
486493
);
487494
}
488495
if (PLATFORM_RESERVED_NAMESPACES.has(targetNamespace)) {
489-
throw invalidRequest(
496+
throw refusal(
497+
'INVALID_REQUEST',
498+
400,
490499
`\`targetNamespace\` '${targetNamespace}' is a platform-reserved namespace `
491500
+ `(${[...PLATFORM_RESERVED_NAMESPACES].map((ns) => `'${ns}'`).join(', ')}), which many packages share, `
492501
+ 'so it cannot carry one copy\'s names. Name a namespace of the copy\'s own.',
502+
{ targetNamespace },
493503
);
494504
}
495505
}

0 commit comments

Comments
 (0)