Upload returns that sentinel when the desk already had the file (no real image id ever existed for it); passing it to DELETE /api/images/<id> gets a 400, not a 404, so the prior already-gone handling missed it.
50 lines
2.9 KiB
TypeScript
50 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { TransportError } from "../src/delivery/transports.js";
|
|
import { ZEROZERO_URL, ZerozeroClient } from "../src/delivery/zerozero.js";
|
|
|
|
const LOGIN_SITE = "https://www.zerozero.pt";
|
|
|
|
/** Enough of fotos.zerozero.pt's login dance to reach `DELETE /api/images/<id>`, answered with `removeStatus`. */
|
|
function fakeZerozero(removeStatus: number): typeof fetch {
|
|
return (async (input: string | URL, init: RequestInit = {}) => {
|
|
const url = new URL(String(input));
|
|
const method = init.method ?? "GET";
|
|
const cookie = new Headers(init.headers).get("cookie") ?? "";
|
|
if (url.origin === ZEROZERO_URL && url.pathname === "/api/auth/login" && method === "GET") return Response.json({ authURL: `${LOGIN_SITE}/authorize` });
|
|
if (url.origin === LOGIN_SITE && url.pathname === "/authorize" && method === "GET") {
|
|
if (/session=ok/.test(cookie)) return new Response(null, { status: 302, headers: { location: `${ZEROZERO_URL}/callback` } });
|
|
return new Response("", { status: 200 });
|
|
}
|
|
if (url.origin === LOGIN_SITE && url.pathname === "/ax_login.php" && method === "POST") {
|
|
return new Response(JSON.stringify({ status: true }), { status: 200, headers: { "set-cookie": "session=ok; Domain=zerozero.pt" } });
|
|
}
|
|
if (url.origin === ZEROZERO_URL && url.pathname === "/callback" && method === "GET") return new Response("", { status: 200 });
|
|
if (url.origin === ZEROZERO_URL && url.pathname === "/api/auth/me" && method === "GET") return Response.json({ role: "photographer", photographer_name: "Foo" });
|
|
if (url.origin === ZEROZERO_URL && /^\/api\/images\//.test(url.pathname) && method === "DELETE") return new Response("", { status: removeStatus });
|
|
throw new Error(`unexpected request: ${method} ${url}`);
|
|
}) as unknown as typeof fetch;
|
|
}
|
|
|
|
describe("ZerozeroClient.remove", () => {
|
|
it("treats an already-gone image (403 or 404) as removed, like every other transport", async () => {
|
|
for (const status of [403, 404]) {
|
|
const client = new ZerozeroClient({ username: "foto", password: "segredo" }, fakeZerozero(status));
|
|
await expect(client.remove("956673")).resolves.toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it("still throws (and flags as transient) on an unrelated failure", async () => {
|
|
const client = new ZerozeroClient({ username: "foto", password: "segredo" }, fakeZerozero(500));
|
|
const err = await client.remove("956673").catch((e: unknown) => e);
|
|
expect(err).toBeInstanceOf(TransportError);
|
|
expect((err as TransportError).transient).toBe(true);
|
|
});
|
|
|
|
it("skips the call for a 'synced-<file name>' placeholder id: there was never a real image to take back", async () => {
|
|
const client = new ZerozeroClient({ username: "foto", password: "segredo" }, (() => {
|
|
throw new Error("should not call fetch");
|
|
}) as unknown as typeof fetch);
|
|
await expect(client.remove("synced-PSR11532.jpg")).resolves.toBeUndefined();
|
|
});
|
|
});
|