165 lines
6.3 KiB
TypeScript
165 lines
6.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
requireSession: vi.fn(),
|
|
loadLifecycle: vi.fn(),
|
|
updatePreference: vi.fn(),
|
|
requestExport: vi.fn(),
|
|
requestDeletion: vi.fn(),
|
|
cancelDeletion: vi.fn(),
|
|
validateCsrf: 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-lifecycle.server", () => ({
|
|
PortalLifecycleError: class PortalLifecycleError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code);
|
|
}
|
|
},
|
|
loadPortalLifecycle: mocks.loadLifecycle,
|
|
updatePortalNotificationPreference: mocks.updatePreference,
|
|
requestPortalGdprExport: mocks.requestExport,
|
|
requestPortalAccountDeletion: mocks.requestDeletion,
|
|
cancelPortalAccountDeletion: mocks.cancelDeletion,
|
|
}));
|
|
|
|
import { action, loader } from "./settings";
|
|
|
|
const session = {
|
|
identity: {
|
|
tenantId: "tenant-a",
|
|
portalUserId: "portal-user-a",
|
|
tenantFrozen: false,
|
|
portalTitle: "Acme Portal",
|
|
viewerFirstName: "Karla",
|
|
viewerLastName: "Kunde",
|
|
},
|
|
origin: { origin: "https://portal.example.test", hostname: "portal.example.test", tenantSlug: "acme", customHost: false },
|
|
responseHeaders: new Headers({ "Set-Cookie": "portal=renewed" }),
|
|
};
|
|
|
|
function routeArgs(request: Request) {
|
|
return { request, params: { tenant: "acme" }, context: {} };
|
|
}
|
|
|
|
function postForm(values: Readonly<Record<string, string>>): Request {
|
|
const body = new URLSearchParams();
|
|
body.set("csrf", "csrf-token");
|
|
for (const [key, value] of Object.entries(values)) body.set(key, value);
|
|
return new Request("https://portal.example.test/acme/settings", { method: "POST", body });
|
|
}
|
|
|
|
describe("customer portal lifecycle settings route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.requireSession.mockResolvedValue(session);
|
|
mocks.validateCsrf.mockResolvedValue(undefined);
|
|
mocks.loadLifecycle.mockResolvedValue({
|
|
profile: {
|
|
email: "karla@example.test",
|
|
firstName: "Karla",
|
|
lastName: "Kunde",
|
|
phone: null,
|
|
locale: "de",
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
lastLoginAt: null,
|
|
lastActivityAt: null,
|
|
},
|
|
notificationPreferences: [{ eventType: "status_mail", emailEnabled: false, mutedUntil: null }],
|
|
exportJobs: [
|
|
{
|
|
id: "export-a",
|
|
status: "ready",
|
|
progressPercent: 100,
|
|
createdAt: new Date("2026-07-16T08:00:00.000Z"),
|
|
resultExpiresAt: new Date("2099-07-17T08:00:00.000Z"),
|
|
failureCode: null,
|
|
},
|
|
],
|
|
deletionRequest: null,
|
|
});
|
|
mocks.updatePreference.mockResolvedValue("preference-a");
|
|
mocks.requestExport.mockResolvedValue({ exportId: "export-a" });
|
|
mocks.requestDeletion.mockResolvedValue({
|
|
deletionRequestId: "deletion-a",
|
|
requestedAt: new Date(),
|
|
cancellableUntil: new Date(),
|
|
});
|
|
mocks.cancelDeletion.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it("builds a complete VM with persisted preferences, safe defaults, and a private download path", async () => {
|
|
const response = await loader(routeArgs(new Request("https://portal.example.test/acme/settings")));
|
|
const data = (await response.json()) as {
|
|
vm: { status: string; data: { notificationPreferences: Array<{ eventType: string; emailEnabled: boolean }>; exportJobs: Array<{ downloadHref: string }> } };
|
|
paths: { settings: string };
|
|
};
|
|
|
|
expect(response.headers.get("Cache-Control")).toBe("private, no-store");
|
|
expect(data.paths.settings).toBe("/acme/settings");
|
|
expect(data.vm.status).toBe("ready");
|
|
expect(data.vm.data.notificationPreferences).toHaveLength(2);
|
|
expect(data.vm.data.notificationPreferences).toContainEqual({
|
|
eventType: "status_mail",
|
|
emailEnabled: false,
|
|
mutedUntil: null,
|
|
});
|
|
expect(data.vm.data.notificationPreferences).toContainEqual({
|
|
eventType: "lifecycle",
|
|
emailEnabled: true,
|
|
mutedUntil: null,
|
|
});
|
|
expect(data.vm.data.exportJobs[0]?.downloadHref).toBe("/acme/exports/export-a");
|
|
});
|
|
|
|
it("validates CSRF and forwards deletion confirmation through the portal session", async () => {
|
|
const request = postForm({
|
|
intent: "request_deletion",
|
|
password: "correct horse battery staple",
|
|
idempotencyKey: "deletion-key-a",
|
|
});
|
|
const response = await action(routeArgs(request));
|
|
|
|
expect(response.status).toBe(202);
|
|
await expect(response.json()).resolves.toEqual({
|
|
ok: true,
|
|
messageKey: "customerPortal.lifecycle.messages.deletionRequested",
|
|
});
|
|
expect(mocks.validateCsrf).toHaveBeenCalledWith(request, expect.objectContaining({ get: expect.any(Function) }));
|
|
expect(mocks.requestDeletion).toHaveBeenCalledWith(session, {
|
|
password: "correct horse battery staple",
|
|
idempotencyKey: "deletion-key-a",
|
|
request,
|
|
});
|
|
});
|
|
|
|
it("persists only allow-listed notification event types", async () => {
|
|
const request = postForm({ intent: "preference", eventType: "lifecycle", enabled: "false" });
|
|
const response = await action(routeArgs(request));
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(mocks.updatePreference).toHaveBeenCalledWith(session, {
|
|
eventType: "lifecycle",
|
|
emailEnabled: false,
|
|
});
|
|
|
|
const invalid = await action(routeArgs(postForm({ intent: "preference", eventType: "billing", enabled: "true" })));
|
|
expect(invalid.status).toBe(400);
|
|
const removed = await action(routeArgs(postForm({ intent: "preference", eventType: "document_released", enabled: "true" })));
|
|
expect(removed.status).toBe(400);
|
|
expect(mocks.updatePreference).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("returns 404 for all lifecycle mutations on a frozen tenant", async () => {
|
|
mocks.requireSession.mockResolvedValueOnce({ ...session, identity: { ...session.identity, tenantFrozen: true } });
|
|
await expect(action(routeArgs(postForm({ intent: "request_export" })))).rejects.toMatchObject({ status: 404 });
|
|
expect(mocks.validateCsrf).not.toHaveBeenCalled();
|
|
expect(mocks.requestExport).not.toHaveBeenCalled();
|
|
});
|
|
});
|