import { count, createUser, db, del, formWithFile, get, insertDump, PNG, post, test, } from "../helpers.ts"; import type { AttachmentResponse } from "../../../shared/dump.ts"; import type { Comment } from "../../../shared/comment.ts"; import { assertEquals } from "@std/assert"; import { runStartupTasks } from "../../db/startup.ts"; test("attachments: upload an image, serve it back", async () => { const user = await createUser(); const res = await post("/api/attachments", { auth: user.auth, form: formWithFile(PNG), }); assertEquals(res.status, 201); const id = res.data.id; const served = await get(`/api/attachments/${id}`); assertEquals(served.status, 200); assertEquals(served.bytes, PNG); assertEquals(served.headers.get("content-type"), "image/png"); assertEquals(served.headers.get("x-content-type-options"), "nosniff"); assertEquals(served.headers.get("cache-control"), "public, max-age=3600"); assertEquals( (await get(`/api/attachments/${crypto.randomUUID()}`)).status, 404, ); }); test("attachments: refuses non-images, non-multipart and anonymous uploads", async () => { const user = await createUser(); assertEquals( (await post("/api/attachments", { auth: user.auth, form: formWithFile( new TextEncoder().encode("