Files

97 lines
4.2 KiB
TypeScript

// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
import { signClickTarget } from "@hivegrid/email-sequences/click-tracking-token";
const mocks = vi.hoisted(() => ({ recordEmailClick: vi.fn(), resolveOutboundEmailClickRlsIdentity: vi.fn() }));
vi.mock("@hivegrid/db/email-tracking.server", async (importOriginal) => {
const actual = await importOriginal<typeof import("@hivegrid/db/email-tracking.server")>();
return { ...actual, recordEmailClick: mocks.recordEmailClick };
});
vi.mock("@hivegrid/db/rls-resolvers.server", () => ({
resolveOutboundEmailClickRlsIdentity: mocks.resolveOutboundEmailClickRlsIdentity,
}));
vi.mock("@hivegrid/db/rls-context.server", () => ({
runWithRlsContext: (_context: unknown, callback: () => unknown) => callback(),
}));
vi.mock("~/utils/db.server", () => ({ db: {} }));
vi.mock("~/utils/server/IpUtils", () => ({ getTrustedClientIpOrNull: () => "203.0.113.10" }));
vi.mock("~/locale/i18next.server", () => ({
getTranslations: vi.fn(async () => ({ t: () => "This link is invalid or has expired." })),
}));
const { loader } = await import("./e.c.$outboundEmailId");
const secret = "0123456789abcdef0123456789abcdef0123456789abcdef";
const outboundEmailId = "outbound_email_1";
const targetUrl = "https://destination.example/path";
beforeEach(() => {
vi.clearAllMocks();
process.env.MARKETING_TOKEN_SECRET = secret;
mocks.recordEmailClick.mockResolvedValue({ status: "recorded" });
mocks.resolveOutboundEmailClickRlsIdentity.mockResolvedValue({ tenantId: "tenant_1", resourceId: outboundEmailId });
});
describe("outbound email click tracking redirect", () => {
it("rejects an unsigned target without recording a click", async () => {
const response = await loader({
params: { outboundEmailId },
request: new Request(`https://app.example/e/c/${outboundEmailId}?u=${encodeURIComponent(targetUrl)}`),
} as never);
expect(response.status).toBe(400);
await expect(response.text()).resolves.toBe("This link is invalid or has expired.");
expect(mocks.recordEmailClick).not.toHaveBeenCalled();
});
it("accepts only a target bound to the outbound email id", async () => {
const signature = signClickTarget({ deliveryId: outboundEmailId, targetUrl, secret });
const url = new URL(`https://app.example/e/c/${outboundEmailId}`);
url.searchParams.set("u", targetUrl);
url.searchParams.set("s", signature);
const response = await loader({ params: { outboundEmailId }, request: new Request(url) } as never);
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(targetUrl);
expect(response.headers.get("Cache-Control")).toBe("no-store");
expect(response.headers.get("Referrer-Policy")).toBe("no-referrer");
expect(mocks.resolveOutboundEmailClickRlsIdentity).toHaveBeenCalledWith(outboundEmailId);
expect(mocks.recordEmailClick).toHaveBeenCalledWith(
expect.objectContaining({ tenantId: "tenant_1", outboundEmailId }),
expect.anything(),
);
expect(mocks.recordEmailClick).toHaveBeenCalledOnce();
});
it("still redirects but never writes when the signed id has no tenant identity", async () => {
mocks.resolveOutboundEmailClickRlsIdentity.mockResolvedValue(null);
const signature = signClickTarget({ deliveryId: outboundEmailId, targetUrl, secret });
const url = new URL(`https://app.example/e/c/${outboundEmailId}`);
url.searchParams.set("u", targetUrl);
url.searchParams.set("s", signature);
const response = await loader({ params: { outboundEmailId }, request: new Request(url) } as never);
expect(response.status).toBe(302);
expect(mocks.recordEmailClick).not.toHaveBeenCalled();
});
it("rejects a signature issued for a different outbound email", async () => {
const signature = signClickTarget({ deliveryId: "different_email", targetUrl, secret });
const url = new URL(`https://app.example/e/c/${outboundEmailId}`);
url.searchParams.set("u", targetUrl);
url.searchParams.set("s", signature);
const response = await loader({ params: { outboundEmailId }, request: new Request(url) } as never);
expect(response.status).toBe(400);
expect(mocks.recordEmailClick).not.toHaveBeenCalled();
});
});