|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | + |
| 3 | +import { ORG_SELECTOR_HEADER } from "../auth/organization"; |
| 4 | +import { classifyApiOrgScope, isApiPath, prepareApiOrgScope } from "./org-scope"; |
| 5 | + |
| 6 | +// The worker-boundary seam that turns the URL's first path segment into the org |
| 7 | +// scope for the `/api/*` plane. The wire form is `/<slug>/api/...` (or the |
| 8 | +// legacy `/<org_id>/api/...`); the boundary strips the prefix to the bare |
| 9 | +// `/api/...` the app handler routes and pins the selector in an internal header. |
| 10 | +// Org rides ONLY in the URL — a client-supplied selector header is never trusted. |
| 11 | + |
| 12 | +describe("isApiPath", () => { |
| 13 | + it("matches the bare API plane", () => { |
| 14 | + expect(isApiPath("/api")).toBe(true); |
| 15 | + expect(isApiPath("/api/executions")).toBe(true); |
| 16 | + expect(isApiPath("/api/billing/customer")).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("does not match org-scoped, MCP, or console paths", () => { |
| 20 | + expect(isApiPath("/acme-corp/api/executions")).toBe(false); |
| 21 | + expect(isApiPath("/mcp")).toBe(false); |
| 22 | + expect(isApiPath("/settings")).toBe(false); |
| 23 | + expect(isApiPath("/apiary")).toBe(false); // not `/api` nor `/api/` |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("classifyApiOrgScope", () => { |
| 28 | + it("classifies a slug-scoped API path", () => { |
| 29 | + expect(classifyApiOrgScope("/acme-corp/api/billing/customer")).toEqual({ |
| 30 | + selector: "acme-corp", |
| 31 | + barePath: "/api/billing/customer", |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("classifies the legacy org-id-scoped form", () => { |
| 36 | + expect(classifyApiOrgScope("/org_01ABCDEF/api/executions")).toEqual({ |
| 37 | + selector: "org_01ABCDEF", |
| 38 | + barePath: "/api/executions", |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns null for a bare API path (no selector segment)", () => { |
| 43 | + expect(classifyApiOrgScope("/api/billing/customer")).toBeNull(); |
| 44 | + expect(classifyApiOrgScope("/api")).toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns null when the second segment is not `api`", () => { |
| 48 | + expect(classifyApiOrgScope("/acme-corp/mcp")).toBeNull(); |
| 49 | + expect(classifyApiOrgScope("/settings/mcp")).toBeNull(); |
| 50 | + expect(classifyApiOrgScope("/acme-corp")).toBeNull(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("prepareApiOrgScope", () => { |
| 55 | + it("rewrites a slug-scoped request to the bare path and pins the selector", () => { |
| 56 | + const out = prepareApiOrgScope( |
| 57 | + new Request("https://executor.test/acme-corp/api/billing/customer", { method: "POST" }), |
| 58 | + ); |
| 59 | + expect(new URL(out.url).pathname).toBe("/api/billing/customer"); |
| 60 | + expect(out.headers.get(ORG_SELECTOR_HEADER)).toBe("acme-corp"); |
| 61 | + expect(out.method).toBe("POST"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("pins the legacy org-id selector", () => { |
| 65 | + const out = prepareApiOrgScope( |
| 66 | + new Request("https://executor.test/org_01ABCDEF/api/executions"), |
| 67 | + ); |
| 68 | + expect(new URL(out.url).pathname).toBe("/api/executions"); |
| 69 | + expect(out.headers.get(ORG_SELECTOR_HEADER)).toBe("org_01ABCDEF"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("strips a client-supplied selector header on a bare API path", () => { |
| 73 | + // Org may come ONLY from the URL — a client cannot smuggle a different org |
| 74 | + // by setting the internal selector header on a bare `/api/...` request. |
| 75 | + const out = prepareApiOrgScope( |
| 76 | + new Request("https://executor.test/api/billing/customer", { |
| 77 | + headers: { [ORG_SELECTOR_HEADER]: "org_attacker" }, |
| 78 | + }), |
| 79 | + ); |
| 80 | + expect(new URL(out.url).pathname).toBe("/api/billing/customer"); |
| 81 | + expect(out.headers.has(ORG_SELECTOR_HEADER)).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it("leaves a bare API path without a selector header untouched", () => { |
| 85 | + const req = new Request("https://executor.test/api/executions"); |
| 86 | + expect(prepareApiOrgScope(req)).toBe(req); |
| 87 | + }); |
| 88 | + |
| 89 | + it("leaves a non-API path untouched", () => { |
| 90 | + const req = new Request("https://executor.test/settings"); |
| 91 | + expect(prepareApiOrgScope(req)).toBe(req); |
| 92 | + }); |
| 93 | +}); |
0 commit comments