The best way to control Claude Code, Codex and more from any computer or phone

main
ts 100 lines 3.56 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as recaptchaenterprise from "@distilled.cloud/gcp/recaptchaenterprise_v1";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import RecaptchaBindingsHost, { Signup } from "./fixtures/bindings-host.ts";
10
11 const testOptions = { providers: GCP.providers() };
12 const { test, beforeAll, afterAll } = Test.make(testOptions);
13 const sharedStack = Core.scratchStack(testOptions, "RecaptchaBindings");
14
15 let baseUrl: string;
16 let hostAccount: string;
17 let project: string;
18 let keyName: string;
19 let keyId: string;
20
21 describe(
22 "RecaptchaEnterprise Bindings",
23 {
24 tags: ["provider:gcp", "provider:gcp:recaptchaenterprise", "provider:gcp:run", "live"],
25 },
26 () => {
27 beforeAll(
28 Effect.gen(function* () {
29 yield* sharedStack.destroy();
30 const out = yield* sharedStack.deploy(
31 Effect.gen(function* () {
32 const host = yield* RecaptchaBindingsHost;
33 const key = yield* Signup;
34 return {
35 uri: host.uri,
36 serviceAccount: host.serviceAccount,
37 name: key.name,
38 keyId: key.keyId,
39 project: key.project,
40 };
41 }),
42 );
43 baseUrl = out.uri!;
44 hostAccount = out.serviceAccount!;
45 keyName = out.name;
46 keyId = out.keyId;
47 project = out.project;
48 }),
49 { timeout: 900_000 },
50 );
51
52 afterAll(sharedStack.destroy(), { timeout: 600_000 });
53
54 describe("CreateAssessment", () => {
55 test.provider(
56 "assesses a token against the key as the host's service account, granted recaptchaenterprise.agent on the project",
57 (_stack) =>
58 Effect.gen(function* () {
59 const assessment =
60 yield* expectProbe<recaptchaenterprise.GoogleCloudRecaptchaenterpriseV1Assessment>(
61 baseUrl,
62 "createAssessment",
63 );
64 expect(assessment.name).toEqual(expect.stringContaining("/assessments/"));
65 expect(assessment.event?.siteKey).toEqual(keyId);
66 expect(assessment.event?.expectedAction).toEqual("login");
67 // The token is not a real reCAPTCHA token.
68 expect(assessment.tokenProperties?.valid).toEqual(false);
69
70 // The assessment exists server-side: annotating it succeeds.
71 yield* recaptchaenterprise.annotateProjectsAssessments({
72 name: assessment.name ?? "",
73 body: { annotation: "LEGITIMATE" },
74 });
75
76 // reCAPTCHA keys have no IAM policy of their own.
77 const policy = yield* crm.getIamPolicyProjects({
78 resource: `projects/${project}`,
79 body: { options: { requestedPolicyVersion: 3 } },
80 });
81 const roles = (policy.bindings ?? [])
82 .filter((binding) =>
83 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
84 )
85 .map((binding) => ({
86 role: binding.role,
87 condition: binding.condition,
88 }));
89 expect(roles).toEqual([
90 { role: "roles/recaptchaenterprise.agent", condition: undefined },
91 ]);
92 }),
93 {
94 tags: ["provider:gcp", "provider:gcp:recaptchaenterprise", "live"],
95 timeout: 600_000,
96 },
97 );
98 });
99 },
100 );