1import * as AWS from "@/AWS";
2import { CustomVerificationEmailTemplate } from "@/AWS/SES";
3import * as Test from "@/Test/Alchemy";
4import * as sesv2 from "@distilled.cloud/aws/sesv2";
5import { expect } from "alchemy-test";
6import * as Data from "effect/Data";
7import * as Effect from "effect/Effect";
8import * as Result from "effect/Result";
9import * as Schedule from "effect/Schedule";
10
11const { test } = Test.make({ providers: AWS.providers() });
12
13// SES rejects CreateCustomVerificationEmailTemplate outright when its
14// FromEmailAddress is not already a verified identity ("NotFoundException: The
15// from email address <...> is not verified"), so the whole suite needs a
16// verified sender. Point AWS_TEST_SES_FROM at one to run it. Sending through a
17// template additionally requires production (out-of-sandbox) access.
18const VERIFIED_FROM = process.env.AWS_TEST_SES_FROM;
19const FROM = VERIFIED_FROM ?? "verify@alchemy-test.example.com";
20
21class CvetStillExists extends Data.TaggedError("CvetStillExists")<{
22 readonly name: string;
23}> {}
24
25const assertCvetDeleted = (name: string) =>
26 sesv2.getCustomVerificationEmailTemplate({ TemplateName: name }).pipe(
27 Effect.flatMap(() => Effect.fail(new CvetStillExists({ name }))),
28 Effect.catchTag("NotFoundException", () => Effect.void),
29 Effect.retry({
30 while: (e) => e._tag === "CvetStillExists",
31 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
32 }),
33 );
34
35// UNGATED probe. Both lifecycle tests below need a verified sender, so on a
36// bare account this file would otherwise assert nothing at all. This proves
37// the gate itself is real and correctly typed: SES refuses to create the
38// template when its FromEmailAddress is unverified, and distilled surfaces
39// that as NotFoundException rather than an untyped catch-all. If AWS ever
40// changes the rejection, this fails instead of silently skipping forever.
41test.provider(
42 "creating a template from an unverified sender fails with the typed tag",
43 (stack) =>
44 Effect.gen(function* () {
45 yield* stack.destroy();
46
47 const result = yield* Effect.result(
48 stack.deploy(
49 Effect.gen(function* () {
50 return yield* CustomVerificationEmailTemplate("UnverifiedProbe", {
51 fromEmailAddress: "definitely-not-verified@alchemy-test.invalid",
52 templateSubject: "Please confirm your email",
53 templateContent:
54 "<html><body>Click to verify your address.</body></html>",
55 successRedirectionURL: "https://example.com/verified",
56 failureRedirectionURL: "https://example.com/verify-failed",
57 });
58 }),
59 ),
60 );
61
62 expect(Result.isFailure(result)).toBe(true);
63 if (Result.isFailure(result)) {
64 expect(JSON.stringify(result.failure)).toContain("NotFoundException");
65 }
66
67 yield* stack.destroy();
68 }),
69 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
70);
71
72test.provider.skipIf(!VERIFIED_FROM)(
73 "custom verification template lifecycle: create, update, delete",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 const template = yield* stack.deploy(
79 Effect.gen(function* () {
80 return yield* CustomVerificationEmailTemplate("Verify", {
81 fromEmailAddress: FROM,
82 templateSubject: "Please confirm your email",
83 templateContent:
84 "<html><body>Click to verify your address.</body></html>",
85 successRedirectionURL: "https://example.com/verified",
86 failureRedirectionURL: "https://example.com/verify-failed",
87 });
88 }),
89 );
90
91 expect(template.templateName).toBeDefined();
92
93 // out-of-band verification via distilled
94 const observed = yield* sesv2.getCustomVerificationEmailTemplate({
95 TemplateName: template.templateName,
96 });
97 expect(observed.TemplateSubject).toBe("Please confirm your email");
98 expect(observed.SuccessRedirectionURL).toBe(
99 "https://example.com/verified",
100 );
101
102 // update subject and content in place
103 yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* CustomVerificationEmailTemplate("Verify", {
106 fromEmailAddress: FROM,
107 templateSubject: "Confirm your email address",
108 templateContent: "<html><body>Verify to get started.</body></html>",
109 successRedirectionURL: "https://example.com/done",
110 failureRedirectionURL: "https://example.com/verify-failed",
111 });
112 }),
113 );
114 const updated = yield* sesv2.getCustomVerificationEmailTemplate({
115 TemplateName: template.templateName,
116 });
117 expect(updated.TemplateSubject).toBe("Confirm your email address");
118 expect(updated.SuccessRedirectionURL).toBe("https://example.com/done");
119
120 yield* stack.destroy();
121 yield* assertCvetDeleted(template.templateName);
122 }),
123 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
124);
125
126test.provider.skipIf(!VERIFIED_FROM)(
127 "custom name replaces on rename",
128 (stack) =>
129 Effect.gen(function* () {
130 yield* stack.destroy();
131
132 const first = yield* stack.deploy(
133 Effect.gen(function* () {
134 return yield* CustomVerificationEmailTemplate("Named", {
135 templateName: "alchemy-test-cvet-a",
136 fromEmailAddress: FROM,
137 templateSubject: "A",
138 templateContent: "<html><body>A</body></html>",
139 successRedirectionURL: "https://example.com/a",
140 failureRedirectionURL: "https://example.com/a-fail",
141 });
142 }),
143 );
144 expect(first.templateName).toBe("alchemy-test-cvet-a");
145
146 const second = yield* stack.deploy(
147 Effect.gen(function* () {
148 return yield* CustomVerificationEmailTemplate("Named", {
149 templateName: "alchemy-test-cvet-b",
150 fromEmailAddress: FROM,
151 templateSubject: "B",
152 templateContent: "<html><body>B</body></html>",
153 successRedirectionURL: "https://example.com/b",
154 failureRedirectionURL: "https://example.com/b-fail",
155 });
156 }),
157 );
158 expect(second.templateName).toBe("alchemy-test-cvet-b");
159 yield* assertCvetDeleted("alchemy-test-cvet-a");
160
161 yield* stack.destroy();
162 yield* assertCvetDeleted("alchemy-test-cvet-b");
163 }),
164 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
165);