Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/chat/services/chatAuditSession.server.test.ts
T

75 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@hivegrid/config", async (importOriginal) => {
const actual = await importOriginal<typeof import("@hivegrid/config")>();
return {
...actual,
loadRequiredWebConfig: () => ({
nodeEnv: "test",
routing: {
appHost: "app.localhost",
adminHost: "admin.localhost",
marketingHost: "localhost",
protocol: "http",
},
sessionSecret: "chat-audit-test-session-secret-with-enough-entropy",
}),
};
});
import {
__resetChatAuditSessionCookieForTests,
beginChatAuditSession,
CHAT_AUDIT_SESSION_TTL_MS,
normalizeChatAuditReason,
readChatAuditSession,
} from "./chatAuditSession.server";
describe("AK-P15b short-lived chat audit sessions", () => {
beforeEach(() => __resetChatAuditSessionCookieForTests());
it("binds the signed reason session to its tenant and user", async () => {
const now = new Date("2026-07-16T10:00:00.000Z");
const issued = await beginChatAuditSession({
tenantId: "tenant-a",
userId: "admin-a",
reason: " Kundenreklamation nachvollziehen ",
now,
sessionId: "audit-session-1",
});
const request = requestWithCookie(issued.cookie);
await expect(readChatAuditSession({ request, tenantId: "tenant-a", userId: "admin-a", now })).resolves.toMatchObject({
id: "audit-session-1",
reason: "Kundenreklamation nachvollziehen",
expiresAt: now.getTime() + CHAT_AUDIT_SESSION_TTL_MS,
});
await expect(readChatAuditSession({ request, tenantId: "tenant-b", userId: "admin-a", now })).resolves.toBeNull();
await expect(readChatAuditSession({ request, tenantId: "tenant-a", userId: "admin-b", now })).resolves.toBeNull();
});
it("expires server-side even when a client retains the cookie", async () => {
const now = new Date("2026-07-16T10:00:00.000Z");
const issued = await beginChatAuditSession({ tenantId: "tenant-a", userId: "admin-a", reason: "Audit", now });
await expect(
readChatAuditSession({
request: requestWithCookie(issued.cookie),
tenantId: "tenant-a",
userId: "admin-a",
now: new Date(now.getTime() + CHAT_AUDIT_SESSION_TTL_MS),
})
).resolves.toBeNull();
});
it("rejects a missing justification", () => {
expect(normalizeChatAuditReason(" \n ")).toBeNull();
expect(normalizeChatAuditReason("Legitimer Supportfall")).toBe("Legitimer Supportfall");
});
});
function requestWithCookie(setCookie: string): Request {
return new Request("http://app.localhost/demo/settings/chat/audit", {
headers: { cookie: setCookie.split(";", 1)[0]! },
});
}