swadhinbiswas Add user email delivery test API · 2/19/2026
633bfd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { beforeEach, describe, expect, it, vi } from "vitest";
const { sendTestEmailMock, isSmtpConfiguredMock } = vi.hoisted(() => ({
sendTestEmailMock: vi.fn(async () => true),
isSmtpConfiguredMock: vi.fn(() => true),
}));
vi.mock("@/lib/email", () => ({
sendTestEmail: sendTestEmailMock,
isSmtpConfigured: isSmtpConfiguredMock,
}));
import { POST as emailTestPost } from "@/pages/api/user/email/test";
async function readJson(response: Response): Promise<any> {
return response.json();
}
describe("user email test route", () => {
beforeEach(() => {
sendTestEmailMock.mockReset();
isSmtpConfiguredMock.mockReset();
sendTestEmailMock.mockResolvedValue(true);
isSmtpConfiguredMock.mockReturnValue(true);
});
it("returns 401 when unauthenticated", async () => {
const response = await emailTestPost({
locals: {},
request: new Request("http://localhost/api/user/email/test", { method: "POST" }),
} as any);
expect(response.status).toBe(401);
});
it("uses user email and dryRun=true by default", async () => {
const response = await emailTestPost({
locals: { user: { id: "user-1", email: "[email protected]" } },
request: new Request("http://localhost/api/user/email/test", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({}),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.dryRun).toBe(true);
expect(body?.data?.to).toBe("[email protected]");
expect(sendTestEmailMock).toHaveBeenCalledWith({ to: "[email protected]", dryRun: true });
});
it("supports explicit destination and non-dry run", async () => {
const response = await emailTestPost({
locals: { user: { id: "user-1", email: "[email protected]" } },
request: new Request("http://localhost/api/user/email/test", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ to: "[email protected]", dryRun: false }),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.dryRun).toBe(false);
expect(body?.data?.to).toBe("[email protected]");
expect(sendTestEmailMock).toHaveBeenCalledWith({ to: "[email protected]", dryRun: false });
});
});