Fix watchlist add and thumbnail API validation

This commit is contained in:
Dymas
2026-05-16 11:19:46 +02:00
parent d5f81d15b7
commit 8a2184f6f3
6 changed files with 78 additions and 9 deletions
+31
View File
@@ -1116,6 +1116,37 @@ class HandlerRouteTests(unittest.TestCase):
self.assertEqual(handler.error_status, HTTPStatus.BAD_REQUEST)
self.assertIn("data_url", handler.error_message)
def test_watchlist_add_duplicate_returns_ok_instead_of_created(self):
body = b'{"show_id":"show-1","title":"Show 1","category":"watching"}'
handler = DummyHandler("/api/watchlist/add", body=body, content_length=len(body))
result = {"message": "Already tracking Show 1.", "item": {"show_id": "show-1", "title": "Show 1"}, "created": False}
with mock.patch.object(APP.WATCHLIST, "add", return_value=result):
APP.Handler.do_POST(handler)
self.assertEqual(handler.json_payload, result)
self.assertEqual(handler.json_status, HTTPStatus.OK)
def test_ensure_thumbnails_rejects_non_array_show_ids(self):
body = b'{"show_ids":"show-1"}'
handler = DummyHandler("/api/watchlist/ensure-thumbnails", body=body, content_length=len(body))
APP.Handler.do_POST(handler)
self.assertEqual(handler.error_status, HTTPStatus.BAD_REQUEST)
self.assertIn("show_ids", handler.error_message)
def test_ensure_thumbnails_parses_string_false_force(self):
body = b'{"show_ids":["show-1"],"force":"false"}'
handler = DummyHandler("/api/watchlist/ensure-thumbnails", body=body, content_length=len(body))
with mock.patch.object(APP.WATCHLIST, "ensure_thumbnails", return_value={"items": []}) as ensure_thumbnails:
APP.Handler.do_POST(handler)
ensure_thumbnails.assert_called_once_with(["show-1"], limit=6, force=False)
self.assertEqual(handler.json_status, HTTPStatus.OK)
def test_ensure_thumbnails_rejects_invalid_force_value(self):
body = b'{"show_ids":["show-1"],"force":"maybe"}'
handler = DummyHandler("/api/watchlist/ensure-thumbnails", body=body, content_length=len(body))
APP.Handler.do_POST(handler)
self.assertEqual(handler.error_status, HTTPStatus.BAD_REQUEST)
self.assertIn("force", handler.error_message)
def test_thumbnail_route_serves_cached_file_without_fetching(self):
thumb_path = APP.THUMBNAIL_ROOT / "show-1.jpg"
thumb_path.parent.mkdir(parents=True, exist_ok=True)