The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 SESTestFunctionLive, { SESTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "SESBindings"); |
| 15 | |
| 16 | // A verified from-address on the account. It does NOT unlock a successful |
| 17 | // `SendEmail` — that binding is scoped to the identity the fixture binds — but |
| 18 | // the suppression-list write plane needs production access, and an account |
| 19 | // that has a verified sender is invariably out of the sandbox. |
| 20 | const VERIFIED_FROM = process.env.AWS_TEST_SES_FROM; |
| 21 | |
| 22 | // Names an existing custom verification email template. Creating one requires |
| 23 | // a verified sender, so the fixture cannot declare it — pass a template you |
| 24 | // created out of band to exercise the real send. |
| 25 | const CVE_TEMPLATE = process.env.AWS_TEST_SES_CVE_TEMPLATE; |
| 26 | |
| 27 | // Recipient for the real custom-verification send. Defaults to the mailbox |
| 28 | // simulator; override only with an address you own. |
| 29 | const CVE_RECIPIENT = process.env.AWS_TEST_SES_CVE_RECIPIENT; |
| 30 | |
| 31 | // Never skips a test — tightens it. With Virtual Deliverability Manager |
| 32 | // enabled the insight bindings must return real data rather than the typed |
| 33 | // rejection a VDM-less account gives. |
| 34 | const VDM_ENABLED = !!process.env.AWS_TEST_SES_VDM; |
| 35 | |
| 36 | // The SES mailbox simulator accepts mail without affecting reputation. |
| 37 | const SIMULATOR = "success@simulator.amazonses.com"; |
| 38 | |
| 39 | // A syntactically valid address at the fixture's (unverified) domain |
| 40 | // identity — SES rejects it with the typed MessageRejected tag in sandbox. |
| 41 | const UNVERIFIED_FROM = "noreply@ses-bindings.alchemy-test.example.com"; |
| 42 | |
| 43 | // Deterministic address the suppression-list tests add and remove. The test |
| 44 | // ends by deleting it, so repeated runs leave no residue. |
| 45 | const SUPPRESSED_ADDRESS = "suppressed@ses-bindings.alchemy-test.example.com"; |
| 46 | |
| 47 | // The tags SES raises about a REQUEST — a bad argument, a missing object, or |
| 48 | // an unentitled account. A binding that failed to attach its IAM action would |
| 49 | // answer AccessDeniedException instead, so asserting membership here (rather |
| 50 | // than `typeof error === "string"`) is what makes these tests able to fail. |
| 51 | const REQUEST_TAGS = ["BadRequestException", "NotFoundException"]; |
| 52 | |
| 53 | // SendCustomVerificationEmail additionally rejects an unverified sender and a |
| 54 | // sandboxed account through its own tags. |
| 55 | const CVE_REQUEST_TAGS = [ |
| 56 | ...REQUEST_TAGS, |
| 57 | "MessageRejected", |
| 58 | "MailFromDomainNotVerifiedException", |
| 59 | "SendingPausedException", |
| 60 | "LimitExceededException", |
| 61 | ]; |
| 62 | |
| 63 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 64 | |
| 65 | let baseUrl: string; |
| 66 | |
| 67 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 68 | readonly status: number; |
| 69 | readonly body: string; |
| 70 | }> {} |
| 71 | |
| 72 | // Retry transient 5xx only; a genuine 4xx/assertion failure surfaces |
| 73 | // immediately. |
| 74 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 75 | HttpClient.execute(request).pipe( |
| 76 | Effect.flatMap((response) => |
| 77 | response.status >= 500 |
| 78 | ? response.text.pipe( |
| 79 | Effect.flatMap((body) => |
| 80 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 81 | ), |
| 82 | ) |
| 83 | : Effect.succeed(response), |
| 84 | ), |
| 85 | Effect.retry({ |
| 86 | while: (e) => e._tag === "TransientUpstream", |
| 87 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | describe( |
| 92 | "SES Bindings", |
| 93 | { |
| 94 | tags: ["provider:aws", "provider:aws:batch", "provider:aws:lambda", "provider:aws:ses", "live"], |
| 95 | }, |
| 96 | () => { |
| 97 | beforeAll( |
| 98 | Effect.gen(function* () { |
| 99 | yield* Effect.logInfo("SES test setup: destroying previous resources"); |
| 100 | yield* sharedStack.destroy(); |
| 101 | |
| 102 | yield* Effect.logInfo("SES test setup: deploying fixture"); |
| 103 | const { functionUrl } = yield* sharedStack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | return yield* SESTestFunction; |
| 106 | }).pipe(Effect.provide(SESTestFunctionLive)), |
| 107 | ); |
| 108 | |
| 109 | expect(functionUrl).toBeTruthy(); |
| 110 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 111 | const readinessUrl = `${baseUrl}/health`; |
| 112 | |
| 113 | yield* Effect.logInfo(`SES test setup: probing readiness at ${readinessUrl}`); |
| 114 | yield* HttpClient.get(readinessUrl).pipe( |
| 115 | Effect.flatMap((response) => |
| 116 | response.status === 200 |
| 117 | ? Effect.succeed(response) |
| 118 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 119 | ), |
| 120 | Effect.tapError((error) => |
| 121 | Effect.logWarning(`SES test setup: fixture not ready yet (${String(error)})`), |
| 122 | ), |
| 123 | Effect.retry({ schedule: readinessPolicy }), |
| 124 | ); |
| 125 | |
| 126 | // The freshly attached role policy takes a while to propagate through |
| 127 | // IAM — poll the send route until SES stops answering AccessDenied so |
| 128 | // the tests below observe the real (sandbox) behavior. |
| 129 | yield* HttpClient.execute( |
| 130 | HttpClientRequest.post( |
| 131 | `${baseUrl}/send-simple?from=${encodeURIComponent(UNVERIFIED_FROM)}`, |
| 132 | ), |
| 133 | ).pipe( |
| 134 | Effect.flatMap((r) => r.json), |
| 135 | Effect.flatMap((body) => |
| 136 | (body as { error?: string }).error === "AccessDeniedException" |
| 137 | ? Effect.fail(new Error("IAM policy not propagated yet")) |
| 138 | : Effect.succeed(body), |
| 139 | ), |
| 140 | Effect.tapError((error) => |
| 141 | Effect.logWarning(`SES test setup: send not authorized yet (${String(error)})`), |
| 142 | ), |
| 143 | Effect.retry({ schedule: readinessPolicy }), |
| 144 | ); |
| 145 | }), |
| 146 | { timeout: 240_000 }, |
| 147 | ); |
| 148 | |
| 149 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 150 | |
| 151 | describe("SendEmail", () => { |
| 152 | test.provider( |
| 153 | "sandbox: unverified sender surfaces the typed MessageRejected tag through the binding", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | const response = (yield* send( |
| 157 | HttpClientRequest.post( |
| 158 | `${baseUrl}/send-simple?from=${encodeURIComponent(UNVERIFIED_FROM)}`, |
| 159 | ), |
| 160 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 161 | messageId?: string; |
| 162 | error?: string; |
| 163 | message?: string; |
| 164 | }; |
| 165 | |
| 166 | // Sandbox + unverified FROM identity: SES rejects the message with |
| 167 | // the typed MessageRejected error ("Email address is not |
| 168 | // verified"). This proves the binding wires IAM + request |
| 169 | // marshalling correctly all the way into the deployed Lambda. |
| 170 | // SendingPausedException: the testing account's SES sending is |
| 171 | // paused. Still a typed SES error through the binding (not IAM |
| 172 | // AccessDenied / an unknown tag) — the binding is wired. The |
| 173 | // sandbox MessageRejected path is what we get when sending is on. |
| 174 | expect(["MessageRejected", "SendingPausedException"]).toContain(response.error); |
| 175 | if (response.error === "MessageRejected") { |
| 176 | expect(response.message).toContain("not verified"); |
| 177 | } |
| 178 | }), |
| 179 | { exclusive: "aws:ses-account" }, |
| 180 | ); |
| 181 | |
| 182 | test.provider( |
| 183 | "sandbox: templated send is rejected with the same typed tag", |
| 184 | (_stack) => |
| 185 | Effect.gen(function* () { |
| 186 | const response = (yield* send( |
| 187 | HttpClientRequest.post( |
| 188 | `${baseUrl}/send-template?from=${encodeURIComponent(UNVERIFIED_FROM)}`, |
| 189 | ), |
| 190 | ).pipe(Effect.flatMap((r) => r.json))) as { messageId?: string; error?: string }; |
| 191 | expect(["MessageRejected", "SendingPausedException"]).toContain(response.error); |
| 192 | }), |
| 193 | { exclusive: "aws:ses-account" }, |
| 194 | ); |
| 195 | |
| 196 | test.provider( |
| 197 | "a binding declared without a configuration set is granted the same send", |
| 198 | (_stack) => |
| 199 | Effect.gen(function* () { |
| 200 | // The fixture binds SendEmail twice: once with a configuration set |
| 201 | // and once without (`SES.SendEmail(identity)`). The no-config-set |
| 202 | // grant is a separate IAM policy, so it needs its own proof that it |
| 203 | // reaches SES — otherwise a regression in the one-argument overload |
| 204 | // would go unnoticed. |
| 205 | const response = (yield* send( |
| 206 | HttpClientRequest.post( |
| 207 | `${baseUrl}/send-plain?from=${encodeURIComponent(UNVERIFIED_FROM)}`, |
| 208 | ), |
| 209 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 210 | messageId?: string; |
| 211 | error?: string; |
| 212 | message?: string; |
| 213 | }; |
| 214 | |
| 215 | // Same sandbox rejection as the config-set path — crucially NOT |
| 216 | // AccessDenied, which is what a missing/misscoped policy looks like. |
| 217 | expect(response.error).not.toBe("AccessDeniedException"); |
| 218 | expect(["MessageRejected", "SendingPausedException"]).toContain(response.error); |
| 219 | if (response.error === "MessageRejected") { |
| 220 | expect(response.message).toContain("not verified"); |
| 221 | } |
| 222 | }), |
| 223 | { exclusive: "aws:ses-account" }, |
| 224 | ); |
| 225 | |
| 226 | // `SendEmail` is identity-scoped: the binding grants ses:SendEmail only on |
| 227 | // the ARN of the identity it was bound to (plus that domain's addresses, |
| 228 | // its templates, and the configuration set). A sender outside that |
| 229 | // identity is refused by IAM before SES ever evaluates the message — |
| 230 | // including one that is verified on the account. Verified in a live |
| 231 | // production account with a verified sender, where the send returns |
| 232 | // AccessDeniedException rather than a MessageId. |
| 233 | // |
| 234 | // Exercising a genuine successful send therefore needs an identity the |
| 235 | // FIXTURE binds and that is really verified, i.e. a domain under our |
| 236 | // control with DKIM published — the setup Receiving.smoke.test.ts builds |
| 237 | // on the standing Cloudflare test zone. AWS_TEST_SES_FROM alone cannot |
| 238 | // grant it. |
| 239 | test.provider( |
| 240 | "a sender outside the bound identity is denied by the scoped IAM policy", |
| 241 | (_stack) => |
| 242 | Effect.gen(function* () { |
| 243 | const outsider = encodeURIComponent("sender@not-the-bound-domain.test"); |
| 244 | const response = (yield* send( |
| 245 | HttpClientRequest.post(`${baseUrl}/send-simple?from=${outsider}`), |
| 246 | ).pipe(Effect.flatMap((r) => r.json))) as { messageId?: string; error?: string }; |
| 247 | |
| 248 | // SendingPaused is evaluated before IAM on a paused account — |
| 249 | // still typed, still not a successful send. |
| 250 | expect(["AccessDeniedException", "SendingPausedException"]).toContain(response.error); |
| 251 | expect(response.messageId).toBeUndefined(); |
| 252 | }), |
| 253 | { exclusive: "aws:ses-account" }, |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | describe("SendBulkEmail", () => { |
| 258 | test.provider( |
| 259 | "sandbox: bulk templated send from an unverified sender is rejected with the typed tag", |
| 260 | (_stack) => |
| 261 | Effect.gen(function* () { |
| 262 | const response = (yield* send( |
| 263 | HttpClientRequest.post( |
| 264 | `${baseUrl}/send-bulk?from=${encodeURIComponent(UNVERIFIED_FROM)}`, |
| 265 | ), |
| 266 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 267 | error?: string; |
| 268 | results?: { status?: string; messageId?: string }[]; |
| 269 | }; |
| 270 | |
| 271 | // Sandbox + unverified FROM: SES rejects either the whole request |
| 272 | // (typed MessageRejected) or the individual entry |
| 273 | // (Status MESSAGE_REJECTED) — both prove the binding wires IAM and |
| 274 | // request marshalling into the deployed Lambda. |
| 275 | if (response.error !== undefined) { |
| 276 | expect(response.error).toBe("MessageRejected"); |
| 277 | } else { |
| 278 | expect(response.results?.[0]?.status).toBe("MESSAGE_REJECTED"); |
| 279 | } |
| 280 | }), |
| 281 | { exclusive: "aws:ses-account" }, |
| 282 | ); |
| 283 | }); |
| 284 | |
| 285 | describe("RenderEmailTemplate", () => { |
| 286 | test.provider( |
| 287 | "renders the bound template server-side with personalization data", |
| 288 | (_stack) => |
| 289 | Effect.gen(function* () { |
| 290 | const response = (yield* send( |
| 291 | HttpClientRequest.post(`${baseUrl}/render-template`), |
| 292 | ).pipe(Effect.flatMap((r) => r.json))) as { rendered?: string; error?: string }; |
| 293 | expect(response.error).toBeUndefined(); |
| 294 | expect(response.rendered).toContain("Hello, Ada!"); |
| 295 | }), |
| 296 | { exclusive: "aws:ses-account" }, |
| 297 | ); |
| 298 | }); |
| 299 | |
| 300 | describe("GetAccount", () => { |
| 301 | test.provider( |
| 302 | "reads the account's sending status and quota", |
| 303 | (_stack) => |
| 304 | Effect.gen(function* () { |
| 305 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/account`)).pipe( |
| 306 | Effect.flatMap((r) => r.json), |
| 307 | )) as { |
| 308 | sendingEnabled?: boolean; |
| 309 | productionAccess?: boolean; |
| 310 | max24HourSend?: number; |
| 311 | error?: string; |
| 312 | }; |
| 313 | expect(response.error).toBeUndefined(); |
| 314 | expect(typeof response.sendingEnabled).toBe("boolean"); |
| 315 | expect(typeof response.productionAccess).toBe("boolean"); |
| 316 | }), |
| 317 | { exclusive: "aws:ses-account" }, |
| 318 | ); |
| 319 | }); |
| 320 | |
| 321 | describe("Suppression List", () => { |
| 322 | test.provider( |
| 323 | "sandbox: reads work and the write surfaces the typed BadRequestException", |
| 324 | (_stack) => |
| 325 | Effect.gen(function* () { |
| 326 | const email = encodeURIComponent(SUPPRESSED_ADDRESS); |
| 327 | |
| 328 | // put — sandbox accounts cannot write to the suppression list; |
| 329 | // SES rejects with the typed BadRequestException ("Your account |
| 330 | // is still in the sandbox."). That proves the binding wires IAM |
| 331 | // and marshalling into the deployed Lambda. |
| 332 | const put = (yield* send( |
| 333 | HttpClientRequest.post(`${baseUrl}/suppress?email=${email}`), |
| 334 | ).pipe(Effect.flatMap((r) => r.json))) as { error?: string; message?: string }; |
| 335 | if (put.error === undefined) { |
| 336 | // Production account: the write succeeded — assert that rather |
| 337 | // than letting an empty branch pass, then clean up and let the |
| 338 | // gated lifecycle test below cover the full flow. |
| 339 | expect(put.error).toBeUndefined(); |
| 340 | const cleanup = (yield* send( |
| 341 | HttpClientRequest.post(`${baseUrl}/unsuppress?email=${email}`), |
| 342 | ).pipe(Effect.flatMap((r) => r.json))) as { error?: string }; |
| 343 | expect(cleanup.error).toBeUndefined(); |
| 344 | } else { |
| 345 | expect(put.error).toBe("BadRequestException"); |
| 346 | expect(put.message).toContain("sandbox"); |
| 347 | |
| 348 | // get of a never-suppressed address — typed NotFoundException. |
| 349 | const missing = (yield* send( |
| 350 | HttpClientRequest.get(`${baseUrl}/suppressed?email=${email}`), |
| 351 | ).pipe(Effect.flatMap((r) => r.json))) as { error?: string }; |
| 352 | expect(missing.error).toBe("NotFoundException"); |
| 353 | } |
| 354 | |
| 355 | // list — the read plane works even in the sandbox. |
| 356 | const list = (yield* send(HttpClientRequest.get(`${baseUrl}/suppressed-list`)).pipe( |
| 357 | Effect.flatMap((r) => r.json), |
| 358 | )) as { emails?: string[]; error?: string }; |
| 359 | expect(list.error).toBeUndefined(); |
| 360 | expect(Array.isArray(list.emails)).toBe(true); |
| 361 | }), |
| 362 | { exclusive: "aws:ses-account" }, |
| 363 | ); |
| 364 | |
| 365 | // Full write lifecycle needs production access (the sandbox blocks |
| 366 | // PutSuppressedDestination with BadRequestException: "Your account is |
| 367 | // still in the sandbox.") — gated with the same env var as real sends. |
| 368 | test.provider.skipIf(!VERIFIED_FROM)( |
| 369 | "put, get, list, and delete a suppressed destination (AWS_TEST_SES_FROM)", |
| 370 | (_stack) => |
| 371 | Effect.gen(function* () { |
| 372 | const email = encodeURIComponent(SUPPRESSED_ADDRESS); |
| 373 | |
| 374 | // put |
| 375 | const put = (yield* send( |
| 376 | HttpClientRequest.post(`${baseUrl}/suppress?email=${email}`), |
| 377 | ).pipe(Effect.flatMap((r) => r.json))) as { error?: string }; |
| 378 | expect(put.error).toBeUndefined(); |
| 379 | |
| 380 | // get — the suppression list is eventually consistent; poll until |
| 381 | // the entry materializes. |
| 382 | const got = yield* send( |
| 383 | HttpClientRequest.get(`${baseUrl}/suppressed?email=${email}`), |
| 384 | ).pipe( |
| 385 | Effect.flatMap((r) => r.json), |
| 386 | Effect.map((body) => body as { email?: string; reason?: string; error?: string }), |
| 387 | Effect.repeat({ |
| 388 | schedule: Schedule.spaced("2 seconds"), |
| 389 | until: (body): boolean => body.reason === "BOUNCE", |
| 390 | times: 10, |
| 391 | }), |
| 392 | ); |
| 393 | expect(got.email).toBe(SUPPRESSED_ADDRESS); |
| 394 | expect(got.reason).toBe("BOUNCE"); |
| 395 | |
| 396 | // list — filtered by reason, contains the address. |
| 397 | const list = yield* send(HttpClientRequest.get(`${baseUrl}/suppressed-list`)).pipe( |
| 398 | Effect.flatMap((r) => r.json), |
| 399 | Effect.map((body) => body as { emails?: string[]; error?: string }), |
| 400 | Effect.repeat({ |
| 401 | schedule: Schedule.spaced("2 seconds"), |
| 402 | until: (body): boolean => (body.emails ?? []).includes(SUPPRESSED_ADDRESS), |
| 403 | times: 10, |
| 404 | }), |
| 405 | ); |
| 406 | expect(list.emails).toContain(SUPPRESSED_ADDRESS); |
| 407 | |
| 408 | // delete — then the get surfaces the typed NotFoundException. |
| 409 | const del = (yield* send( |
| 410 | HttpClientRequest.post(`${baseUrl}/unsuppress?email=${email}`), |
| 411 | ).pipe(Effect.flatMap((r) => r.json))) as { error?: string }; |
| 412 | expect(del.error).toBeUndefined(); |
| 413 | |
| 414 | const gone = yield* send( |
| 415 | HttpClientRequest.get(`${baseUrl}/suppressed?email=${email}`), |
| 416 | ).pipe( |
| 417 | Effect.flatMap((r) => r.json), |
| 418 | Effect.map((body) => body as { error?: string }), |
| 419 | Effect.repeat({ |
| 420 | schedule: Schedule.spaced("2 seconds"), |
| 421 | until: (body): boolean => body.error === "NotFoundException", |
| 422 | times: 10, |
| 423 | }), |
| 424 | ); |
| 425 | expect(gone.error).toBe("NotFoundException"); |
| 426 | }), |
| 427 | { timeout: 120_000 }, |
| 428 | ); |
| 429 | }); |
| 430 | |
| 431 | describe("SendBounce", () => { |
| 432 | // A real bounce requires a message SES actually received within the last |
| 433 | // 24h; set this to that message id to exercise the success path. |
| 434 | const BOUNCE_MESSAGE_ID = process.env.AWS_TEST_SES_BOUNCE_MESSAGE_ID; |
| 435 | |
| 436 | test.provider( |
| 437 | "bouncing a non-existent message surfaces a typed SES error through the binding", |
| 438 | (_stack) => |
| 439 | Effect.gen(function* () { |
| 440 | const response = (yield* send(HttpClientRequest.post(`${baseUrl}/send-bounce`)).pipe( |
| 441 | Effect.flatMap((r) => r.json), |
| 442 | )) as { messageId?: string; error?: string }; |
| 443 | |
| 444 | // No real received message backs the fabricated OriginalMessageId |
| 445 | // (and the bounce sender is unverified in the sandbox), so SES |
| 446 | // rejects the bounce with the typed MessageRejected tag — verified |
| 447 | // live: "Failed to generate a bounce for <id>. The following |
| 448 | // identities are not verified: ...". That proves the binding wires |
| 449 | // ses:SendBounce IAM + request marshalling into the deployed Lambda. |
| 450 | expect(response.error).toBe("MessageRejected"); |
| 451 | expect(response.messageId).toBeUndefined(); |
| 452 | }), |
| 453 | { exclusive: "aws:ses-account" }, |
| 454 | ); |
| 455 | |
| 456 | test.provider.skipIf(!BOUNCE_MESSAGE_ID)( |
| 457 | "bounces a real received message (AWS_TEST_SES_BOUNCE_MESSAGE_ID)", |
| 458 | (_stack) => |
| 459 | Effect.gen(function* () { |
| 460 | const response = (yield* send( |
| 461 | HttpClientRequest.post( |
| 462 | `${baseUrl}/send-bounce?messageId=${encodeURIComponent(BOUNCE_MESSAGE_ID!)}`, |
| 463 | ), |
| 464 | ).pipe(Effect.flatMap((r) => r.json))) as { messageId?: string; error?: string }; |
| 465 | expect(response.error).toBeUndefined(); |
| 466 | expect(response.messageId).toBeTruthy(); |
| 467 | }), |
| 468 | { exclusive: "aws:ses-account" }, |
| 469 | ); |
| 470 | }); |
| 471 | |
| 472 | describe("SendCustomVerificationEmail", () => { |
| 473 | // The unknown-template case never leaves SES, so its recipient is |
| 474 | // irrelevant. The gated case sends for real, so it goes to the mailbox |
| 475 | // simulator, which accepts mail without touching reputation — an address |
| 476 | // at the fixture's example.com domain would hard-bounce. |
| 477 | const sendCustomVerification = (template?: string) => { |
| 478 | const email = encodeURIComponent( |
| 479 | template |
| 480 | ? (CVE_RECIPIENT ?? SIMULATOR) |
| 481 | : "verify-target@ses-bindings.alchemy-test.example.com", |
| 482 | ); |
| 483 | const query = template |
| 484 | ? `?email=${email}&template=${encodeURIComponent(template)}` |
| 485 | : `?email=${email}`; |
| 486 | return send(HttpClientRequest.post(`${baseUrl}/send-custom-verification${query}`)).pipe( |
| 487 | Effect.flatMap((r) => r.json), |
| 488 | ) as Effect.Effect<{ messageId?: string; error?: string; message?: string }, any, any>; |
| 489 | }; |
| 490 | |
| 491 | test.provider( |
| 492 | "an unknown template surfaces a typed SES error through the binding", |
| 493 | (_stack) => |
| 494 | Effect.gen(function* () { |
| 495 | const response = yield* sendCustomVerification(); |
| 496 | |
| 497 | // The fixture declares no template — SES rejects |
| 498 | // CreateCustomVerificationEmailTemplate unless its sender is already |
| 499 | // verified, which a bare account has none of. So the request names a |
| 500 | // template that does not resolve and SES answers with a typed error. |
| 501 | // |
| 502 | // The tag must be one SES raises about the REQUEST. AccessDenied |
| 503 | // would mean the binding failed to attach |
| 504 | // ses:SendCustomVerificationEmail — the regression this test exists |
| 505 | // to catch — so it is asserted against explicitly. |
| 506 | expect(response.error).not.toBe("AccessDeniedException"); |
| 507 | expect(CVE_REQUEST_TAGS).toContain(response.error); |
| 508 | expect(response.messageId).toBeUndefined(); |
| 509 | }), |
| 510 | { exclusive: "aws:ses-account" }, |
| 511 | ); |
| 512 | |
| 513 | // The real send goes through a SECOND binding, scoped by reference to the |
| 514 | // address being verified. It cannot reuse the binding above: SES |
| 515 | // authorizes this action against the identity of the RECIPIENT, and that |
| 516 | // binding only covers addresses at the fixture's own domain. |
| 517 | // |
| 518 | // The reference form takes no ownership, so nothing here adopts or |
| 519 | // destroys the account's real identities. |
| 520 | test.provider.skipIf(!CVE_TEMPLATE || !CVE_RECIPIENT)( |
| 521 | "sends through a real template (AWS_TEST_SES_CVE_TEMPLATE + _RECIPIENT)", |
| 522 | (_stack) => |
| 523 | Effect.gen(function* () { |
| 524 | const email = encodeURIComponent(CVE_RECIPIENT!); |
| 525 | const response = (yield* send( |
| 526 | HttpClientRequest.post( |
| 527 | `${baseUrl}/send-custom-verification-verified?email=${email}&template=${encodeURIComponent(CVE_TEMPLATE!)}`, |
| 528 | ), |
| 529 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 530 | messageId?: string; |
| 531 | error?: string; |
| 532 | message?: string; |
| 533 | }; |
| 534 | |
| 535 | expect(response.error).toBeUndefined(); |
| 536 | expect(response.messageId).toBeTruthy(); |
| 537 | }), |
| 538 | { exclusive: "aws:ses-account" }, |
| 539 | ); |
| 540 | }); |
| 541 | |
| 542 | describe("GetMessageInsights", () => { |
| 543 | const insights = (messageId?: string) => |
| 544 | send( |
| 545 | HttpClientRequest.get( |
| 546 | `${baseUrl}/message-insights${ |
| 547 | messageId ? `?messageId=${encodeURIComponent(messageId)}` : "" |
| 548 | }`, |
| 549 | ), |
| 550 | ).pipe(Effect.flatMap((r) => r.json)) as Effect.Effect< |
| 551 | { messageId?: string; error?: string }, |
| 552 | any, |
| 553 | any |
| 554 | >; |
| 555 | |
| 556 | test.provider( |
| 557 | "a well-formed but unknown message id is rejected with a typed tag", |
| 558 | (_stack) => |
| 559 | Effect.gen(function* () { |
| 560 | const response = yield* insights(); |
| 561 | |
| 562 | // No real send backs the fabricated MessageId, so SES rejects it — |
| 563 | // NotFoundException, or BadRequestException when VDM is disabled on |
| 564 | // the account. AccessDenied would mean ses:GetMessageInsights never |
| 565 | // reached the role. |
| 566 | expect(response.error).not.toBe("AccessDeniedException"); |
| 567 | expect(REQUEST_TAGS).toContain(response.error); |
| 568 | expect(response.messageId).toBeUndefined(); |
| 569 | if (VDM_ENABLED) expect(response.error).toBe("NotFoundException"); |
| 570 | }), |
| 571 | { exclusive: "aws:ses-account" }, |
| 572 | ); |
| 573 | |
| 574 | test.provider.skipIf(!VDM_ENABLED)( |
| 575 | "a malformed message id is rejected with BadRequestException (AWS_TEST_SES_VDM)", |
| 576 | (_stack) => |
| 577 | Effect.gen(function* () { |
| 578 | // Only reachable with VDM enabled. Without it SES short-circuits |
| 579 | // every GetMessageInsights call with NotFoundException ("To use this |
| 580 | // feature you must enable Virtual Deliverability Manager") before it |
| 581 | // ever looks at the id, so malformed and well-formed ids are |
| 582 | // indistinguishable on a VDM-less account. |
| 583 | const response = yield* insights("not-a-message-id"); |
| 584 | |
| 585 | expect(response.error).not.toBe("AccessDeniedException"); |
| 586 | expect(response.error).toBe("BadRequestException"); |
| 587 | expect(response.messageId).toBeUndefined(); |
| 588 | }), |
| 589 | { exclusive: "aws:ses-account" }, |
| 590 | ); |
| 591 | }); |
| 592 | |
| 593 | describe("BatchGetMetricData", () => { |
| 594 | const metricData = (partialDay = false) => |
| 595 | send( |
| 596 | HttpClientRequest.post(`${baseUrl}/metric-data${partialDay ? "?partialDay=1" : ""}`), |
| 597 | ).pipe(Effect.flatMap((r) => r.json)) as Effect.Effect< |
| 598 | { results?: number; error?: string }, |
| 599 | any, |
| 600 | any |
| 601 | >; |
| 602 | |
| 603 | test.provider( |
| 604 | "a midnight-aligned window reaches SES through the binding", |
| 605 | (_stack) => |
| 606 | Effect.gen(function* () { |
| 607 | const response = yield* metricData(); |
| 608 | |
| 609 | // With VDM enabled SES returns a (possibly empty) Results array; |
| 610 | // without it the account is rejected with BadRequestException. Both |
| 611 | // prove the IAM action and payload marshalling are wired — but |
| 612 | // neither may be an authorization failure. |
| 613 | expect(response.error).not.toBe("AccessDeniedException"); |
| 614 | if (response.error === undefined) { |
| 615 | expect(typeof response.results).toBe("number"); |
| 616 | } else { |
| 617 | expect(REQUEST_TAGS).toContain(response.error); |
| 618 | } |
| 619 | }), |
| 620 | { exclusive: "aws:ses-account" }, |
| 621 | ); |
| 622 | |
| 623 | test.provider.skipIf(!VDM_ENABLED)( |
| 624 | "returns a metric series on a VDM-enabled account (AWS_TEST_SES_VDM)", |
| 625 | (_stack) => |
| 626 | Effect.gen(function* () { |
| 627 | const response = yield* metricData(); |
| 628 | expect(response.error).toBeUndefined(); |
| 629 | expect(typeof response.results).toBe("number"); |
| 630 | }), |
| 631 | { exclusive: "aws:ses-account" }, |
| 632 | ); |
| 633 | |
| 634 | test.provider.skipIf(!VDM_ENABLED)( |
| 635 | "a partial-day window is rejected with BadRequestException (AWS_TEST_SES_VDM)", |
| 636 | (_stack) => |
| 637 | Effect.gen(function* () { |
| 638 | // SES requires daily-aggregated queries to run midnight-to-midnight |
| 639 | // UTC, which pins that the binding forwards the timestamps it is |
| 640 | // given. Only reachable with VDM enabled: without it SES rejects |
| 641 | // every BatchGetMetricData call with NotFoundException before |
| 642 | // validating the window. |
| 643 | const response = yield* metricData(true); |
| 644 | |
| 645 | expect(response.error).not.toBe("AccessDeniedException"); |
| 646 | expect(response.error).toBe("BadRequestException"); |
| 647 | }), |
| 648 | { exclusive: "aws:ses-account" }, |
| 649 | ); |
| 650 | }); |
| 651 | |
| 652 | describe("GetDomainStatisticsReport", () => { |
| 653 | const domainStatistics = (domain?: string) => |
| 654 | send( |
| 655 | HttpClientRequest.get( |
| 656 | `${baseUrl}/domain-statistics${domain ? `?domain=${encodeURIComponent(domain)}` : ""}`, |
| 657 | ), |
| 658 | ).pipe(Effect.flatMap((r) => r.json)) as Effect.Effect< |
| 659 | { days?: number; error?: string }, |
| 660 | any, |
| 661 | any |
| 662 | >; |
| 663 | |
| 664 | test.provider( |
| 665 | "requesting a domain deliverability report reaches SES through the binding", |
| 666 | (_stack) => |
| 667 | Effect.gen(function* () { |
| 668 | const response = yield* domainStatistics(); |
| 669 | |
| 670 | // The deliverability dashboard is a paid subscription; without it |
| 671 | // SES rejects the request with a typed tag. Either way the call must |
| 672 | // have been authorized. |
| 673 | expect(response.error).not.toBe("AccessDeniedException"); |
| 674 | if (response.error === undefined) { |
| 675 | expect(typeof response.days).toBe("number"); |
| 676 | } else { |
| 677 | expect(REQUEST_TAGS).toContain(response.error); |
| 678 | } |
| 679 | }), |
| 680 | { exclusive: "aws:ses-account" }, |
| 681 | ); |
| 682 | |
| 683 | test.provider( |
| 684 | "a domain the account does not own is rejected with a typed tag", |
| 685 | (_stack) => |
| 686 | Effect.gen(function* () { |
| 687 | const response = yield* domainStatistics("not-our-domain.alchemy-test.example.com"); |
| 688 | |
| 689 | expect(response.error).not.toBe("AccessDeniedException"); |
| 690 | expect(REQUEST_TAGS).toContain(response.error); |
| 691 | expect(response.days).toBeUndefined(); |
| 692 | }), |
| 693 | { exclusive: "aws:ses-account" }, |
| 694 | ); |
| 695 | }); |
| 696 | |
| 697 | describe("GetBlacklistReports", () => { |
| 698 | const blacklistReports = (ip?: string) => |
| 699 | send( |
| 700 | HttpClientRequest.get( |
| 701 | `${baseUrl}/blacklist-reports${ip ? `?ip=${encodeURIComponent(ip)}` : ""}`, |
| 702 | ), |
| 703 | ).pipe(Effect.flatMap((r) => r.json)) as Effect.Effect< |
| 704 | { ips?: string[]; error?: string }, |
| 705 | any, |
| 706 | any |
| 707 | >; |
| 708 | |
| 709 | // This binding has no request-rejection path to pin: SES needs no |
| 710 | // subscription for it and validates nothing about the items it is given. |
| 711 | // A garbage item (`not-an-ip-address`) returns 200 with an empty |
| 712 | // BlacklistReport exactly like a well-formed IP the account does not own, |
| 713 | // so a "malformed input" test here would assert the same thing as the |
| 714 | // success test. One unconditional test is the honest coverage. |
| 715 | test.provider( |
| 716 | "returns a blacklist report for the requested IPs", |
| 717 | (_stack) => |
| 718 | Effect.gen(function* () { |
| 719 | const response = yield* blacklistReports(); |
| 720 | |
| 721 | expect(response.error).toBeUndefined(); |
| 722 | expect(Array.isArray(response.ips)).toBe(true); |
| 723 | }), |
| 724 | { exclusive: "aws:ses-account" }, |
| 725 | ); |
| 726 | }); |
| 727 | }, |
| 728 | ); |