feat: add extensive test suite
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
import { adminCheck } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { cdn } from "@/lib/db/schema";
|
||||
import { listFilesService } from "./service";
|
||||
|
||||
export async function listFiles() {
|
||||
if (!(await adminCheck())) throw "Unauthorized";
|
||||
|
||||
return await db
|
||||
.select({ id: cdn.id, name: cdn.name, size: cdn.size, type: cdn.type })
|
||||
.from(cdn);
|
||||
return listFilesService({
|
||||
adminCheck,
|
||||
list: () =>
|
||||
db
|
||||
.select({ id: cdn.id, name: cdn.name, size: cdn.size, type: cdn.type })
|
||||
.from(cdn),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { listFilesService } from "./service";
|
||||
|
||||
describe("listFilesService", () => {
|
||||
test("requires admin before querying CDN files", async () => {
|
||||
const list = mock(async () => []);
|
||||
expect(
|
||||
listFilesService({ adminCheck: async () => false, list }),
|
||||
).rejects.toBe("Unauthorized");
|
||||
expect(list).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("returns the chooser projection from its query", async () => {
|
||||
const files = [
|
||||
{ id: "asset", name: "image.png", size: "4", type: "image/png" },
|
||||
];
|
||||
expect(
|
||||
await listFilesService({
|
||||
adminCheck: async () => true,
|
||||
list: async () => files,
|
||||
}),
|
||||
).toBe(files);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
export async function listFilesService<T>(dependencies: {
|
||||
adminCheck: () => Promise<unknown>;
|
||||
list: () => Promise<T[]>;
|
||||
}) {
|
||||
if (!(await dependencies.adminCheck())) throw "Unauthorized";
|
||||
return dependencies.list();
|
||||
}
|
||||
Reference in New Issue
Block a user