Cotton code
34 lines · 1.9 KB · TypeScriptRaw
/** * Public photos (ROUTES-AUDIT 15, 23 Nov 2026): a business's alone, bounded an hour and in all, each the picture its * type says; never on a personal database. Invented objects only. */import { SELF } from "cloudflare:test";import { describe, expect, it } from "vitest";import { ADMIN, personalObject } from "./personal.ts";const PNG = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAACAAAAAYCAIAAAAUMWhjAAAAJElEQVR4nGM4EaBBU8QwasGoBaMWjFowasGoBaMWjFowNCwAAOj5wC7/fbJHAAAAAElFTkSuQmCC"), (c) => c.charCodeAt(0));const put = (slug: string, body: Uint8Array, type: string, auth: Record<string, string> = ADMIN) =>  SELF.fetch(`https://example.test/b/${slug}/v1/photos`, { method: "POST", headers: { ...auth, "content-type": type }, body });describe("POST /v1/photos", () => {  it("is refused on a personal database: its files are its sealed media, never public", async () => {    const { owner } = await personalObject("photos-personal");    const res = await put("photos-personal", PNG, "image/png", { authorization: `Bearer ${owner}` });    expect(res.status).toBe(404);    expect(((await res.json()) as { code: string }).code).toBe("not_personal");  });  it("takes a business's picture only when its bytes are its type's, and at most 60 an hour", async () => {    const lying = await put("photos-studio", new TextEncoder().encode("<html>not a picture</html>"), "image/png");    expect(lying.status).toBe(400);    const first = await put("photos-studio", PNG, "image/png");    expect(first.status).toBe(201);    const { url } = (await first.json()) as { url: string };    expect((await SELF.fetch(`https://example.test/b/photos-studio${url}`)).status).toBe(200);    for (let i = 1; i < 60; i++) expect((await put("photos-studio", PNG, "image/png")).status).toBe(201);    const over = await put("photos-studio", PNG, "image/png");    expect(over.status).toBe(429);    expect(((await over.json()) as { code: string }).code).toBe("too_many_photos");  });});