import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { describe, expect, it } from "vitest"; import { composeContractContributions, ContractContributionError, } from "../../src/contracts/external-contract-runtime.ts"; import { INSTALLED_REST_AUTH_PROFILES, installRestAuthProfileRegistry, REST_AUTH_PROFILES, } from "../../src/contracts/rest-profiles.ts"; import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts"; describe("one installed REST auth profile registry", () => { it("refuses a raw profile record that is incoherent", () => { expect(() => installRestAuthProfileRegistry({ EXTERNAL_BEARER: { ...REST_AUTH_PROFILES.EXTERNAL_BEARER, credentials: "same-origin" as const, }, }), ).toThrow(TypeError); }); it("does not answer for an inherited profile id", () => { expect(INSTALLED_REST_AUTH_PROFILES.has("toString")).toBe(false); expect(INSTALLED_REST_AUTH_PROFILES.get("toString")).toBeUndefined(); }); it("composes contributions against the registry it is given", () => { const registry = installRestAuthProfileRegistry({ ...REST_AUTH_PROFILES, CUSTOM_BEARER: { ...REST_AUTH_PROFILES.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); }); }); /** * A cookie-authenticated write must carry a CSRF proof header: the browser * attaches the cookie to any site's request, so the header is what tells the * server the page itself sent it. The V2 client refused such a binding; the * installed HTTP contract has to refuse it too. */ describe("cookie profiles on writes", () => { const cookie = (authProfileId: string, csrf: readonly ("x-csrf-token" | "x-xsrf-token")[]) => ({ authProfileId, transport: "SAME_ORIGIN_COOKIE" as const, credentials: "include" as const, allowedCredentialHeaders: [...csrf], requiredCredentialHeaders: [...csrf], }); const registry = installRestAuthProfileRegistry({ ...REST_AUTH_PROFILES, COOKIE_READ: cookie("COOKIE_READ", []), COOKIE_WRITE: cookie("COOKIE_WRITE", ["x-xsrf-token"]), }); const withProfile = (index: number, authProfileId: string) => { const row = TEST_CONTRACT_CONTRIBUTION.http[index]!; return { ...TEST_CONTRACT_CONTRIBUTION, http: [{ ...row, frontend: { ...row.frontend, authProfileId } }], }; }; // TEST_CONTRACT_CONTRIBUTION.http: [list (GET), detail (GET), create (POST)] it("refuses a write whose cookie profile requires no CSRF header", () => { expect(() => composeContractContributions([withProfile(2, "COOKIE_READ")], registry), ).toThrow( expect.objectContaining({ reason: "test-feature/TEST_CREATE_ENTITY: POST with cookie profile COOKIE_READ must require x-csrf-token or x-xsrf-token", }), ); }); it("accepts a write that requires a CSRF header, and a cookie read without one", () => { expect( composeContractContributions([withProfile(2, "COOKIE_WRITE")], registry) .httpByOperationId.size, ).toBe(1); expect( composeContractContributions([withProfile(0, "COOKIE_READ")], registry) .httpByOperationId.size, ).toBe(1); }); }); /** * 1101/16-03. The only bearer profile is the platform's, not the sample's: * `EXTERNAL_BEARER`. The old name stays for one release as a deprecated alias * that resolves to the same transport and headers. */ describe("EXTERNAL_BEARER and its deprecated alias (16-03)", () => { it("resolves the alias exactly like EXTERNAL_BEARER but for its id", () => { expect(INSTALLED_REST_AUTH_PROFILES.get("EXTERNAL_BEARER")).toMatchObject({ transport: "BEARER_HEADER", requiredCredentialHeaders: ["authorization"], }); expect({ ...INSTALLED_REST_AUTH_PROFILES.get("REFERENCE_EXTERNAL_BEARER"), authProfileId: "EXTERNAL_BEARER", }).toEqual(INSTALLED_REST_AUTH_PROFILES.get("EXTERNAL_BEARER")); }); it("keeps the old name in src only on the alias line", async () => { const files = (await readdir("src", { recursive: true })) .filter((file) => /\.tsx?$/u.test(file)) .map((file) => path.join("src", file)); const lines: string[] = []; for (const file of files) { for (const line of (await readFile(file, "utf8")).split("\n")) { if (line.includes("REFERENCE_EXTERNAL_BEARER")) { lines.push(`${file}: ${line.trim()}`); } } } expect(lines).toEqual([ 'src/contracts/rest-profiles.ts: REFERENCE_EXTERNAL_BEARER: Object.freeze({ ...EXTERNAL_BEARER, authProfileId: "REFERENCE_EXTERNAL_BEARER" }),', ]); }); });