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
+129
@@ -0,0 +1,129 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
class PortalUploadError extends Error {
|
||||
constructor(readonly code: string) {
|
||||
super(code);
|
||||
}
|
||||
}
|
||||
class RequestBodyLimitError extends Error {
|
||||
constructor(readonly limitBytes: number) {
|
||||
super("request body too large");
|
||||
}
|
||||
}
|
||||
return {
|
||||
PortalUploadError,
|
||||
RequestBodyLimitError,
|
||||
requireSession: vi.fn(),
|
||||
validateCsrf: vi.fn(),
|
||||
uploadFiles: vi.fn(),
|
||||
readLimitedFormData: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("~/services/customer-portal/portal-data.server", () => ({
|
||||
requirePortalSessionContext: mocks.requireSession,
|
||||
portalNotFound: () => new Response(null, { status: 404 }),
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-csrf.server", () => ({ validatePortalCsrfToken: mocks.validateCsrf }));
|
||||
vi.mock("~/utils/http/readLimitedRequestBody.server", () => ({
|
||||
RequestBodyLimitError: mocks.RequestBodyLimitError,
|
||||
readLimitedRequestFormData: mocks.readLimitedFormData,
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-upload.server", () => ({
|
||||
PORTAL_UPLOAD_ABSOLUTE_MAX_FILE_BYTES: 100 * 1024 * 1024,
|
||||
PORTAL_UPLOAD_ABSOLUTE_REQUEST_BYTES: 101 * 1024 * 1024,
|
||||
PORTAL_UPLOAD_DEFAULT_MAX_FILES: 10,
|
||||
PortalUploadError: mocks.PortalUploadError,
|
||||
uploadPortalFiles: mocks.uploadFiles,
|
||||
}));
|
||||
|
||||
import { action } from "./project-upload";
|
||||
|
||||
const session = {
|
||||
identity: { tenantId: "tenant-a", portalUserId: "portal-user-a", tenantFrozen: false },
|
||||
responseHeaders: new Headers({ "Set-Cookie": "portal=renewed" }),
|
||||
};
|
||||
|
||||
function args(request: Request) {
|
||||
return { request, params: { tenant: "acme", projectId: "project-a" }, context: {} } as never;
|
||||
}
|
||||
|
||||
function uploadRequest(files: readonly File[]): Request {
|
||||
const form = new FormData();
|
||||
form.set("csrf", "csrf-token");
|
||||
form.set("idempotencyKey", "upload-key-a");
|
||||
for (const file of files) form.append("files", file);
|
||||
mocks.readLimitedFormData.mockResolvedValueOnce(form);
|
||||
const request = new Request("https://portal.example.test/acme/projects/project-a/upload", { method: "POST" });
|
||||
return request;
|
||||
}
|
||||
|
||||
function testFile(contents: string, name: string, type: string): File {
|
||||
const file = new File([contents], name, { type });
|
||||
Object.defineProperty(file, "arrayBuffer", {
|
||||
value: async () => new TextEncoder().encode(contents).buffer,
|
||||
});
|
||||
return file;
|
||||
}
|
||||
|
||||
describe("customer portal project upload route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.requireSession.mockResolvedValue(session);
|
||||
mocks.validateCsrf.mockResolvedValue(undefined);
|
||||
mocks.uploadFiles.mockResolvedValue([{ uploadId: "upload-a", fileId: "file-a" }]);
|
||||
});
|
||||
|
||||
it("forwards every selected file through the portal session after CSRF validation", async () => {
|
||||
const request = uploadRequest([testFile("photo", "baustelle.jpg", "image/jpeg"), testFile("voice", "memo.mp3", "audio/mpeg")]);
|
||||
const response = await action(args(request));
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(mocks.validateCsrf).toHaveBeenCalledWith(request, expect.any(FormData));
|
||||
expect(mocks.readLimitedFormData).toHaveBeenCalledWith(request, 101 * 1024 * 1024);
|
||||
expect(mocks.uploadFiles).toHaveBeenCalledWith({
|
||||
identity: session.identity,
|
||||
projectId: "project-a",
|
||||
idempotencyKey: "upload-key-a",
|
||||
files: [
|
||||
{ name: "baustelle.jpg", mimeType: "image/jpeg", bytes: expect.any(Uint8Array) },
|
||||
{ name: "memo.mp3", mimeType: "audio/mpeg", bytes: expect.any(Uint8Array) },
|
||||
],
|
||||
});
|
||||
await expect(response.json()).resolves.toMatchObject({ ok: true, messageKey: "customerPortal.upload.success" });
|
||||
});
|
||||
|
||||
it("returns a stable M002 key before the service when more than ten files are posted", async () => {
|
||||
const files = Array.from({ length: 11 }, (_, index) => testFile(String(index), `file-${index}.jpg`, "image/jpeg"));
|
||||
const response = await action(args(uploadRequest(files)));
|
||||
|
||||
expect(response.status).toBe(413);
|
||||
await expect(response.json()).resolves.toEqual({ ok: false, errorKey: "customerPortal.upload.errors.tooManyFiles" });
|
||||
expect(mocks.uploadFiles).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects a streamed body that exceeds the absolute request limit before parsing files", async () => {
|
||||
mocks.readLimitedFormData.mockRejectedValueOnce(new mocks.RequestBodyLimitError(101 * 1024 * 1024));
|
||||
const request = new Request("https://portal.example.test/acme/projects/project-a/upload", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "multipart/form-data; boundary=pilot" },
|
||||
body: "--pilot--",
|
||||
});
|
||||
|
||||
const response = await action(args(request));
|
||||
|
||||
expect(response.status).toBe(413);
|
||||
expect(response.headers.get("set-cookie")).toBe("portal=renewed");
|
||||
await expect(response.json()).resolves.toEqual({ ok: false, errorKey: "customerPortal.upload.errors.requestTooLarge" });
|
||||
expect(mocks.validateCsrf).not.toHaveBeenCalled();
|
||||
expect(mocks.uploadFiles).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("maps frozen and revoked project access to a non-enumerating 404", async () => {
|
||||
mocks.uploadFiles.mockRejectedValueOnce(new mocks.PortalUploadError("PORTAL_FROZEN"));
|
||||
await expect(action(args(uploadRequest([testFile("photo", "photo.jpg", "image/jpeg")])))).rejects.toMatchObject({ status: 404 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user