The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | MAX_POLICY_PERMISSION_GROUPS, |
| 3 | tokenPolicies, |
| 4 | type PermissionGroup, |
| 5 | } from "@alchemy.run/cloudflare/Auth/TokenPolicy"; |
| 6 | import { describe, expect, it } from "@alchemy.run/test"; |
| 7 | |
| 8 | const group = (id: string, scope: string): PermissionGroup => ({ |
| 9 | id, |
| 10 | name: id, |
| 11 | scopes: [scope], |
| 12 | selectable: true, |
| 13 | }); |
| 14 | |
| 15 | const accountGroups = (count: number) => |
| 16 | Array.from({ length: count }, (_, index) => |
| 17 | group(`account-${index}`, "com.cloudflare.api.account"), |
| 18 | ); |
| 19 | |
| 20 | describe( |
| 21 | "Cloudflare token policies", |
| 22 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:auth", "local"] }, |
| 23 | () => { |
| 24 | it("buckets permission groups by resource scope", () => { |
| 25 | const policies = tokenPolicies(["acct"], "user", [ |
| 26 | group("a", "com.cloudflare.api.account"), |
| 27 | group("z", "com.cloudflare.api.account.zone"), |
| 28 | group("u", "com.cloudflare.api.user"), |
| 29 | group("dropped", "com.cloudflare.edge.worker.script"), |
| 30 | ]); |
| 31 | |
| 32 | expect(policies).toEqual([ |
| 33 | { |
| 34 | effect: "allow", |
| 35 | permissionGroups: [{ id: "a" }], |
| 36 | resources: { "com.cloudflare.api.account.acct": "*" }, |
| 37 | }, |
| 38 | { |
| 39 | effect: "allow", |
| 40 | permissionGroups: [{ id: "z" }], |
| 41 | resources: { "com.cloudflare.api.account.zone.*": "*" }, |
| 42 | }, |
| 43 | { |
| 44 | effect: "allow", |
| 45 | permissionGroups: [{ id: "u" }], |
| 46 | resources: { "com.cloudflare.api.user.user": "*" }, |
| 47 | }, |
| 48 | ]); |
| 49 | }); |
| 50 | |
| 51 | it("splits a scope with more than 300 groups across policies on the same resources", () => { |
| 52 | // The live account catalog has 303 account-scoped groups, which |
| 53 | // Cloudflare rejects in a single policy. |
| 54 | const policies = tokenPolicies(["acct"], "user", accountGroups(303)); |
| 55 | |
| 56 | expect(policies.map((policy) => policy.permissionGroups.length)).toEqual([ |
| 57 | MAX_POLICY_PERMISSION_GROUPS, |
| 58 | 3, |
| 59 | ]); |
| 60 | expect(policies.every((policy) => policy.resources["com.cloudflare.api.account.acct"])).toBe( |
| 61 | true, |
| 62 | ); |
| 63 | expect( |
| 64 | new Set(policies.flatMap((policy) => policy.permissionGroups.map(({ id }) => id))).size, |
| 65 | ).toBe(303); |
| 66 | }); |
| 67 | |
| 68 | it("keeps exactly 300 groups in one policy", () => { |
| 69 | expect(tokenPolicies(["acct"], "user", accountGroups(300))).toHaveLength(1); |
| 70 | }); |
| 71 | }, |
| 72 | ); |