mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Add more cases for media backend
This commit is contained in:
parent
47a53b6990
commit
16cfd5d900
6 changed files with 1042 additions and 711 deletions
1478
tests/api.test.ts
1478
tests/api.test.ts
File diff suppressed because it is too large
Load diff
94
tests/entities/Media.test.ts
Normal file
94
tests/entities/Media.test.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import { getConfig } from "@config";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
||||
import { LocalBackend, S3Backend } from "~classes/media";
|
||||
import { unlink } from "fs/promises";
|
||||
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
describe("LocalBackend", () => {
|
||||
let localBackend: LocalBackend;
|
||||
let fileName: string;
|
||||
|
||||
beforeAll(() => {
|
||||
localBackend = new LocalBackend();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await unlink(`${process.cwd()}/uploads/${fileName}`);
|
||||
});
|
||||
|
||||
describe("addMedia", () => {
|
||||
it("should write the file to the local filesystem and return the hash", async () => {
|
||||
const media = new File(["test"], "test.txt", {
|
||||
type: "text/plain",
|
||||
});
|
||||
|
||||
const hash = await localBackend.addMedia(media);
|
||||
fileName = `${hash}`;
|
||||
|
||||
expect(hash).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMediaByHash", () => {
|
||||
it("should retrieve the file from the local filesystem and return it as a File object", async () => {
|
||||
const media = await localBackend.getMediaByHash(fileName, "txt");
|
||||
|
||||
expect(media).toBeInstanceOf(File);
|
||||
});
|
||||
|
||||
it("should return null if the file does not exist", async () => {
|
||||
const media = await localBackend.getMediaByHash(
|
||||
"does-not-exist",
|
||||
"txt"
|
||||
);
|
||||
|
||||
expect(media).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("S3Backend", () => {
|
||||
const s3Backend = new S3Backend(config);
|
||||
let fileName: string;
|
||||
|
||||
afterAll(async () => {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: config.s3.bucket_name,
|
||||
Key: fileName,
|
||||
});
|
||||
|
||||
await s3Backend.client.send(command);
|
||||
});
|
||||
|
||||
describe("addMedia", () => {
|
||||
it("should write the file to the S3 bucket and return the hash", async () => {
|
||||
const media = new File(["test"], "test.txt", {
|
||||
type: "text/plain",
|
||||
});
|
||||
|
||||
const hash = await s3Backend.addMedia(media);
|
||||
fileName = `${hash}`;
|
||||
|
||||
expect(hash).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMediaByHash", () => {
|
||||
it("should retrieve the file from the S3 bucket and return it as a File object", async () => {
|
||||
const media = await s3Backend.getMediaByHash(fileName, "txt");
|
||||
|
||||
expect(media).toBeInstanceOf(File);
|
||||
});
|
||||
|
||||
it("should return null if the file does not exist", async () => {
|
||||
const media = await s3Backend.getMediaByHash(
|
||||
"does-not-exist",
|
||||
"txt"
|
||||
);
|
||||
|
||||
expect(media).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue