From fd4e7b395f64c78b94e32e63736438cf986bafff Mon Sep 17 00:00:00 2001 From: Nyan404 Date: Fri, 21 Aug 2026 14:48:04 +0300 Subject: [PATCH] fix(upload): identify image host requests --- MEMORY.md | 2 +- app/belki/image_host.py | 6 +++++- structure_and_data.md | 2 +- tests/test_image_host.py | 12 +++++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/MEMORY.md b/MEMORY.md index 856c4ad..6d51c48 100644 --- a/MEMORY.md +++ b/MEMORY.md @@ -28,7 +28,7 @@ - 2026-08-21: project-local frontend instructions include `frontend-craft`, `frontend-design`, and `ui-ux-pro-max`; use them together for UI work. - 2026-08-21: `/belki` is the single public Belki workspace: both reports and its “Заявки” tab are open without a password. The old `/applications` route remains only as a direct legacy entry point. `/admin` is the only password-protected surface. - 2026-08-21: `/admin` now keeps a server-memory `HttpOnly` cookie session across page refreshes instead of retaining an admin password in the browser. Its authenticated WebSocket sends a refresh signal every 25 seconds; this needs the cwshed Nginx `/api/` Upgrade proxy settings. -- 2026-08-21: `img.nyan404.ru` is only the image-host viewer/frontend; Belki uploads must use the image-host API `https://nyan.nyan404.ru`. Upload failures are converted to actionable API errors rather than a generic calculator 500. +- 2026-08-21: `img.nyan404.ru` is only the image-host viewer/frontend; Belki uploads must use the image-host API `https://nyan.nyan404.ru`. The API rejects the default Python `urllib` user agent, so `ImageHostClient` identifies requests as `BelkiCalculator/1.0`. Upload failures are converted to actionable API errors rather than a generic calculator 500. - 2026-08-21: Belki Python base image `reg.nyan404.ru/cwshed-clone/belki-base:py312-d6c42c4497ce` pins `pip==26.2.1`; CI upgrades to the same version before installing requirements. # 2026-08-16 — несколько котят в прокачке diff --git a/app/belki/image_host.py b/app/belki/image_host.py index 9cd5ac8..dc0d58f 100644 --- a/app/belki/image_host.py +++ b/app/belki/image_host.py @@ -97,7 +97,11 @@ class ImageHostClient: f"{self.api_url}{path}", data=json.dumps(payload).encode("utf-8"), method="POST", - headers={"Content-Type": "application/json", "Accept": "application/json"}, + headers={ + "Content-Type": "application/json", + "Accept": "application/json", + "User-Agent": "BelkiCalculator/1.0 (+https://cat.nyan404.ru/belki)", + }, ) try: with urlopen(request, timeout=self.timeout) as response: diff --git a/structure_and_data.md b/structure_and_data.md index a73bdec..ed3ceff 100644 --- a/structure_and_data.md +++ b/structure_and_data.md @@ -54,7 +54,7 @@ - 2026-08-16: corrected coaching payout direction for non-self-sufficient kittens. The escort receives the 1-per-5-minutes base rate or 1.5x for non-self-sufficient ЛУ; kitten points are not reported. Added a 30-minute regression case. - 2026-08-16: adopted `2026-08-16-krylany-calculator-rules.md`: Krylan is the Belka/escort role, kittens always receive 1 point per 5 minutes, non-self-sufficient ЛУ multiplies escort points by 1.5, and ZU forces self-sufficient calculation. - 2026-08-17: aligned `web/calculator.html` status controls with the VK bot and shared domain: LU allows at most one non-self-sufficient kitten, while ZU forces all kittens to self-sufficient; extra-kitten status controls are synchronized and validated before submission. -- 2026-08-17: web-selected screenshots are sent through `/api/web/upload` to `BELKI_IMAGE_HOST_API_URL` (production API `https://nyan.nyan404.ru`, viewer `https://img.nyan404.ru`) and persisted as report attachments. +- 2026-08-17: web-selected screenshots are sent through `/api/web/upload` to `BELKI_IMAGE_HOST_API_URL` (production API `https://nyan.nyan404.ru`, viewer `https://img.nyan404.ru`) and persisted as report attachments. The image-host API requires an explicit Belki `User-Agent`; the default Python `urllib` header is rejected with HTTP 403. - 2026-08-21: added structured community applications for feathers, kitten, flying-squirrel, and curator roles across VK, `/applications`, and the top-level `/admin` "Заявки" tab; acceptance is a status transition, not a generated forum post. - 2026-08-21: added a disabled durable Google Sheets export outbox for application-created and application-accepted events; no external request is attempted until an approved Apps Script URL and `BELKI_GOOGLE_EXPORT_ENABLED=1` are configured. # Обновление 2026-08-16: несколько котят diff --git a/tests/test_image_host.py b/tests/test_image_host.py index 8f03545..faae29a 100644 --- a/tests/test_image_host.py +++ b/tests/test_image_host.py @@ -1,10 +1,20 @@ import unittest -from unittest.mock import patch +from unittest.mock import MagicMock, patch from app.belki.image_host import ImageHostClient, ImageHostError class ImageHostClientTests(unittest.TestCase): + def test_initiate_identifies_the_belki_client(self) -> None: + client = ImageHostClient(api_url="https://api.example.test") + response = MagicMock() + response.__enter__.return_value = response + with patch("app.belki.image_host.urlopen", return_value=response) as urlopen: + with patch("app.belki.image_host.json.load", return_value={"files": []}): + client._request_json("/api/uploads/initiate", {"files": []}) + request = urlopen.call_args.args[0] + self.assertEqual(request.get_header("User-agent"), "BelkiCalculator/1.0 (+https://cat.nyan404.ru/belki)") + def test_upload_wraps_presigned_put_failure(self) -> None: client = ImageHostClient(api_url="https://api.example.test") with patch.object(client, "_request_json", return_value={"files": [{"uploadUrl": "https://storage.example.test/upload"}]}):