91 lines
3.5 KiB
TypeScript
91 lines
3.5 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => {
|
|
class PortalComplaintError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
return {
|
|
PortalComplaintError,
|
|
requireSession: vi.fn(),
|
|
validateCsrf: vi.fn(),
|
|
submitComplaint: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock("~/services/customer-portal/portal-data.server", () => ({
|
|
requirePortalSessionContext: mocks.requireSession,
|
|
portalNotFound: () => new Response(null, { status: 404 }),
|
|
}));
|
|
vi.mock("~/services/customer-portal/portal-csrf.server", () => ({ validatePortalCsrfToken: mocks.validateCsrf }));
|
|
vi.mock("~/services/customer-portal/portal-complaint.server", () => ({
|
|
PortalComplaintError: mocks.PortalComplaintError,
|
|
submitPortalComplaint: mocks.submitComplaint,
|
|
}));
|
|
|
|
import { action } from "./project-complaint";
|
|
|
|
const session = {
|
|
identity: { tenantId: "tenant-a", portalUserId: "portal-user-a", tenantFrozen: false },
|
|
responseHeaders: new Headers({ "Set-Cookie": "portal=renewed" }),
|
|
};
|
|
|
|
function args(request: Request) {
|
|
return { request, params: { tenant: "acme", projectId: "project-a", formId: "form-a" }, context: {} } as never;
|
|
}
|
|
|
|
function complaintRequest(payload: string): Request {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf-token");
|
|
form.set("idempotencyKey", "complaint-key-a");
|
|
form.set("gdprAccepted", "true");
|
|
form.set("payload", payload);
|
|
const request = new Request("https://portal.example.test/acme/projects/project-a/complaints/form-a", { method: "POST" });
|
|
Object.defineProperty(request, "formData", { value: async () => form });
|
|
return request;
|
|
}
|
|
|
|
describe("customer portal complaint route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.requireSession.mockResolvedValue(session);
|
|
mocks.validateCsrf.mockResolvedValue(undefined);
|
|
mocks.submitComplaint.mockResolvedValue({ complaintId: "complaint-a", submissionId: "submission-a" });
|
|
});
|
|
|
|
it("submits the authenticated portal_form payload with GDPR proof and idempotency", async () => {
|
|
const request = complaintRequest(JSON.stringify({ subject: "Heizung bleibt kalt", urgent: true }));
|
|
const response = await action(args(request));
|
|
|
|
expect(response.status).toBe(201);
|
|
expect(mocks.validateCsrf).toHaveBeenCalledWith(request, expect.any(FormData));
|
|
expect(mocks.submitComplaint).toHaveBeenCalledWith({
|
|
identity: session.identity,
|
|
projectId: "project-a",
|
|
formId: "form-a",
|
|
payload: {
|
|
values: { subject: "Heizung bleibt kalt", urgent: true },
|
|
gdprAccepted: true,
|
|
idempotencyKey: "complaint-key-a",
|
|
},
|
|
});
|
|
await expect(response.json()).resolves.toMatchObject({ ok: true, complaintId: "complaint-a", messageKey: "customerPortal.complaint.success" });
|
|
});
|
|
|
|
it("rejects malformed JSON with a stable M002 key before persistence", async () => {
|
|
const response = await action(args(complaintRequest("{")));
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({ ok: false, errorKey: "customerPortal.complaint.errors.invalidPayload" });
|
|
expect(mocks.submitComplaint).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("maps unavailable forms and project access to a non-enumerating 404", async () => {
|
|
mocks.submitComplaint.mockRejectedValueOnce(new mocks.PortalComplaintError("FORM_NOT_FOUND"));
|
|
await expect(action(args(complaintRequest(JSON.stringify({ subject: "Hidden" }))))).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
});
|