Files
clean-architecture-frontend…/tests/unit/rest-profile-contract.test.ts
T
DongHyeonkaandClaude Opus 5.5 5124c2f65f fix(contracts): resolve REST auth profiles from the installed registry (Q-6.7-9)
Composition checked authProfileId against the module-global installed
registry, the V3 executor used its injected registry, and the V2 client
resolved against the raw REST_AUTH_PROFILES record (or an injected raw
record) that was never installed. A BEARER_HEADER profile with
`credentials: "same-origin"` is refused by installation but resolved
fine.

- resolveRestSecurityProfiles / validateRestProfileBindings take
  `RestAuthProfileSource` (installed registry or raw record) and default
  to INSTALLED_REST_AUTH_PROFILES. A raw record is installed (validated)
  before lookup; lookups go through the registry, and the CSRF record
  lookup uses Object.hasOwn.
- composeContractContributions takes the registry as an optional
  second parameter (default INSTALLED_REST_AUTH_PROFILES).
- src/adapters/http/client.ts: only the V2 resolve call site (and its
  import) now falls back to INSTALLED_REST_AUTH_PROFILES.

Verified: 3 new cases in tests/unit/rest-profile-contract.test.ts were
RED before (incoherent raw record accepted, installed registry not
resolvable, composition ignored the given registry); gates green except
the known removal-fixture test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:45:37 +09:00

175 lines
5.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { defineRestOperation } from "../../src/contracts/api-operations.ts";
import {
composeContractContributions,
ContractContributionError,
} from "../../src/contracts/external-contract-runtime.ts";
import {
createRestProviderProfile,
INSTALLED_REST_AUTH_PROFILES,
installRestAuthProfileRegistry,
resolveRestSecurityProfiles,
REST_AUTH_PROFILES,
validateRestProfileBindings,
} from "../../src/contracts/rest-profiles.ts";
import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts";
/**
* §24.12: the common REST profile contract must stay independently verifiable
* after the sample feature is removed, so this suite owns its own operation
* fixture instead of importing an installed feature contract.
*/
const SAMPLE_COMMAND = defineRestOperation({
method: "POST",
path: "/api/sample-resources",
operationId: "CREATE_SAMPLE_RESOURCE",
auth: "external-session",
timeoutMs: null,
idempotency: "keyed",
retry: "runtime",
requestSource: "body",
requestSchema: "SampleCommand",
responseSchema: "SamplePayload",
owner: "platform-test-fixture",
contractVersion: 2,
protocol: "REST",
semantics: "COMMAND",
replayPolicy: "KEYED_COMMAND",
idempotencyKeyPolicy: "REQUIRED",
mapperId: "SampleMapper",
successStatuses: [200, 201],
responseMediaTypes: ["application/json"],
maxResponseBytes: 32_768,
providerId: "PRIMARY_API",
authProfileId: "REFERENCE_EXTERNAL_BEARER",
csrfProfileId: "NO_CSRF_BEARER",
pathSchema: "NoRequest",
pathParameterNames: [],
maxEncodedSearchBytes: 0,
});
const SAMPLE_OPERATIONS = Object.freeze({
CREATE_SAMPLE_RESOURCE: SAMPLE_COMMAND,
});
describe("REST provider/auth/CSRF profiles", () => {
it("preserves the provider prefix and rejects unsafe endpoint forms", () => {
expect(
createRestProviderProfile(
"PRIMARY_API",
"https://api.test/base/",
["omit"],
),
).toMatchObject({
providerId: "PRIMARY_API",
baseUrl: "https://api.test/base/",
redirect: "error",
referrerPolicy: "no-referrer",
});
expect(() =>
createRestProviderProfile(
"PRIMARY_API",
"https://user:password@api.test/base/",
),
).toThrow("Invalid REST provider profile");
expect(() =>
createRestProviderProfile("PRIMARY_API", "http://api.test/base/"),
).toThrow("Invalid REST provider profile");
});
it("resolves bearer auth to omit credentials and no CSRF", () => {
const resolved = resolveRestSecurityProfiles(
SAMPLE_COMMAND,
createRestProviderProfile("PRIMARY_API", "https://api.test", ["omit"]),
);
expect(resolved).toMatchObject({
auth: {
transport: "BEARER_HEADER",
credentials: "omit",
allowedCredentialHeaders: ["authorization"],
},
csrf: { mode: "NONE" },
});
});
it("validates every installed profile binding as a set", () => {
expect(
validateRestProfileBindings(SAMPLE_OPERATIONS, {
PRIMARY_API: ["omit"],
}),
).toBe(true);
expect(() =>
validateRestProfileBindings(SAMPLE_OPERATIONS, {}),
).toThrow("Unregistered REST provider binding");
});
});
/**
* Q-6.7-9. Composition, the V3 executor and the V2 resolve must all answer
* from one validated installed registry. A raw record handed to resolve used
* to skip installation, so a bearer profile with ambient credentials passed.
*/
describe("one installed REST auth profile registry", () => {
const provider = createRestProviderProfile("PRIMARY_API", "https://api.test", [
"omit",
"same-origin",
]);
it("rejects a raw profile record that installation would refuse", () => {
const incoherent = {
REFERENCE_EXTERNAL_BEARER: {
...REST_AUTH_PROFILES.REFERENCE_EXTERNAL_BEARER,
credentials: "same-origin" as const,
},
};
expect(() =>
resolveRestSecurityProfiles(SAMPLE_COMMAND, provider, incoherent),
).toThrow(TypeError);
});
it("resolves through the installed registry", () => {
expect(
resolveRestSecurityProfiles(
SAMPLE_COMMAND,
provider,
INSTALLED_REST_AUTH_PROFILES,
).auth,
).toBe(INSTALLED_REST_AUTH_PROFILES.get("REFERENCE_EXTERNAL_BEARER"));
});
it("does not resolve an inherited profile id", () => {
expect(() =>
resolveRestSecurityProfiles(
{ ...SAMPLE_COMMAND, authProfileId: "toString" },
provider,
REST_AUTH_PROFILES,
),
).toThrow("incoherent");
});
it("composes contributions against the registry it is given", () => {
const registry = installRestAuthProfileRegistry({
...REST_AUTH_PROFILES,
CUSTOM_BEARER: {
...REST_AUTH_PROFILES.REFERENCE_EXTERNAL_BEARER,
authProfileId: "CUSTOM_BEARER",
},
});
const row = TEST_CONTRACT_CONTRIBUTION.http[0]!;
const contribution = {
...TEST_CONTRACT_CONTRIBUTION,
http: [
{ ...row, frontend: { ...row.frontend, authProfileId: "CUSTOM_BEARER" } },
],
};
expect(() => composeContractContributions([contribution])).toThrow(
ContractContributionError,
);
expect(
composeContractContributions([contribution], registry).httpByOperationId
.size,
).toBe(1);
});
});