The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import { testName } from "../../names.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers(), state: Cloudflare.state() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | test.provider( |
| 16 | "create, update rules, and delete group", |
| 17 | (stack) => |
| 18 | Effect.gen(function* () { |
| 19 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 20 | |
| 21 | yield* stack.destroy(); |
| 22 | |
| 23 | const group = yield* stack.deploy( |
| 24 | Effect.gen(function* () { |
| 25 | return yield* Cloudflare.Access.Group("BasicGroup", { |
| 26 | include: [{ emailDomain: { domain: "example.com" } }], |
| 27 | }); |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | expect(group.groupId).toBeDefined(); |
| 32 | expect(group.accountId).toEqual(accountId); |
| 33 | expect(group.name).toBeDefined(); |
| 34 | |
| 35 | const actual = yield* zeroTrust.getAccessGroupForAccount({ |
| 36 | accountId, |
| 37 | groupId: group.groupId, |
| 38 | }); |
| 39 | expect(actual.id).toEqual(group.groupId); |
| 40 | expect(actual.name).toEqual(group.name); |
| 41 | expect(actual.include?.length).toEqual(1); |
| 42 | |
| 43 | // Update — add an include arm plus exclude/require rule groups. The |
| 44 | // group must converge in place (same id). |
| 45 | const updated = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | return yield* Cloudflare.Access.Group("BasicGroup", { |
| 48 | include: [{ emailDomain: { domain: "example.com" } }, { geo: { countryCode: "US" } }], |
| 49 | exclude: [{ email: { email: "intern@example.com" } }], |
| 50 | require: [{ emailDomain: { domain: "example.com" } }], |
| 51 | }); |
| 52 | }), |
| 53 | ); |
| 54 | expect(updated.groupId).toEqual(group.groupId); |
| 55 | |
| 56 | const afterUpdate = yield* zeroTrust.getAccessGroupForAccount({ |
| 57 | accountId, |
| 58 | groupId: group.groupId, |
| 59 | }); |
| 60 | expect(afterUpdate.include?.length).toEqual(2); |
| 61 | expect(afterUpdate.exclude?.length).toEqual(1); |
| 62 | expect(afterUpdate.require?.length).toEqual(1); |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const afterDestroy = yield* zeroTrust |
| 67 | .getAccessGroupForAccount({ accountId, groupId: group.groupId }) |
| 68 | .pipe(Effect.catchTag("AccessGroupNotFound", () => Effect.succeed(undefined))); |
| 69 | expect(afterDestroy?.id ?? undefined).toBeUndefined(); |
| 70 | }).pipe(logLevel), |
| 71 | { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] }, |
| 72 | ); |
| 73 | |
| 74 | test.provider( |
| 75 | "rename updates the group in place", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | |
| 82 | const group = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* Cloudflare.Access.Group("RenameGroup", { |
| 85 | name: testName("alchemy-test-access-group-rename-a"), |
| 86 | include: [{ everyone: {} }], |
| 87 | }); |
| 88 | }), |
| 89 | ); |
| 90 | expect(group.name).toEqual(testName("alchemy-test-access-group-rename-a")); |
| 91 | |
| 92 | const renamed = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* Cloudflare.Access.Group("RenameGroup", { |
| 95 | name: testName("alchemy-test-access-group-rename-b"), |
| 96 | include: [{ everyone: {} }], |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | expect(renamed.groupId).toEqual(group.groupId); |
| 101 | expect(renamed.name).toEqual(testName("alchemy-test-access-group-rename-b")); |
| 102 | |
| 103 | const actual = yield* zeroTrust.getAccessGroupForAccount({ |
| 104 | accountId, |
| 105 | groupId: group.groupId, |
| 106 | }); |
| 107 | expect(actual.name).toEqual(testName("alchemy-test-access-group-rename-b")); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | }).pipe(logLevel), |
| 111 | { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] }, |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "list enumerates the deployed access group", |
| 116 | (stack) => |
| 117 | Effect.gen(function* () { |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const group = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Cloudflare.Access.Group("ListGroup", { |
| 123 | include: [{ emailDomain: { domain: "example.com" } }], |
| 124 | }); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | const provider = yield* Provider.findProvider(Cloudflare.Access.Group); |
| 129 | const all = yield* provider.list(); |
| 130 | |
| 131 | const found = all.find((g) => g.groupId === group.groupId); |
| 132 | expect(found).toBeDefined(); |
| 133 | expect(found?.accountId).toEqual(group.accountId); |
| 134 | expect(found?.name).toEqual(group.name); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | }).pipe(logLevel), |
| 138 | { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] }, |
| 139 | ); |
| 140 | |
| 141 | test.provider( |
| 142 | "group can be referenced from an access policy", |
| 143 | (stack) => |
| 144 | Effect.gen(function* () { |
| 145 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const { group, policy } = yield* stack.deploy( |
| 150 | Effect.gen(function* () { |
| 151 | const group = yield* Cloudflare.Access.Group("PolicyGroup", { |
| 152 | include: [{ emailDomain: { domain: "example.com" } }], |
| 153 | }); |
| 154 | const policy = yield* Cloudflare.Access.Policy("GroupPolicy", { |
| 155 | decision: "allow", |
| 156 | include: [{ group: { id: group.groupId } }], |
| 157 | }); |
| 158 | return { group, policy }; |
| 159 | }), |
| 160 | ); |
| 161 | |
| 162 | const actual = yield* zeroTrust.getAccessPolicy({ accountId, policyId: policy.policyId }); |
| 163 | const includes = (actual.include ?? []) as Array<{ group?: { id: string } }>; |
| 164 | expect(includes[0]?.group?.id).toEqual(group.groupId); |
| 165 | |
| 166 | yield* stack.destroy(); |
| 167 | }).pipe(logLevel), |
| 168 | { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] }, |
| 169 | ); |