| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { describe, expect } from "alchemy-test"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import CognitoTriggerFunctionLive, { |
| 11 | CognitoTriggerFunction, |
| 12 | } from "./trigger-handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "CognitoTriggers"); |
| 17 | |
| 18 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 19 | // over 60s on a fresh deploy under parallel-suite load. |
| 20 | const readinessPolicy = Schedule.max([ |
| 21 | Schedule.fixed("2 seconds"), |
| 22 | Schedule.recurs(75), |
| 23 | ]); |
| 24 | |
| 25 | let baseUrl: string; |
| 26 | |
| 27 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 28 | readonly status: number; |
| 29 | readonly body: string; |
| 30 | }> {} |
| 31 | |
| 32 | // Retry transient 5xx only (cold re-init, IAM / trigger-permission |
| 33 | // propagation); 4xx and assertion failures surface immediately. |
| 34 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 35 | HttpClient.execute(request).pipe( |
| 36 | Effect.flatMap((response) => |
| 37 | response.status >= 500 |
| 38 | ? response.text.pipe( |
| 39 | Effect.flatMap((body) => |
| 40 | Effect.fail( |
| 41 | new TransientUpstream({ status: response.status, body }), |
| 42 | ), |
| 43 | ), |
| 44 | ) |
| 45 | : Effect.succeed(response), |
| 46 | ), |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "TransientUpstream", |
| 49 | schedule: Schedule.max([ |
| 50 | Schedule.exponential("500 millis"), |
| 51 | Schedule.recurs(6), |
| 52 | ]), |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const decodeJwtPayload = (token: string): Record<string, unknown> => |
| 57 | JSON.parse(Buffer.from(token.split(".")[1]!, "base64url").toString("utf8")); |
| 58 | |
| 59 | describe( |
| 60 | "Cognito UserPool Triggers", |
| 61 | { |
| 62 | tags: [ |
| 63 | "provider:aws", |
| 64 | "provider:aws:cognito", |
| 65 | "provider:aws:lambda", |
| 66 | "live", |
| 67 | ], |
| 68 | }, |
| 69 | () => { |
| 70 | beforeAll( |
| 71 | Effect.gen(function* () { |
| 72 | yield* Effect.logInfo( |
| 73 | "Cognito triggers setup: destroying previous stack", |
| 74 | ); |
| 75 | yield* sharedStack.destroy(); |
| 76 | |
| 77 | yield* Effect.logInfo("Cognito triggers setup: deploying fixture"); |
| 78 | const { functionUrl } = yield* sharedStack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | return yield* CognitoTriggerFunction; |
| 81 | }).pipe(Effect.provide(CognitoTriggerFunctionLive)), |
| 82 | ); |
| 83 | |
| 84 | expect(functionUrl).toBeTruthy(); |
| 85 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 86 | const readinessUrl = `${baseUrl}/config`; |
| 87 | |
| 88 | yield* Effect.logInfo( |
| 89 | `Cognito triggers setup: probing readiness at ${readinessUrl}`, |
| 90 | ); |
| 91 | yield* HttpClient.get(readinessUrl).pipe( |
| 92 | Effect.flatMap((response) => |
| 93 | response.status === 200 |
| 94 | ? Effect.succeed(response) |
| 95 | : Effect.fail( |
| 96 | new Error(`Function not ready: ${response.status}`), |
| 97 | ), |
| 98 | ), |
| 99 | Effect.tapError((error) => |
| 100 | Effect.logWarning( |
| 101 | `Cognito triggers setup: fixture not ready yet (${String(error)})`, |
| 102 | ), |
| 103 | ), |
| 104 | Effect.retry({ schedule: readinessPolicy }), |
| 105 | ); |
| 106 | }), |
| 107 | { timeout: 240_000 }, |
| 108 | ); |
| 109 | |
| 110 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 111 | |
| 112 | describe("UserPoolTriggerEventSource", () => { |
| 113 | test.provider( |
| 114 | "preSignUp auto-confirms sign-up; preTokenGeneration stamps a claim", |
| 115 | (_stack) => |
| 116 | Effect.gen(function* () { |
| 117 | const config = (yield* send( |
| 118 | HttpClientRequest.get(`${baseUrl}/config`), |
| 119 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 120 | userPoolId: string; |
| 121 | clientId: string; |
| 122 | }; |
| 123 | |
| 124 | const response = (yield* send( |
| 125 | HttpClientRequest.post( |
| 126 | `${baseUrl}/sign-up-flow?username=trigger-user`, |
| 127 | ), |
| 128 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 129 | userConfirmed: boolean | undefined; |
| 130 | userStatus: string | undefined; |
| 131 | emailVerified: string | undefined; |
| 132 | idToken: string | undefined; |
| 133 | }; |
| 134 | |
| 135 | // PreSignUp auto-confirm: the sign-up response itself reports the |
| 136 | // user as confirmed, and the pool shows CONFIRMED — no |
| 137 | // confirmation code was ever entered. |
| 138 | expect(response.userConfirmed).toBe(true); |
| 139 | expect(response.userStatus).toBe("CONFIRMED"); |
| 140 | expect(response.emailVerified).toBe("true"); |
| 141 | |
| 142 | // Password auth succeeded immediately after sign-up, proving the |
| 143 | // account was usable without confirmation. |
| 144 | expect(response.idToken).toBeTruthy(); |
| 145 | |
| 146 | // PreTokenGeneration: the ID token carries the claim the trigger |
| 147 | // handler stamped in, and is a real JWT issued by our pool. |
| 148 | const payload = decodeJwtPayload(response.idToken!); |
| 149 | expect(payload.alchemyTrigger).toBe("pre-token-generation"); |
| 150 | expect(payload.iss).toContain(`/${config.userPoolId}`); |
| 151 | expect(payload["cognito:username"]).toBe("trigger-user"); |
| 152 | expect(payload.aud).toBe(config.clientId); |
| 153 | }), |
| 154 | { timeout: 120_000 }, |
| 155 | ); |
| 156 | }); |
| 157 | }, |
| 158 | ); |