Media upload for avatars and banners, more work, fix tests

This commit is contained in:
Jesse Wierzbinski 2023-10-19 09:53:59 -10:00
parent 16cfd5d900
commit 460b68c381
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
17 changed files with 360 additions and 139 deletions

View file

@ -37,4 +37,6 @@ describe("Instance", () => {
afterAll(async () => {
await instance.remove();
await AppDataSource.destroy();
});

View file

@ -1,17 +1,27 @@
import { getConfig } from "@config";
import { ConfigType, 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();
const originalConfig = getConfig();
const modifiedConfig: ConfigType = {
...originalConfig,
media: {
...originalConfig.media,
conversion: {
...originalConfig.media.conversion,
convert_images: false,
},
},
};
describe("LocalBackend", () => {
let localBackend: LocalBackend;
let fileName: string;
beforeAll(() => {
localBackend = new LocalBackend();
localBackend = new LocalBackend(modifiedConfig);
});
afterAll(async () => {
@ -25,7 +35,7 @@ describe("LocalBackend", () => {
});
const hash = await localBackend.addMedia(media);
fileName = `${hash}`;
fileName = hash;
expect(hash).toBeDefined();
});
@ -33,16 +43,14 @@ describe("LocalBackend", () => {
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");
const media = await localBackend.getMediaByHash(fileName);
expect(media).toBeInstanceOf(File);
});
it("should return null if the file does not exist", async () => {
const media = await localBackend.getMediaByHash(
"does-not-exist",
"txt"
);
const media =
await localBackend.getMediaByHash("does-not-exist.txt");
expect(media).toBeNull();
});
@ -50,12 +58,12 @@ describe("LocalBackend", () => {
});
describe("S3Backend", () => {
const s3Backend = new S3Backend(config);
const s3Backend = new S3Backend(modifiedConfig);
let fileName: string;
afterAll(async () => {
const command = new DeleteObjectCommand({
Bucket: config.s3.bucket_name,
Bucket: modifiedConfig.s3.bucket_name,
Key: fileName,
});
@ -69,7 +77,7 @@ describe("S3Backend", () => {
});
const hash = await s3Backend.addMedia(media);
fileName = `${hash}`;
fileName = hash;
expect(hash).toBeDefined();
});
@ -77,16 +85,13 @@ describe("S3Backend", () => {
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");
const media = await s3Backend.getMediaByHash(fileName);
expect(media).toBeInstanceOf(File);
});
it("should return null if the file does not exist", async () => {
const media = await s3Backend.getMediaByHash(
"does-not-exist",
"txt"
);
const media = await s3Backend.getMediaByHash("does-not-exist.txt");
expect(media).toBeNull();
});