Rebasely Beta: data may be purged

examples/storefront Public

TypeScript · 61 lines · 2.7 KiB · at 39ab077

import assert from "node:assert/strict";import { test } from "node:test";import { setup } from "./helpers/app.ts";const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3, 4]);async function upload(s: ReturnType<typeof setup>, id: string, bytes: Uint8Array, type = "image/png") {  return s.call("PUT", `/admin/products/${id}/image`, bytes, { ...s.admin, "content-type": type });}test("an uploaded image is stored under a content hash and linked from the product", async () => {  const s = setup();  await s.addProduct("wool-socks");  const res = await upload(s, "wool-socks", PNG);  assert.equal(res.status, 200);  assert.match(res.body.product.image_url, /^\/images\/products\/wool-socks\/[0-9a-f]{16}\.png$/);  const img = await s.call("GET", res.body.product.image_url);  assert.equal(img.status, 200);  assert.equal(img.headers.get("content-type"), "image/png");  assert.equal(img.headers.get("cache-control"), "public, max-age=31536000, immutable");});test("a new image gets a new URL", async () => {  const s = setup();  await s.addProduct("wool-socks");  const first = (await upload(s, "wool-socks", PNG)).body.product.image_url;  const second = (await upload(s, "wool-socks", new Uint8Array([...PNG, 9]))).body.product.image_url;  assert.notEqual(first, second);  assert.equal((await s.call("GET", first)).status, 200, "the old URL keeps working");});test("answers 304 when the browser already has the image", async () => {  const s = setup();  await s.addProduct("wool-socks");  const url = (await upload(s, "wool-socks", PNG)).body.product.image_url;  const etag = (await s.call("GET", url)).headers.get("etag")!;  assert.equal((await s.call("GET", url, undefined, { "if-none-match": etag })).status, 304);});test("refuses types that are not images and images over 5 MiB", async () => {  const s = setup();  await s.addProduct("wool-socks");  assert.equal((await upload(s, "wool-socks", PNG, "text/html")).status, 415);  assert.equal((await upload(s, "wool-socks", new Uint8Array(5 * 1024 * 1024 + 1))).status, 413);  assert.equal((await upload(s, "wool-socks", new Uint8Array(0))).status, 400);});test("uploading needs the admin token and an existing product", async () => {  const s = setup();  assert.equal((await upload(s, "nope", PNG)).body.error, "product_not_found");  await s.addProduct("wool-socks");  const res = await s.call("PUT", "/admin/products/wool-socks/image", PNG, { "content-type": "image/png" });  assert.equal(res.status, 401);});test("only product images are served", async () => {  const s = setup();  assert.equal((await s.call("GET", "/images/products/none.png")).status, 404);  assert.equal((await s.call("GET", "/images/private/report.pdf")).status, 404);});