diff --git a/tests/unit/realtime/realtime-stream-registry.test.ts b/tests/unit/realtime/realtime-stream-registry.test.ts index dfb7ed62..f4c5f78d 100644 --- a/tests/unit/realtime/realtime-stream-registry.test.ts +++ b/tests/unit/realtime/realtime-stream-registry.test.ts @@ -1,5 +1,11 @@ import { describe, expect, it } from "vitest"; +import { projectApiOperationRows } from "../../../src/contracts/api-operations.ts"; +import { + composeContractContributions, + type HttpMethod, + type RetrySemantics, +} from "../../../src/contracts/external-contract-runtime.ts"; import { createRealtimePolicyRegistry, defineEventTypeId, @@ -20,6 +26,7 @@ import { TEST_SCHEMA_CODECS, createTestRealtimeRegistry, } from "./fixture.ts"; +import { TEST_CONTRACT_CONTRIBUTION } from "../../helpers/external-contract-fixture.ts"; describe("realtime policy registry", () => { it("deep-snapshots registrations and resolves only stream-owned event types", () => { @@ -209,6 +216,105 @@ describe("realtime policy registry", () => { }); }); +/** + * 1103/04-03, 07-02, 10-04. Recovery sends the snapshot operation on every + * reconnect, so it must be a read that may be sent again: GET with SAFE or + * IDEMPOTENT. A HEAD carries no checkpoint body. The row is the FE-REG-API + * projection of a composed descriptor, so a renamed projected field breaks + * this table too. + */ +describe("realtime snapshot operation binding", () => { + const projected = projectApiOperationRows( + composeContractContributions([TEST_CONTRACT_CONTRIBUTION]).contributions, + ); + const methods: readonly HttpMethod[] = [ + "GET", + "HEAD", + "POST", + "PUT", + "PATCH", + "DELETE", + ]; + const retries: readonly RetrySemantics[] = [ + "SAFE", + "IDEMPOTENT", + "KEYED", + "NEVER", + ]; + const register = ( + recovery: "CURSOR" | "SNAPSHOT_ONLY", + apiOperations: Readonly>, + ) => { + const baseline = createTestRealtimeRegistry(); + const stream = baseline.listStreams()[0]!; + return createRealtimePolicyRegistry({ + streams: [ + { + ...stream, + recovery: + recovery === "CURSOR" + ? stream.recovery + : { + mode: "SNAPSHOT_ONLY", + snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT", + checkpointCodecId: "ReferenceRealtimeCheckpoint", + barrier: "CONNECT_BUFFER", + }, + }, + ], + eventTypes: [baseline.listEventTypes()[0]!], + bindings: { + ...bindings(), + apiOperations: apiOperations as ReturnType< + typeof bindings + >["apiOperations"], + }, + }); + }; + const snapshotRow = (method: HttpMethod, retrySemantics: RetrySemantics) => ({ + GET_REFERENCE_REALTIME_SNAPSHOT: { + ...projected.TEST_LIST_ENTITIES!, + operationId: "GET_REFERENCE_REALTIME_SNAPSHOT", + method, + retrySemantics, + }, + }); + const cases = methods.flatMap((method) => + retries.map((retrySemantics) => ({ + method, + retrySemantics, + accepted: + method === "GET" && + (retrySemantics === "SAFE" || retrySemantics === "IDEMPOTENT"), + })), + ); + + it.each(cases)( + "$method $retrySemantics as the snapshot operation: accepted=$accepted", + ({ method, retrySemantics, accepted }) => { + for (const recovery of ["CURSOR", "SNAPSHOT_ONLY"] as const) { + const install = () => + register(recovery, snapshotRow(method, retrySemantics)); + if (accepted) { + expect(install().findStream(STREAM_ID)?.recovery.mode).toBe(recovery); + } else { + expect(install, recovery).toThrow( + recovery === "CURSOR" + ? "Realtime cursor recovery profile is invalid." + : "Realtime snapshot recovery profile is invalid.", + ); + } + } + }, + ); + + it("refuses a snapshot operation id with no FE-REG-API row", () => { + expect(() => register("CURSOR", {})).toThrow( + "Realtime cursor recovery profile is invalid.", + ); + }); +}); + function bindings() { return { schemaCodecs: TEST_SCHEMA_CODECS, diff --git a/tests/unit/rest-profile-contract.test.ts b/tests/unit/rest-profile-contract.test.ts index 8451c7a3..b7c0abc7 100644 --- a/tests/unit/rest-profile-contract.test.ts +++ b/tests/unit/rest-profile-contract.test.ts @@ -63,26 +63,79 @@ describe("one installed REST auth profile registry", () => { * 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")[]) => ({ + const cookie = ( + authProfileId: string, + headers: readonly ("x-csrf-token" | "x-xsrf-token" | "x-tenant-context")[], + ) => ({ authProfileId, transport: "SAME_ORIGIN_COOKIE" as const, credentials: "include" as const, - allowedCredentialHeaders: [...csrf], - requiredCredentialHeaders: [...csrf], + allowedCredentialHeaders: [...headers], + requiredCredentialHeaders: [...headers], }); const registry = installRestAuthProfileRegistry({ ...REST_AUTH_PROFILES, COOKIE_READ: cookie("COOKIE_READ", []), COOKIE_WRITE: cookie("COOKIE_WRITE", ["x-xsrf-token"]), + COOKIE_CSRF: cookie("COOKIE_CSRF", ["x-csrf-token"]), + COOKIE_TENANT: cookie("COOKIE_TENANT", ["x-tenant-context"]), }); - const withProfile = (index: number, authProfileId: string) => { + const withProfile = ( + index: number, + authProfileId: string, + method?: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE", + ) => { const row = TEST_CONTRACT_CONTRIBUTION.http[index]!; return { ...TEST_CONTRACT_CONTRIBUTION, - http: [{ ...row, frontend: { ...row.frontend, authProfileId } }], + http: [ + { + contract: method ? { ...row.contract, method } : row.contract, + frontend: { ...row.frontend, authProfileId }, + }, + ], }; }; // TEST_CONTRACT_CONTRIBUTION.http: [list (GET), detail (GET), create (POST)] + const compose = ( + index: number, + authProfileId: string, + method?: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE", + ) => + composeContractContributions( + [withProfile(index, authProfileId, method)], + registry, + ).httpByOperationId.size; + + // 1103/07-02, 10-03. Every write method, not POST alone, and only the two + // CSRF header names count: another required header is not a CSRF proof. + it.each(["POST", "PUT", "PATCH", "DELETE"] as const)( + "refuses a %s whose cookie profile requires no CSRF header", + (method) => { + for (const authProfileId of ["COOKIE_READ", "COOKIE_TENANT"]) { + expect(() => compose(2, authProfileId, method)).toThrow( + expect.objectContaining({ + reason: `test-feature/TEST_CREATE_ENTITY: ${method} with cookie profile ${authProfileId} must require x-csrf-token or x-xsrf-token`, + }), + ); + } + }, + ); + + it.each(["POST", "PUT", "PATCH", "DELETE"] as const)( + "accepts a %s that requires x-csrf-token or x-xsrf-token", + (method) => { + expect(compose(2, "COOKIE_CSRF", method)).toBe(1); + expect(compose(2, "COOKIE_WRITE", method)).toBe(1); + }, + ); + + it.each(["GET", "HEAD"] as const)( + "accepts a cookie %s without a CSRF header", + (method) => { + expect(compose(0, "COOKIE_READ", method)).toBe(1); + }, + ); it("refuses a write whose cookie profile requires no CSRF header", () => { expect(() =>