mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
tests: ensuring overwrite is passed and respected in media api
This commit is contained in:
@@ -8,6 +8,7 @@ import type { TAppMediaConfig } from "../../src/media/media-schema";
|
||||
import { StorageLocalAdapter } from "adapter/node/storage/StorageLocalAdapter";
|
||||
import { assetsPath, assetsTmpPath } from "../helper";
|
||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
||||
import * as proto from "data/prototype";
|
||||
|
||||
beforeAll(() => {
|
||||
disableConsoleLog();
|
||||
@@ -128,4 +129,87 @@ describe("MediaController", () => {
|
||||
expect(destFile.exists()).resolves.toBe(true);
|
||||
await destFile.delete();
|
||||
});
|
||||
|
||||
test("entity upload with max_items and overwrite", async () => {
|
||||
const app = createApp({
|
||||
config: {
|
||||
media: mergeObject(
|
||||
{
|
||||
enabled: true,
|
||||
adapter: {
|
||||
type: "local",
|
||||
config: {
|
||||
path: assetsTmpPath,
|
||||
},
|
||||
},
|
||||
},
|
||||
{},
|
||||
),
|
||||
data: {
|
||||
entities: {
|
||||
posts: proto
|
||||
.entity("posts", {
|
||||
title: proto.text(),
|
||||
cover: proto.medium(),
|
||||
})
|
||||
.toJSON(),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
await app.build();
|
||||
|
||||
// create a post first
|
||||
const createRes = await app.server.request("/api/data/entity/posts", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ title: "Test Post" }),
|
||||
});
|
||||
expect(createRes.status).toBe(201);
|
||||
const { data: post } = (await createRes.json()) as any;
|
||||
|
||||
const file = Bun.file(path);
|
||||
const uploadedFiles: string[] = [];
|
||||
|
||||
// upload first file to entity (should succeed)
|
||||
const res1 = await app.server.request(`/api/media/entity/posts/${post.id}/cover`, {
|
||||
method: "POST",
|
||||
body: file,
|
||||
});
|
||||
expect(res1.status).toBe(201);
|
||||
const result1 = (await res1.json()) as any;
|
||||
uploadedFiles.push(result1.name);
|
||||
|
||||
// upload second file without overwrite (should fail - max_items reached)
|
||||
const res2 = await app.server.request(`/api/media/entity/posts/${post.id}/cover`, {
|
||||
method: "POST",
|
||||
body: file,
|
||||
});
|
||||
expect(res2.status).toBe(400);
|
||||
const result2 = (await res2.json()) as any;
|
||||
expect(result2.error).toContain("Max items");
|
||||
|
||||
// upload third file with overwrite=true (should succeed and delete old file)
|
||||
const res3 = await app.server.request(
|
||||
`/api/media/entity/posts/${post.id}/cover?overwrite=true`,
|
||||
{
|
||||
method: "POST",
|
||||
body: file,
|
||||
},
|
||||
);
|
||||
expect(res3.status).toBe(201);
|
||||
const result3 = (await res3.json()) as any;
|
||||
uploadedFiles.push(result3.name);
|
||||
|
||||
// verify old file was deleted from storage
|
||||
const oldFile = Bun.file(assetsTmpPath + "/" + uploadedFiles[0]);
|
||||
expect(await oldFile.exists()).toBe(false);
|
||||
|
||||
// verify new file exists
|
||||
const newFile = Bun.file(assetsTmpPath + "/" + uploadedFiles[1]);
|
||||
expect(await newFile.exists()).toBe(true);
|
||||
|
||||
// cleanup
|
||||
await newFile.delete();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user