Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+86
@@ -0,0 +1,86 @@
|
||||
// @vitest-environment node
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getTranslations: vi.fn(),
|
||||
getTenantIdFromUrl: vi.fn(),
|
||||
verifyUserHasPermission: vi.fn(),
|
||||
getCustomerBillingInvoiceDownloadTarget: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/locale/i18next.server", () => ({ getTranslations: (...args: unknown[]) => mocks.getTranslations(...args) }));
|
||||
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: (...args: unknown[]) => mocks.getTenantIdFromUrl(...args) }));
|
||||
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: (...args: unknown[]) => mocks.verifyUserHasPermission(...args) }));
|
||||
vi.mock("~/services/billing/customer-invoices.server", () => ({
|
||||
CustomerBillingInvoiceError: class CustomerBillingInvoiceError extends Error {
|
||||
override readonly name = "CustomerBillingInvoiceError";
|
||||
readonly i18nKey: string;
|
||||
constructor(readonly code: "CONFIGURATION_MISSING" | "INVOICE_NOT_FOUND" | "DOWNLOAD_UNAVAILABLE") {
|
||||
super(code);
|
||||
this.i18nKey = `settings.subscription.errors.${code}`;
|
||||
}
|
||||
},
|
||||
getCustomerBillingInvoiceDownloadTarget: (...args: unknown[]) => mocks.getCustomerBillingInvoiceDownloadTarget(...args),
|
||||
}));
|
||||
|
||||
import { loader } from "./invoices.$invoiceId.download";
|
||||
|
||||
function callLoader(params: Record<string, string | undefined> = { tenant: "acme", invoiceId: "invoice_1" }) {
|
||||
return loader({
|
||||
request: new Request("https://app.example.test/acme/settings/subscription/invoices/invoice_1/download"),
|
||||
params,
|
||||
context: {},
|
||||
unstable_pattern: "app.$tenant/settings/subscription/invoices.$invoiceId.download",
|
||||
} as LoaderFunctionArgs);
|
||||
}
|
||||
|
||||
describe("customer invoice download route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.getTranslations.mockResolvedValue({ t: (key: string) => key });
|
||||
mocks.getTenantIdFromUrl.mockResolvedValue("tenant_1");
|
||||
mocks.verifyUserHasPermission.mockResolvedValue(undefined);
|
||||
mocks.getCustomerBillingInvoiceDownloadTarget.mockResolvedValue({
|
||||
fileUrl: "https://files.example.test/invoice_1.pdf",
|
||||
filename: "invoice_1.pdf",
|
||||
});
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("pdf", { headers: { "content-type": "application/pdf" } }))
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("checks invoice permission before streaming the generated Lago PDF", async () => {
|
||||
const response = (await callLoader()) as Response;
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
await expect(response.text()).resolves.toBe("pdf");
|
||||
expect(response.headers.get("Content-Type")).toContain("application/pdf");
|
||||
expect(response.headers.get("Content-Disposition")).toBe('attachment; filename="invoice_1.pdf"');
|
||||
expect(mocks.verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "app.settings.subscription.invoices.view", "tenant_1");
|
||||
expect(mocks.getCustomerBillingInvoiceDownloadTarget).toHaveBeenCalledWith({ tenantId: "tenant_1", invoiceId: "invoice_1" });
|
||||
expect(fetch).toHaveBeenCalledWith("https://files.example.test/invoice_1.pdf", { headers: { accept: "application/pdf,*/*" } });
|
||||
});
|
||||
|
||||
it("returns a localized 404 when the invoice does not belong to the tenant", async () => {
|
||||
const { CustomerBillingInvoiceError } = await import("~/services/billing/customer-invoices.server");
|
||||
mocks.getCustomerBillingInvoiceDownloadTarget.mockRejectedValueOnce(new CustomerBillingInvoiceError("INVOICE_NOT_FOUND"));
|
||||
|
||||
await expect(callLoader()).rejects.toMatchObject({
|
||||
status: 404,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns a localized 404 when the upstream PDF cannot be loaded", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response("missing", { status: 404 })));
|
||||
|
||||
await expect(callLoader()).rejects.toMatchObject({
|
||||
status: 404,
|
||||
});
|
||||
});
|
||||
});
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
import { type LoaderFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getCustomerBillingInvoiceDownloadTarget, CustomerBillingInvoiceError } from "~/services/billing/customer-invoices.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
|
||||
function invoiceErrorStatus(error: unknown): number {
|
||||
if (error instanceof CustomerBillingInvoiceError) {
|
||||
return error.code === "CONFIGURATION_MISSING" ? 503 : 404;
|
||||
}
|
||||
return 503;
|
||||
}
|
||||
|
||||
function invoiceErrorKey(error: unknown): string {
|
||||
if (error instanceof CustomerBillingInvoiceError) return error.i18nKey;
|
||||
return "settings.subscription.errors.DOWNLOAD_UNAVAILABLE";
|
||||
}
|
||||
|
||||
function contentDispositionFilename(filename: string): string {
|
||||
return filename.replace(/["\\\r\n]/gu, "_");
|
||||
}
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, "app.settings.subscription.invoices.view", tenantId);
|
||||
|
||||
const invoiceId = params.invoiceId?.trim();
|
||||
if (!invoiceId) {
|
||||
const { t } = await getTranslations(request);
|
||||
throw new Response(t("settings.subscription.errors.INVOICE_NOT_FOUND"), { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const target = await getCustomerBillingInvoiceDownloadTarget({ tenantId, invoiceId });
|
||||
const upstream = await fetch(target.fileUrl, { headers: { accept: "application/pdf,*/*" } });
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
throw new CustomerBillingInvoiceError("DOWNLOAD_UNAVAILABLE");
|
||||
}
|
||||
return new Response(upstream.body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": upstream.headers.get("content-type") || "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${contentDispositionFilename(target.filename)}"`,
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const { t } = await getTranslations(request);
|
||||
throw new Response(t(invoiceErrorKey(error)), { status: invoiceErrorStatus(error) });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user