import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ requireIdentity: vi.fn(), getReleasedFile: vi.fn(), createDownloadUrl: vi.fn(), assertTenantKey: vi.fn(), auditDownload: vi.fn(), recordDownload: vi.fn(), })); vi.mock("~/services/customer-portal/portal-data.server", () => ({ requirePortalSessionContext: mocks.requireIdentity, getPortalReleasedFile: mocks.getReleasedFile, auditPortalDocumentDownload: mocks.auditDownload, portalNotFound: () => new Response(null, { status: 404 }), })); vi.mock("~/utils/integrations/mediaSigning.server", () => ({ createMediaDownloadUrl: mocks.createDownloadUrl })); vi.mock("~/lib/metrics/storage-traffic", () => ({ recordAuthorizedStorageDownload: mocks.recordDownload })); vi.mock("@hivegrid/storage", async (importOriginal) => ({ ...(await importOriginal()), assertFileKeyBelongsToTenant: mocks.assertTenantKey, })); import { loader } from "./document-download"; const identity = { tenantId: "tenant-a", tenantSlug: "acme" }; const cleanFile = { storageKey: "tenants/tenant-a/files/release.pdf", displayName: "Freigabe.pdf", scanStatus: "clean", }; describe("customer portal document download", () => { beforeEach(() => { vi.clearAllMocks(); mocks.requireIdentity.mockResolvedValue({ identity, responseHeaders: { "Cache-Control": "private, no-store" } }); mocks.getReleasedFile.mockResolvedValue(cleanFile); mocks.createDownloadUrl.mockResolvedValue("https://objects.example.test/signed"); mocks.auditDownload.mockResolvedValue(undefined); }); it("signs only a clean, tenant-owned released file", async () => { await expect( loader({ request: new Request("https://portal.hivegrid.test/acme/documents/release-a"), params: { tenant: "acme", releaseId: "release-a" }, context: {}, }) ).rejects.toMatchObject({ status: 302 }); expect(mocks.assertTenantKey).toHaveBeenCalledWith("tenant-a", cleanFile.storageKey, "download"); expect(mocks.createDownloadUrl).toHaveBeenCalledWith({ key: cleanFile.storageKey, filename: cleanFile.displayName, ttlSeconds: 60, }); expect(mocks.auditDownload).toHaveBeenCalledWith(identity, "release-a", expect.any(Request)); expect(mocks.auditDownload.mock.invocationCallOrder[0]).toBeGreaterThan(mocks.createDownloadUrl.mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER); expect(mocks.recordDownload).toHaveBeenCalledWith({ surface: "portal_document", delivery: "signed_redirect", objectSizeBytes: null, }); expect(mocks.recordDownload.mock.invocationCallOrder[0]).toBeGreaterThan(mocks.auditDownload.mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER); }); it.each(["pending", "infected", "error"])("returns 404 for a %s scanner verdict", async (scanStatus) => { mocks.getReleasedFile.mockResolvedValue({ ...cleanFile, scanStatus }); await expect( loader({ request: new Request("https://portal.hivegrid.test/acme/documents/release-a"), params: { tenant: "acme", releaseId: "release-a" }, context: {}, }) ).rejects.toMatchObject({ status: 404 }); expect(mocks.createDownloadUrl).not.toHaveBeenCalled(); expect(mocks.auditDownload).not.toHaveBeenCalled(); expect(mocks.recordDownload).not.toHaveBeenCalled(); }); it("returns 404 when the storage key does not belong to the tenant", async () => { mocks.assertTenantKey.mockImplementationOnce(() => { throw new Error("foreign key"); }); await expect( loader({ request: new Request("https://portal.hivegrid.test/acme/documents/release-a"), params: { tenant: "acme", releaseId: "release-a" }, context: {}, }) ).rejects.toMatchObject({ status: 404 }); expect(mocks.createDownloadUrl).not.toHaveBeenCalled(); expect(mocks.auditDownload).not.toHaveBeenCalled(); expect(mocks.recordDownload).not.toHaveBeenCalled(); }); it("does not count a URL whose required audit write fails before delivery", async () => { mocks.auditDownload.mockRejectedValueOnce(new Error("audit unavailable")); await expect( loader({ request: new Request("https://portal.hivegrid.test/acme/documents/release-a"), params: { tenant: "acme", releaseId: "release-a" }, context: {}, }) ).rejects.toThrow("audit unavailable"); expect(mocks.recordDownload).not.toHaveBeenCalled(); }); });