Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

116 lines
3.1 KiB
TypeScript

/**
* Integration tests for the configurable media upload size limit.
*
* Starts a server with maxUploadSize=1 MB and verifies that both
* upload paths (direct multipart and signed-URL) enforce the limit.
*/
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { assertNodeVersion, createTestServer, type TestServerContext } from "../server.js";
const PORT = 4400;
const ONE_MB = 1024 * 1024;
let ctx: TestServerContext;
beforeAll(async () => {
assertNodeVersion();
ctx = await createTestServer({
port: PORT,
seed: false,
env: { EMDASH_MAX_UPLOAD_SIZE: String(ONE_MB) },
});
}, 120_000);
afterAll(async () => {
await ctx?.cleanup();
});
describe("direct multipart upload", () => {
it("rejects a file that exceeds maxUploadSize with 413", async () => {
const bigFile = new File([new Uint8Array(2 * ONE_MB)], "big.pdf", {
type: "application/pdf",
});
const body = new FormData();
body.append("file", bigFile);
const res = await fetch(`${ctx.baseUrl}/_emdash/api/media`, {
method: "POST",
headers: {
Authorization: `Bearer ${ctx.token}`,
"X-EmDash-Request": "1",
},
body,
});
expect(res.status).toBe(413);
const json = (await res.json()) as { error: { code: string } };
expect(json.error.code).toBe("PAYLOAD_TOO_LARGE");
});
it("accepts a file within maxUploadSize", async () => {
const smallFile = new File([new Uint8Array(512 * 1024)], "small.pdf", {
type: "application/pdf",
});
const body = new FormData();
body.append("file", smallFile);
const res = await fetch(`${ctx.baseUrl}/_emdash/api/media`, {
method: "POST",
headers: {
Authorization: `Bearer ${ctx.token}`,
"X-EmDash-Request": "1",
},
body,
});
// 201 = created successfully
expect(res.status).toBe(201);
});
});
describe("signed-URL upload (upload-url endpoint)", () => {
it("rejects a declared size that exceeds maxUploadSize with 400", async () => {
const res = await fetch(`${ctx.baseUrl}/_emdash/api/media/upload-url`, {
method: "POST",
headers: {
Authorization: `Bearer ${ctx.token}`,
"X-EmDash-Request": "1",
"Content-Type": "application/json",
},
body: JSON.stringify({
filename: "big.pdf",
contentType: "application/pdf",
size: 2 * ONE_MB,
}),
});
expect(res.status).toBe(400);
const json = (await res.json()) as { error: { code: string } };
expect(json.error.code).toBe("VALIDATION_ERROR");
});
it("passes size validation for a declared size within maxUploadSize", async () => {
// Local storage does not support signed URLs, so a valid-size request
// proceeds past Zod validation and fails later with 501.
const res = await fetch(`${ctx.baseUrl}/_emdash/api/media/upload-url`, {
method: "POST",
headers: {
Authorization: `Bearer ${ctx.token}`,
"X-EmDash-Request": "1",
"Content-Type": "application/json",
},
body: JSON.stringify({
filename: "ok.pdf",
contentType: "application/pdf",
size: 512 * 1024,
}),
});
// 501 means the request passed size validation and hit the storage layer.
// A size-rejection would produce 400.
expect(res.status).toBe(501);
});
});