/** * ทดสอบกติกาการอัปโหลดรูป — กัน 3 เคสที่พังเงียบ: * - รับไฟล์ที่ไม่ใช่รูป (เปลี่ยนนามสกุลมา) * - path traversal ตอนเสิร์ฟไฟล์ (`../`) * - ชื่อไฟล์ชนกัน (ต้องมี timestamp + random) */ import { describe, expect, it } from "vitest"; import { MAX_UPLOAD_BYTES, extensionForType, isValidImageUpload, makeUploadName, safeUploadPath, sanitizeSlug, sniffImageType, typeForExtension, } from "@/lib/upload"; const jpeg = new Uint8Array([ 0xff, 0xd8, 0xff, 0xe0, 0, 0x10, 0x4a, 0x46, 0x49, 0x46, 0, 1, ]); const png = new Uint8Array([ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, ]); const webp = new Uint8Array([ 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, ]); const gif = new Uint8Array([ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0, 0, 0, 0, 0, 0, ]); describe("sniffImageType", () => { it("ตรวจ JPEG / PNG / WebP จาก magic bytes", () => { expect(sniffImageType(jpeg)).toBe("image/jpeg"); expect(sniffImageType(png)).toBe("image/png"); expect(sniffImageType(webp)).toBe("image/webp"); }); it("ไฟล์ที่ไม่ใช่รูป (หรือสั้นเกิน) → null", () => { expect(sniffImageType(gif)).toBeNull(); expect(sniffImageType(new Uint8Array([1, 2, 3]))).toBeNull(); }); }); describe("isValidImageUpload", () => { it("รับรูปที่ถูกต้อง", () => { expect(isValidImageUpload(jpeg)).toBe(true); }); it("ปฏิเสธไฟล์ว่างและไฟล์ที่ใหญ่เกินเพดาน", () => { expect(isValidImageUpload(new Uint8Array([]))).toBe(false); const huge = new Uint8Array(MAX_UPLOAD_BYTES + 1); huge.set(jpeg); expect(isValidImageUpload(huge)).toBe(false); }); }); describe("extensionForType / typeForExtension", () => { it("แมปชนิด ↔ นามสกุลไป-กลับได้", () => { expect(extensionForType("image/jpeg")).toBe("jpg"); expect(typeForExtension("webp")).toBe("image/webp"); }); it("ชนิดที่ไม่รองรับ → null", () => { expect(extensionForType("image/gif")).toBeNull(); expect(extensionForType("text/html")).toBeNull(); }); }); describe("sanitizeSlug", () => { it("เหลือ a-z 0-9 และ - เท่านั้น", () => { expect(sanitizeSlug("Phahol Theatre!")).toBe("phahol-theatre"); expect(sanitizeSlug("../../etc/passwd")).toBe("etc-passwd"); }); it("ว่าง → ใช้ค่า fallback", () => { expect(sanitizeSlug("!!!")).toBe("cinema"); }); }); describe("makeUploadName", () => { it("มี slug + random + timestamp + นามสกุลที่ถูกต้อง", () => { const name = makeUploadName( "house-cinema-samyan", "image/webp", 1700000000000, "abc123", ); expect(name).toBe("house-cinema-samyan-abc123-1700000000000.webp"); }); it("ชื่อไม่ชนกันเมื่อเรียกซ้ำในมิลลิวินาทีเดียวกัน", () => { const a = makeUploadName("x", "image/jpeg", 1, "aaa111"); const b = makeUploadName("x", "image/jpeg", 1, "bbb222"); expect(a).not.toBe(b); }); it("ชนิดที่ไม่รองรับ → โยน error", () => { expect(() => makeUploadName("x", "image/gif")).toThrow(); }); }); describe("safeUploadPath", () => { it("รับพาธปกติ", () => { expect(safeUploadPath("cinemas/abc-1.webp")).toBe("cinemas/abc-1.webp"); }); it("ปฏิเสธ path traversal และ absolute path", () => { expect(safeUploadPath("../secrets.json")).toBeNull(); expect(safeUploadPath("cinemas/../../x")).toBeNull(); expect(safeUploadPath("/etc/passwd")).toBeNull(); expect(safeUploadPath("")).toBeNull(); }); });