add verification service and email service tests (13 new tests)
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 12m26s

This commit is contained in:
Hoid 2026-02-26 07:04:39 +00:00
parent 9dcc473e78
commit 1a37765f41
2 changed files with 133 additions and 17 deletions

View file

@ -1,7 +1,11 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock nodemailer before importing email service
const mockSendMail = vi.fn();
vi.unmock("../services/email.js");
const { mockSendMail } = vi.hoisted(() => ({
mockSendMail: vi.fn(),
}));
vi.mock("nodemailer", () => ({
default: {
createTransport: vi.fn(() => ({
@ -10,7 +14,6 @@ vi.mock("nodemailer", () => ({
},
}));
// Mock logger
vi.mock("../services/logger.js", () => ({
default: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() },
}));
@ -25,35 +28,28 @@ describe("Email Service", () => {
describe("sendVerificationEmail", () => {
it("constructs correct email with code", async () => {
mockSendMail.mockResolvedValueOnce({ messageId: "test-123" });
const result = await sendVerificationEmail("user@example.com", "654321");
expect(result).toBe(true);
expect(mockSendMail).toHaveBeenCalledOnce();
const mailOptions = mockSendMail.mock.calls[0][0];
expect(mailOptions.to).toBe("user@example.com");
expect(mailOptions.subject).toContain("DocFast");
expect(mailOptions.subject).toContain("Verify");
expect(mailOptions.text).toContain("654321");
expect(mailOptions.html).toContain("654321");
const opts = mockSendMail.mock.calls[0][0];
expect(opts.to).toBe("user@example.com");
expect(opts.subject).toContain("Verify");
expect(opts.text).toContain("654321");
expect(opts.html).toContain("654321");
});
it("returns false when SMTP fails", async () => {
mockSendMail.mockRejectedValueOnce(new Error("SMTP connection refused"));
const result = await sendVerificationEmail("user@example.com", "123456");
expect(result).toBe(false);
});
it("includes expiry notice in email body", async () => {
mockSendMail.mockResolvedValueOnce({ messageId: "test-456" });
await sendVerificationEmail("user@example.com", "111111");
const mailOptions = mockSendMail.mock.calls[0][0];
expect(mailOptions.text).toContain("15 minutes");
const opts = mockSendMail.mock.calls[0][0];
expect(opts.text).toContain("15 minutes");
});
});
});