Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/portal-setup-mail-contract.server.test.ts
T

146 lines
5.7 KiB
TypeScript

import { createHash } from "node:crypto";
import { describe, expect, it } from "vitest";
import {
bindEncryptedPortalSetupMailToOutbox,
buildPortalSetupCapabilityPlaintext,
findPortalSetupRawToken,
hashPortalSetupMailContent,
hasValidEncryptedPortalSetupMailContent,
hasValidMaterializedPortalSetupMailContent,
hasValidRedactedPortalSetupMailContent,
materializePortalSetupMailContent,
PORTAL_SETUP_MAIL_SOURCE,
PORTAL_SETUP_SECRET_REDACTED_MARKER,
PORTAL_SETUP_TOKEN_PLACEHOLDER,
readPortalSetupCapabilityPlaintext,
} from "./portal-setup-mail-contract.server";
const rawToken = "p".repeat(43);
const setupTokenHash = createHash("sha256").update(rawToken).digest("hex");
const text = `Richten Sie Ihren Zugang ein: https://portal.example.test/acme/setup/${PORTAL_SETUP_TOKEN_PLACEHOLDER}`;
const html = `<p>Richten Sie Ihren Zugang ein: <a href="https://portal.example.test/acme/setup/${PORTAL_SETUP_TOKEN_PLACEHOLDER}">Portal öffnen</a></p>`;
const subject = "Kundenportal-Zugang einrichten";
const recipient = "customer@example.test";
const baseMetadata = {
invitationSource: "portal_admin_invite",
portalId: "portal_1",
portalUserId: "portal_user_1",
portalTokenId: "portal_token_1",
setupTokenHash,
expiresAt: "2026-07-25T12:00:00.000Z",
mailContentHash: hashPortalSetupMailContent({ recipient, subject, text, html }),
};
function encryptedPayload() {
return {
tenantId: "tenant_1",
to: [recipient],
subject,
text,
html,
metadata: {
source: PORTAL_SETUP_MAIL_SOURCE,
...baseMetadata,
encryptedCapability: "v1.encrypted-capability",
},
};
}
describe("portal setup mail contract", () => {
it("round-trips a capability envelope only for the exact tenant and mail identity", () => {
const plaintext = buildPortalSetupCapabilityPlaintext({ tenantId: "tenant_1", rawToken, metadata: baseMetadata });
const expected = { tenantId: "tenant_1", metadata: encryptedPayload().metadata };
expect(readPortalSetupCapabilityPlaintext(plaintext, expected)).toBe(rawToken);
expect(() => readPortalSetupCapabilityPlaintext(plaintext, { ...expected, tenantId: "tenant_2" })).toThrow(/identity/u);
expect(() =>
readPortalSetupCapabilityPlaintext(plaintext, {
...expected,
metadata: { ...expected.metadata, portalUserId: "portal_user_2" },
}),
).toThrow(/identity/u);
expect(() =>
readPortalSetupCapabilityPlaintext(
JSON.stringify({ ...(JSON.parse(plaintext) as Record<string, unknown>), unexpected: rawToken }),
expected,
),
).toThrow(/identity/u);
});
it("accepts only the exact encrypted placeholder payload", () => {
const payload = encryptedPayload();
expect(hasValidEncryptedPortalSetupMailContent(payload)).toBe(true);
expect(hasValidEncryptedPortalSetupMailContent({ ...payload, rawToken })).toBe(false);
expect(hasValidEncryptedPortalSetupMailContent({ ...payload, attachments: [] })).toBe(false);
expect(hasValidEncryptedPortalSetupMailContent({ ...payload, cc: ["copy@example.test"] })).toBe(false);
expect(hasValidEncryptedPortalSetupMailContent({ ...payload, text: `${text} changed` })).toBe(false);
expect(
hasValidEncryptedPortalSetupMailContent({
...payload,
metadata: { ...payload.metadata, unexpected: "value" },
}),
).toBe(false);
});
it("binds the deterministic outbox identity before materializing the raw token", () => {
const bound = bindEncryptedPortalSetupMailToOutbox(encryptedPayload(), {
outboxEventId: "outbox_1",
dispatchKey: "portal-setup:portal_token_1",
});
const materialized = materializePortalSetupMailContent(bound, rawToken);
expect(hasValidMaterializedPortalSetupMailContent(materialized)).toBe(true);
expect(JSON.stringify(materialized)).toContain(`/setup/${rawToken}`);
expect(JSON.stringify(materialized)).not.toContain("encrypted-capability");
expect(findPortalSetupRawToken(materialized.text as string, materialized.html as string, setupTokenHash)).toBe(rawToken);
expect(() =>
bindEncryptedPortalSetupMailToOutbox(encryptedPayload(), {
outboxEventId: "outbox_1",
dispatchKey: "portal-setup:portal_token_2",
}),
).toThrow(/binding/u);
});
it("requires a bound, exact materialized shape and a token matching the persisted hash", () => {
const unbound = materializePortalSetupMailContent(encryptedPayload(), rawToken);
const bound = materializePortalSetupMailContent(
bindEncryptedPortalSetupMailToOutbox(encryptedPayload(), {
outboxEventId: "outbox_1",
dispatchKey: "portal-setup:portal_token_1",
}),
rawToken,
);
expect(hasValidMaterializedPortalSetupMailContent(unbound)).toBe(false);
expect(hasValidMaterializedPortalSetupMailContent({ ...bound, templateData: { rawToken } })).toBe(false);
expect(() => materializePortalSetupMailContent(encryptedPayload(), "x".repeat(43))).toThrow(/token hash/u);
});
it("accepts only the token-free redacted allowlist", () => {
const payload = encryptedPayload();
const redacted = {
tenantId: payload.tenantId,
to: payload.to,
subject: payload.subject,
metadata: {
source: PORTAL_SETUP_MAIL_SOURCE,
...baseMetadata,
[PORTAL_SETUP_SECRET_REDACTED_MARKER]: true,
},
};
expect(hasValidRedactedPortalSetupMailContent(redacted)).toBe(true);
expect(hasValidRedactedPortalSetupMailContent({ ...redacted, html: `<p>${rawToken}</p>` })).toBe(false);
expect(hasValidRedactedPortalSetupMailContent({ ...redacted, rawToken })).toBe(false);
expect(
hasValidRedactedPortalSetupMailContent({
...redacted,
metadata: { ...redacted.metadata, encryptedCapability: "still-retained" },
}),
).toBe(false);
});
});