Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
175 lines
6.7 KiB
TypeScript
175 lines
6.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
apiGet: vi.fn(),
|
|
apiPost: vi.fn(),
|
|
apiPut: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/clientApi", () => ({
|
|
apiGet: mocks.apiGet,
|
|
apiPost: mocks.apiPost,
|
|
apiPut: mocks.apiPut,
|
|
}));
|
|
|
|
import {
|
|
buildEmailTemplatesQuery,
|
|
normalizeEmailTemplatePage,
|
|
listEmailTemplates,
|
|
getEmailTemplate,
|
|
createEmailTemplate,
|
|
updateEmailTemplate,
|
|
setEmailTemplateStatus,
|
|
previewEmailTemplate,
|
|
uploadEmailAttachment,
|
|
listEmailLogs,
|
|
getEmailLog,
|
|
} from "./emailTemplates";
|
|
|
|
beforeEach(() => {
|
|
mocks.apiGet.mockReset();
|
|
mocks.apiPost.mockReset();
|
|
mocks.apiPut.mockReset();
|
|
});
|
|
|
|
describe("buildEmailTemplatesQuery", () => {
|
|
it("builds a 0-based page/size query with no filters", () => {
|
|
expect(buildEmailTemplatesQuery({ page: 1, pageSize: 20 })).toBe("page=0&size=20");
|
|
});
|
|
|
|
it("includes name, lang and status when present", () => {
|
|
expect(buildEmailTemplatesQuery({ name: "Reset", lang: "EN", status: "Y", page: 2, pageSize: 10 })).toBe(
|
|
"name=Reset&lang=EN&status=Y&page=1&size=10",
|
|
);
|
|
});
|
|
|
|
it("includes description when present", () => {
|
|
expect(buildEmailTemplatesQuery({ description: "pass", page: 1, pageSize: 20 })).toBe(
|
|
"description=pass&page=0&size=20",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("normalizeEmailTemplatePage", () => {
|
|
it("reads Spring Data Page's content/totalElements shape", () => {
|
|
const page = { content: [{ name: "Reset Password", languages: [] }], totalElements: 42 };
|
|
expect(normalizeEmailTemplatePage(page)).toEqual({ items: page.content, total: 42 });
|
|
});
|
|
|
|
it("returns an empty page for null/non-object input", () => {
|
|
expect(normalizeEmailTemplatePage(null)).toEqual({ items: [], total: 0 });
|
|
expect(normalizeEmailTemplatePage(undefined)).toEqual({ items: [], total: 0 });
|
|
expect(normalizeEmailTemplatePage("oops")).toEqual({ items: [], total: 0 });
|
|
});
|
|
});
|
|
|
|
describe("listEmailTemplates", () => {
|
|
it("calls GET /api/email-templates with the built query and normalizes the result", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
|
|
const result = await listEmailTemplates({ name: "Reset", page: 2, pageSize: 20 });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/email-templates?name=Reset&page=1&size=20");
|
|
expect(result).toEqual({ items: [], total: 0 });
|
|
});
|
|
});
|
|
|
|
describe("getEmailTemplate", () => {
|
|
it("hits the by-name path with an encoded name", async () => {
|
|
const rows = [
|
|
{ id: 1, name: "Reset Password", lang: "EN" as const, subject: "s", body: "b", status: "Y" as const, attachmentId: [] },
|
|
];
|
|
mocks.apiGet.mockResolvedValue(rows);
|
|
const result = await getEmailTemplate("Reset Password");
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/email-templates/by-name/Reset%20Password");
|
|
expect(result).toEqual(rows);
|
|
});
|
|
});
|
|
|
|
describe("createEmailTemplate", () => {
|
|
it("POSTs to /api/email-templates with the body", async () => {
|
|
const payload = { name: "Welcome", lang: "EN" as const, subject: "s", body: "b", status: "Y" as const, attachmentId: [] };
|
|
mocks.apiPost.mockResolvedValue({ id: 9, ...payload });
|
|
const result = await createEmailTemplate(payload);
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/email-templates", payload);
|
|
expect(result.id).toBe(9);
|
|
});
|
|
});
|
|
|
|
describe("updateEmailTemplate", () => {
|
|
it("PUTs to the record's own URL with the id in the path", async () => {
|
|
const payload = { name: "Welcome", lang: "EN" as const, subject: "s", body: "b", status: "Y" as const, attachmentId: [] };
|
|
mocks.apiPut.mockResolvedValue({ id: 5, ...payload });
|
|
const result = await updateEmailTemplate(5, payload);
|
|
expect(mocks.apiPut).toHaveBeenCalledWith("/api/email-templates/5", payload);
|
|
expect(result.id).toBe(5);
|
|
});
|
|
});
|
|
|
|
describe("setEmailTemplateStatus", () => {
|
|
it("PUTs the status to /api/email-templates/{id}/status", async () => {
|
|
const updated = { id: 5, status: "N" as const };
|
|
mocks.apiPut.mockResolvedValue(updated);
|
|
const result = await setEmailTemplateStatus(5, "N");
|
|
expect(mocks.apiPut).toHaveBeenCalledWith("/api/email-templates/5/status", { status: "N" });
|
|
expect(result).toEqual(updated);
|
|
});
|
|
});
|
|
|
|
describe("previewEmailTemplate", () => {
|
|
it("POSTs the body and sample values to the preview route", async () => {
|
|
mocks.apiPost.mockResolvedValue({ renderedHtml: "<p>Hi Amir</p>" });
|
|
const result = await previewEmailTemplate("<p>Hi {{firstName}}</p>", { firstName: "Amir" });
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/email-templates/preview", {
|
|
body: "<p>Hi {{firstName}}</p>",
|
|
sampleValues: { firstName: "Amir" },
|
|
});
|
|
expect(result).toEqual({ renderedHtml: "<p>Hi Amir</p>" });
|
|
});
|
|
});
|
|
|
|
describe("uploadEmailAttachment", () => {
|
|
it("POSTs {fileName, fileType, base} to the attachments route", async () => {
|
|
const ref = { id: 7, fileName: "report.pdf", fileType: "application/pdf" };
|
|
mocks.apiPost.mockResolvedValue(ref);
|
|
const result = await uploadEmailAttachment({
|
|
fileName: "report.pdf",
|
|
fileType: "application/pdf",
|
|
base: "aGVsbG8=",
|
|
});
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/email-templates/attachments", {
|
|
fileName: "report.pdf",
|
|
fileType: "application/pdf",
|
|
base: "aGVsbG8=",
|
|
});
|
|
expect(result).toEqual(ref);
|
|
});
|
|
});
|
|
|
|
describe("listEmailLogs", () => {
|
|
it("calls GET /api/email-logs with the built query and normalizes the result", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [{ id: 1, recipients: ["a@b.c"] }], totalElements: 1 });
|
|
const result = await listEmailLogs({ page: 1, pageSize: 20 });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/email-logs?page=0&size=20");
|
|
expect(result).toEqual({ items: [{ id: 1, recipients: ["a@b.c"] }], total: 1 });
|
|
});
|
|
});
|
|
|
|
describe("getEmailLog", () => {
|
|
it("calls GET /api/email-logs/{id} and returns the detail", async () => {
|
|
const detail = { id: 7, subject: "Reset", recipients: [], attachments: [] };
|
|
mocks.apiGet.mockResolvedValue(detail);
|
|
const result = await getEmailLog(7);
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/email-logs/7");
|
|
expect(result).toEqual(detail);
|
|
});
|
|
});
|
|
|
|
describe("emailLogStatus", () => {
|
|
it("maps send-log codes to a label and tone; unknown codes show themselves", async () => {
|
|
const { emailLogStatus } = await import("./emailTemplates");
|
|
expect(emailLogStatus("P")).toEqual({ label: "Pending", tone: "warning" });
|
|
expect(emailLogStatus("Y")).toEqual({ label: "Success", tone: "positive" });
|
|
expect(emailLogStatus("N")).toEqual({ label: "Failed", tone: "negative" });
|
|
expect(emailLogStatus("X")).toEqual({ label: "X", tone: "neutral" });
|
|
expect(emailLogStatus(undefined).label).toBe("—");
|
|
});
|
|
});
|