# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The approval queue on the page: listed, shown, approved or rejected.""" from __future__ import annotations import json from typing import TYPE_CHECKING import httpx from signal_bot.pack import MEDIA_DIR, TAGS_FILE from signal_bot.pack_custom import CUSTOM_FILE from signal_bot.pack_pending import PENDING_DIR, Pending, listing, queue from tests.manage_fixture import bot_is_down, client_for, deps_at from tests.pack_fixture import PIXEL if TYPE_CHECKING: from pathlib import Path from signal_bot.manage.deps import ManageDeps FIRST = Pending("first.png", ("p1",), ("wakacje",), "uuid-alice", 100, "group.X") SECOND = Pending("second.png", (), ("gory",), "uuid-bob", 50, "group.X") RELOADED = {"status": "reloaded", "commands": 25} def _bot_that_reloads(request: httpx.Request) -> httpx.Response: assert request.url.path == "/_reload" return httpx.Response(200, json=RELOADED) def _bot_that_keeps(request: httpx.Request) -> httpx.Response: assert request.url.path == "/_reload" return httpx.Response(409, json={"status": "kept", "reason": "aliases for nothing"}) def _queued(deps: ManageDeps) -> Path: root = deps.album.directory queue(root, PIXEL, FIRST) queue(root, PIXEL + b"2", SECOND) return root def test_the_listing_is_oldest_first_and_names_no_sender(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) _queued(deps) with client_for(deps) as client: answer = client.get("/api/album/pending").json() assert answer == { "pending": [ {"name": "second.png", "keys": [], "labels": ["gory"], "at": 50}, {"name": "first.png", "keys": ["p1"], "labels": ["wakacje"], "at": 100}, ] } assert "uuid" not in json.dumps(answer) def test_a_queued_file_is_served_by_its_listed_name_and_nothing_else(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) _queued(deps) with client_for(deps) as client: shown = client.get("/api/album/pending/first.png/media") missing = client.get("/api/album/pending/one.png/media") sidecar = client.get("/api/album/pending/first.png.json/media") assert shown.status_code == 200 assert shown.content == PIXEL # one.png is in the album, and the sidecar is in the queue directory; neither is queued. assert missing.status_code == 404 assert sidecar.status_code == 404 def test_approving_moves_it_into_the_album_and_reloads_the_bot(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) root = _queued(deps) with client_for(deps) as client: answer = client.post( "/api/album/pending/first.png/approve", json={"keys": ["p1"], "labels": ["wakacje"]} ) assert answer.status_code == 200 assert answer.json() == { "status": "approved", "keys": ["p1", "t1"], "reloaded": True, "reason": None, } assert (root / MEDIA_DIR / "first.png").read_bytes() == PIXEL assert json.loads((root / TAGS_FILE).read_text(encoding="utf-8"))["first.png"] == ["p1", "t1"] assert json.loads((root / CUSTOM_FILE).read_text(encoding="utf-8")) == {"t1": ["wakacje"]} assert [entry.name for entry in listing(root)] == ["second.png"] def test_a_reload_the_bot_refuses_is_reported_not_rolled_back(tmp_path: Path) -> None: """The photo is in the album; the bot's reason is about something else.""" deps = deps_at(tmp_path, _bot_that_keeps) root = _queued(deps) with client_for(deps) as client: answer = client.post( "/api/album/pending/first.png/approve", json={"keys": ["p1"], "labels": []} ) assert answer.status_code == 200 assert answer.json() == { "status": "approved", "keys": ["p1"], "reloaded": False, "reason": "aliases for nothing", } assert (root / MEDIA_DIR / "first.png").exists() def test_a_bot_that_is_down_is_reported_the_same_way(tmp_path: Path) -> None: deps = deps_at(tmp_path, bot_is_down) _queued(deps) with client_for(deps) as client: answer = client.post( "/api/album/pending/first.png/approve", json={"keys": [], "labels": []} ) assert answer.status_code == 200 assert answer.json()["reloaded"] is False assert "did not answer" in answer.json()["reason"] def test_approving_what_is_not_queued_is_404(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) with client_for(deps) as client: answer = client.post( "/api/album/pending/ghost.png/approve", json={"keys": [], "labels": []} ) assert answer.status_code == 404 assert "not waiting" in answer.json()["detail"] def test_a_label_the_sender_never_proposed_is_400(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) root = _queued(deps) with client_for(deps) as client: answer = client.post( "/api/album/pending/first.png/approve", json={"keys": [], "labels": ["zmyslone"]} ) assert answer.status_code == 400 assert "not proposed" in answer.json()["detail"] assert not (root / MEDIA_DIR / "first.png").exists() def test_an_album_that_would_not_load_is_400_and_the_photo_stays_queued( tmp_path: Path, ) -> None: deps = deps_at(tmp_path, _bot_that_reloads) root = _queued(deps) with client_for(deps) as client: answer = client.post( "/api/album/pending/first.png/approve", json={"keys": ["p9"], "labels": []} ) assert answer.status_code == 400 assert "names somebody" in answer.json()["detail"] assert not (root / MEDIA_DIR / "first.png").exists() assert "first.png" in {entry.name for entry in listing(root)} def test_rejecting_deletes_the_file_and_its_sidecar(tmp_path: Path) -> None: deps = deps_at(tmp_path, _bot_that_reloads) root = _queued(deps) with client_for(deps) as client: answer = client.post("/api/album/pending/first.png/reject", json={}) again = client.post("/api/album/pending/first.png/reject", json={}) assert answer.json() == {"status": "rejected"} assert again.status_code == 200 assert sorted(path.name for path in (root / PENDING_DIR).iterdir()) == [ "second.png", "second.png.json", ] assert not (root / MEDIA_DIR / "first.png").exists()