Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+8
@@ -0,0 +1,8 @@
|
||||
// AK-SET-01 — legacy `settings/subscription` now lives at `settings/billing`.
|
||||
import { legacySettingsRedirectLoader } from "../legacy-redirects.server";
|
||||
|
||||
export const loader = legacySettingsRedirectLoader("settings/billing");
|
||||
|
||||
export default function LegacySubscriptionRedirect() {
|
||||
return null;
|
||||
}
|
||||
+34
-4
@@ -12,6 +12,9 @@ const mocks = vi.hoisted(() => ({
|
||||
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("@hivegrid/config", () => ({
|
||||
loadWebConfig: () => ({ config: { lago: { apiUrl: "https://files.example.test" } } }),
|
||||
}));
|
||||
vi.mock("~/services/billing/customer-invoices.server", () => ({
|
||||
CustomerBillingInvoiceError: class CustomerBillingInvoiceError extends Error {
|
||||
override readonly name = "CustomerBillingInvoiceError";
|
||||
@@ -26,9 +29,9 @@ vi.mock("~/services/billing/customer-invoices.server", () => ({
|
||||
|
||||
import { loader } from "./invoices.$invoiceId.download";
|
||||
|
||||
function callLoader(params: Record<string, string | undefined> = { tenant: "acme", invoiceId: "invoice_1" }) {
|
||||
function callLoader(params: Record<string, string | undefined> = { tenant: "acme", invoiceId: "invoice_1" }, search = "") {
|
||||
return loader({
|
||||
request: new Request("https://app.example.test/acme/settings/subscription/invoices/invoice_1/download"),
|
||||
request: new Request(`https://app.example.test/acme/settings/subscription/invoices/invoice_1/download${search}`),
|
||||
params,
|
||||
context: {},
|
||||
unstable_pattern: "app.$tenant/settings/subscription/invoices.$invoiceId.download",
|
||||
@@ -64,7 +67,31 @@ describe("customer invoice download route", () => {
|
||||
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,*/*" } });
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
new URL("https://files.example.test/invoice_1.pdf"),
|
||||
expect.objectContaining({
|
||||
headers: { accept: "application/pdf,application/octet-stream;q=0.9" },
|
||||
redirect: "manual",
|
||||
signal: expect.any(AbortSignal),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("serves the PDF inline for the in-app preview", async () => {
|
||||
const response = (await callLoader(undefined, "?disposition=inline")) as Response;
|
||||
|
||||
expect(response.headers.get("Content-Disposition")).toBe('inline; filename="invoice_1.pdf"');
|
||||
});
|
||||
|
||||
it("keeps a non-PDF upstream body as an attachment even when inline is requested", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("<script>alert(1)</script>", { headers: { "content-type": "text/html" } }))
|
||||
);
|
||||
|
||||
const response = (await callLoader(undefined, "?disposition=inline")) as Response;
|
||||
|
||||
expect(response.headers.get("Content-Disposition")).toBe('attachment; filename="invoice_1.pdf"');
|
||||
});
|
||||
|
||||
it("returns a localized 404 when the invoice does not belong to the tenant", async () => {
|
||||
@@ -77,7 +104,10 @@ describe("customer invoice download route", () => {
|
||||
});
|
||||
|
||||
it("returns a localized 404 when the upstream PDF cannot be loaded", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response("missing", { status: 404 })));
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("missing", { status: 404 }))
|
||||
);
|
||||
|
||||
await expect(callLoader()).rejects.toMatchObject({
|
||||
status: 404,
|
||||
|
||||
+26
-5
@@ -1,6 +1,8 @@
|
||||
import { type LoaderFunctionArgs } from "react-router";
|
||||
import { loadWebConfig } from "@hivegrid/config";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getCustomerBillingInvoiceDownloadTarget, CustomerBillingInvoiceError } from "~/services/billing/customer-invoices.server";
|
||||
import { createBoundedInvoiceDownloadBody, fetchInvoiceDownloadFile } from "~/services/billing/invoice-download-fetch.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
|
||||
@@ -20,6 +22,19 @@ function contentDispositionFilename(filename: string): string {
|
||||
return filename.replace(/["\\\r\n]/gu, "_");
|
||||
}
|
||||
|
||||
/**
|
||||
* `?disposition=inline` lets the invoice render in the in-app preview frame
|
||||
* instead of triggering a save dialog. Inline is granted only for a real PDF:
|
||||
* the file is streamed from our own origin, so an unexpected HTML body would
|
||||
* otherwise run as first-party script.
|
||||
*/
|
||||
function contentDisposition(request: Request, contentType: string, filename: string): string {
|
||||
const wantsInline = new URL(request.url).searchParams.get("disposition") === "inline";
|
||||
const isPdf = contentType.split(";", 1)[0]?.trim().toLowerCase() === "application/pdf";
|
||||
const mode = wantsInline && isPdf ? "inline" : "attachment";
|
||||
return `${mode}; filename="${contentDispositionFilename(filename)}"`;
|
||||
}
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, "app.settings.subscription.invoices.view", tenantId);
|
||||
@@ -32,16 +47,22 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
|
||||
try {
|
||||
const target = await getCustomerBillingInvoiceDownloadTarget({ tenantId, invoiceId });
|
||||
const upstream = await fetch(target.fileUrl, { headers: { accept: "application/pdf,*/*" } });
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
const upstream = await fetchInvoiceDownloadFile(target.fileUrl, {
|
||||
trustedProviderOrigin: loadWebConfig().config.lago.apiUrl,
|
||||
});
|
||||
if (!upstream.ok) {
|
||||
throw new CustomerBillingInvoiceError("DOWNLOAD_UNAVAILABLE");
|
||||
}
|
||||
return new Response(upstream.body, {
|
||||
const body = createBoundedInvoiceDownloadBody(upstream);
|
||||
if (!body) throw new CustomerBillingInvoiceError("DOWNLOAD_UNAVAILABLE");
|
||||
const contentType = upstream.headers.get("content-type") || "application/pdf";
|
||||
return new Response(body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": upstream.headers.get("content-type") || "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${contentDispositionFilename(target.filename)}"`,
|
||||
"Content-Type": contentType,
|
||||
"Content-Disposition": contentDisposition(request, contentType, target.filename),
|
||||
"Cache-Control": "private, no-store",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user