Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion apps/web/__tests__/unit/send-download-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 });

Expand Down
9 changes: 8 additions & 1 deletion apps/web/actions/send-download-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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." };
Expand Down
Loading