diff --git a/apps/web/__tests__/unit/send-download-link.test.ts b/apps/web/__tests__/unit/send-download-link.test.ts index 1850083413..e83cca31ff 100644 --- a/apps/web/__tests__/unit/send-download-link.test.ts +++ b/apps/web/__tests__/unit/send-download-link.test.ts @@ -27,7 +27,7 @@ describe("sendDownloadLink", () => { headersMock.mockResolvedValue( new Headers({ host: "cap.test", "x-real-ip": "127.0.0.1" }), ); - sendEmailMock.mockResolvedValue(undefined); + sendEmailMock.mockResolvedValue({ data: { id: "email-1" }, error: null }); }); it("sends the email when the rate limit check passes", async () => { @@ -39,6 +39,47 @@ describe("sendDownloadLink", () => { expect(sendEmailMock).toHaveBeenCalledTimes(1); }); + it.each([ + { + data: null, + error: { + name: "validation_error", + message: "Sender domain is not verified", + }, + }, + { + data: null, + error: { + name: "email_not_configured", + message: "Email is not configured", + }, + }, + undefined, + { data: null, error: null }, + ])( + "reports unsuccessful delivery instead of claiming the email was sent: %j", + async (delivery) => { + sendEmailMock.mockResolvedValueOnce(delivery); + const { sendDownloadLink } = await import("@/actions/send-download-link"); + + expect(await sendDownloadLink("user@example.com")).toEqual({ + success: false, + error: "Something went wrong. Please try again.", + }); + expect(sendEmailMock).toHaveBeenCalledOnce(); + }, + ); + + it("reports a delivery exception", async () => { + sendEmailMock.mockRejectedValueOnce(new Error("Network failure")); + const { sendDownloadLink } = await import("@/actions/send-download-link"); + + expect(await sendDownloadLink("user@example.com")).toEqual({ + success: false, + error: "Something went wrong. Please try again.", + }); + }); + it("rejects the request when rate limited", async () => { checkRateLimitMock.mockResolvedValueOnce({ rateLimited: true }); diff --git a/apps/web/actions/send-download-link.ts b/apps/web/actions/send-download-link.ts index 15b1be66fd..d0390e3f39 100644 --- a/apps/web/actions/send-download-link.ts +++ b/apps/web/actions/send-download-link.ts @@ -63,13 +63,20 @@ export async function sendDownloadLink(email: string) { } try { - await sendEmail({ + const delivery = await sendEmail({ email: sanitized, subject: "Your Cap download links", react: DownloadLink({ email: sanitized }), marketing: true, }); + if (!delivery?.data) { + return { + success: false, + error: "Something went wrong. Please try again.", + }; + } + return { success: true }; } catch { return { success: false, error: "Something went wrong. Please try again." };