The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 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 Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import VerifiedPermissionsTestFunctionLive, { VerifiedPermissionsTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "VerifiedPermissionsBindings"); |
| 15 | |
| 16 | const readinessPolicy = Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]); |
| 17 | |
| 18 | let baseUrl: string; |
| 19 | |
| 20 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 21 | readonly status: number; |
| 22 | readonly body: string; |
| 23 | }> {} |
| 24 | |
| 25 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 26 | HttpClient.execute(request).pipe( |
| 27 | Effect.flatMap((response) => |
| 28 | response.status >= 500 |
| 29 | ? response.text.pipe( |
| 30 | Effect.flatMap((body) => |
| 31 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 32 | ), |
| 33 | ) |
| 34 | : Effect.succeed(response), |
| 35 | ), |
| 36 | Effect.retry({ |
| 37 | while: (e) => e._tag === "TransientUpstream", |
| 38 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | describe( |
| 43 | "VerifiedPermissions Bindings", |
| 44 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:verifiedpermissions", "live"] }, |
| 45 | () => { |
| 46 | beforeAll( |
| 47 | Effect.gen(function* () { |
| 48 | yield* sharedStack.destroy(); |
| 49 | const { functionUrl } = yield* sharedStack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | return yield* VerifiedPermissionsTestFunction; |
| 52 | }).pipe(Effect.provide(VerifiedPermissionsTestFunctionLive)), |
| 53 | ); |
| 54 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 55 | // wait for the fresh function URL to serve (AVP is eventually consistent |
| 56 | // — policies take a moment to be evaluatable after CreatePolicy) |
| 57 | yield* send(HttpClientRequest.get(`${baseUrl}/info`)).pipe( |
| 58 | Effect.retry({ schedule: readinessPolicy }), |
| 59 | ); |
| 60 | }), |
| 61 | { timeout: 240_000 }, |
| 62 | ); |
| 63 | afterAll(sharedStack.destroy(), { timeout: 60_000 }); |
| 64 | |
| 65 | describe("IsAuthorized", () => { |
| 66 | test.provider("allows alice to view a photo", (_stack) => |
| 67 | Effect.gen(function* () { |
| 68 | const response = yield* send( |
| 69 | HttpClientRequest.get(`${baseUrl}/authorize?user=alice`), |
| 70 | ).pipe( |
| 71 | Effect.retry({ |
| 72 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]), |
| 73 | }), |
| 74 | ); |
| 75 | const body = (yield* response.json) as { decision: string }; |
| 76 | expect(body.decision).toBe("ALLOW"); |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | test.provider("denies bob (no matching permit policy)", (_stack) => |
| 81 | Effect.gen(function* () { |
| 82 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/authorize?user=bob`)); |
| 83 | const body = (yield* response.json) as { decision: string }; |
| 84 | expect(body.decision).toBe("DENY"); |
| 85 | }), |
| 86 | ); |
| 87 | }); |
| 88 | |
| 89 | describe("BatchIsAuthorized", () => { |
| 90 | test.provider("returns ALLOW for alice and DENY for bob", (_stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/batch`)).pipe( |
| 93 | Effect.retry({ |
| 94 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]), |
| 95 | }), |
| 96 | ); |
| 97 | const body = (yield* response.json) as { decisions: string[] }; |
| 98 | expect(body.decisions).toEqual(["ALLOW", "DENY"]); |
| 99 | }), |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | describe("BatchIsAuthorizedWithToken", () => { |
| 104 | test.provider("rejects a malformed token with a typed ValidationException", (_stack) => |
| 105 | Effect.gen(function* () { |
| 106 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/batch-token`)); |
| 107 | const body = (yield* response.json) as { tag: string }; |
| 108 | // the request reaches AVP (IAM allowed) and fails Cedar-side token |
| 109 | // validation — proving the binding + IAM wiring end-to-end |
| 110 | expect(body.tag).toBe("ValidationException"); |
| 111 | }), |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | describe("GetPolicies", () => { |
| 116 | test.provider("batchGetPolicy returns the AllowAlice policy", (_stack) => |
| 117 | Effect.gen(function* () { |
| 118 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/policies`)); |
| 119 | const body = (yield* response.json) as { ids: string[]; types: string[]; errors: number }; |
| 120 | expect(body.ids).toHaveLength(1); |
| 121 | expect(body.types).toEqual(["STATIC"]); |
| 122 | expect(body.errors).toBe(0); |
| 123 | }), |
| 124 | ); |
| 125 | }); |
| 126 | }, |
| 127 | ); |