feat: connect resource library workflows
This commit is contained in:
94
tests/app/api/library-uploads-presign.route.test.ts
Normal file
94
tests/app/api/library-uploads-presign.route.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { beforeEach, expect, test, vi } from "vitest";
|
||||
|
||||
import { POST } from "../../../app/api/libraries/uploads/presign/route";
|
||||
|
||||
const { backendRequestMock } = vi.hoisted(() => ({
|
||||
backendRequestMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/http/backend-client", () => ({
|
||||
backendRequest: backendRequestMock,
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
backendRequestMock.mockReset();
|
||||
});
|
||||
|
||||
test("proxies upload presign requests and normalizes the response for the frontend", async () => {
|
||||
backendRequestMock.mockResolvedValue({
|
||||
status: 200,
|
||||
data: {
|
||||
method: "PUT",
|
||||
upload_url: "https://s3.example.com/presigned-put",
|
||||
headers: {
|
||||
"content-type": "image/png",
|
||||
},
|
||||
storage_key: "library/models/2026/ava-original.png",
|
||||
public_url: "https://images.example.com/library/models/2026/ava-original.png",
|
||||
},
|
||||
});
|
||||
|
||||
const response = await POST(
|
||||
new Request("http://localhost/api/libraries/uploads/presign", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
resourceType: "model",
|
||||
fileRole: "original",
|
||||
fileName: "ava-original.png",
|
||||
contentType: "image/png",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const payload = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(payload).toEqual({
|
||||
mode: "proxy",
|
||||
data: {
|
||||
method: "PUT",
|
||||
uploadUrl: "https://s3.example.com/presigned-put",
|
||||
headers: {
|
||||
"content-type": "image/png",
|
||||
},
|
||||
storageKey: "library/models/2026/ava-original.png",
|
||||
publicUrl: "https://images.example.com/library/models/2026/ava-original.png",
|
||||
},
|
||||
});
|
||||
expect(backendRequestMock).toHaveBeenCalledWith("/library/uploads/presign", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
resource_type: "model",
|
||||
file_role: "original",
|
||||
file_name: "ava-original.png",
|
||||
content_type: "image/png",
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
test("rejects invalid presign payloads before proxying", async () => {
|
||||
const response = await POST(
|
||||
new Request("http://localhost/api/libraries/uploads/presign", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
resourceType: "unknown",
|
||||
fileRole: "original",
|
||||
fileName: "",
|
||||
contentType: "image/png",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const payload = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(payload).toMatchObject({
|
||||
error: "VALIDATION_ERROR",
|
||||
message: "上传参数不合法。",
|
||||
});
|
||||
expect(backendRequestMock).not.toHaveBeenCalled();
|
||||
});
|
||||
Reference in New Issue
Block a user