myYouTube/tests/test_youtube_client.py
vrubelroman f97ef0d54d Harden YouTube sync: retries + pooled client, uploads-playlist fallback, JSON 404 for /api/*
Findings from a full audit of the live instance, each with tests:

1. Transient TLS failures to Google ("[SSL: UNEXPECTED_EOF_WHILE_READING]")
   were silently dropping 2-4% of channels from every videos sync and once
   failed a whole scheduled subscriptions sync. youtube_client now retries
   transport errors and 5xx (3 attempts, 0.5s/2s backoff; 4xx never), and
   sync_videos runs under youtube_session() so the ~240 calls of a run share
   one pooled client instead of a fresh TLS handshake each. Verified live:
   one EOF during the next sync was retried and the run lost no channels.

2. channels.list omits some channels entirely (three region-restricted TV
   channels here), so they never got an uploads_playlist_id and never had a
   videos sync. The id is derivable ("UC..." -> "UU..."); sync_subscriptions
   falls back to it, also when channels.list fails. Verified live: all three
   now have 200 videos each.

3. SPAStaticFiles served index.html with HTTP 200 for any unknown /api/*
   path (a mistyped endpoint "succeeded" with HTML). /api/* now stays a JSON
   404; client-side routes still fall back to the SPA.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-20 20:20:27 +00:00

505 lines
17 KiB
Python

import pytest
from app.services import youtube_client
class FakeResponse:
def __init__(self, status_code, payload):
self.status_code = status_code
self._payload = payload
self.text = str(payload)
def json(self):
return self._payload
class FakeClient:
def __init__(self, responses):
self._responses = list(responses)
def __enter__(self):
return self
def __exit__(self, *args):
return False
def get(self, url, params=None, headers=None):
return self._responses.pop(0)
def _credentials():
class Cred:
token = "fake-token"
return Cred()
def test_fetch_subscriptions_paginates(monkeypatch):
page1 = FakeResponse(
200,
{
"items": [
{
"id": "subA",
"snippet": {
"title": "Channel A",
"description": "desc a",
"resourceId": {"channelId": "chanA"},
"thumbnails": {"high": {"url": "http://thumb/a.jpg"}},
},
}
],
"nextPageToken": "page2",
},
)
page2 = FakeResponse(
200,
{
"items": [
{
"snippet": {
"title": "Channel B",
"description": "desc b",
"resourceId": {"channelId": "chanB"},
"thumbnails": {"default": {"url": "http://thumb/b.jpg"}},
}
}
]
},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([page1, page2]))
result = youtube_client.fetch_subscriptions(_credentials())
assert [r["youtube_channel_id"] for r in result] == ["chanA", "chanB"]
assert result[0]["youtube_subscription_id"] == "subA"
assert result[0]["thumbnail_url"] == "http://thumb/a.jpg"
assert result[1]["thumbnail_url"] == "http://thumb/b.jpg"
def test_fetch_subscriptions_raises_on_quota_exceeded(monkeypatch):
response = FakeResponse(
403,
{"error": {"message": "Quota exceeded", "errors": [{"reason": "quotaExceeded"}]}},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
with pytest.raises(youtube_client.YouTubeQuotaExceeded):
youtube_client.fetch_subscriptions(_credentials())
def test_fetch_playlist_video_ids(monkeypatch):
response = FakeResponse(
200,
{
"items": [
{"contentDetails": {"videoId": "vid1"}},
{"contentDetails": {"videoId": "vid2"}},
]
},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10)
assert result == ["vid1", "vid2"]
def test_fetch_playlist_video_ids_returns_empty_on_404(monkeypatch):
response = FakeResponse(404, {"error": {"message": "playlist not found"}})
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10)
assert result == []
def test_fetch_playlist_video_ids_paginates(monkeypatch):
page1 = FakeResponse(
200,
{
"items": [{"contentDetails": {"videoId": f"vid{i}"}} for i in range(50)],
"nextPageToken": "page2",
},
)
page2 = FakeResponse(
200,
{"items": [{"contentDetails": {"videoId": f"vid{i}"}} for i in range(50, 80)]},
)
seen = []
class RecordingClient(FakeClient):
def get(self, url, params=None, headers=None):
seen.append({"url": url, "params": params})
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: RecordingClient([page1, page2]))
result = youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 80)
assert result == [f"vid{i}" for i in range(80)]
assert len(seen) == 2
assert seen[0]["params"]["maxResults"] == 50
assert "pageToken" not in seen[0]["params"]
assert seen[1]["params"]["maxResults"] == 30
assert seen[1]["params"]["pageToken"] == "page2"
assert seen[0]["params"]["part"] == "contentDetails"
assert seen[1]["params"]["part"] == "contentDetails"
def test_fetch_playlist_video_ids_stops_at_safety_cap(monkeypatch):
page = FakeResponse(
200,
{"items": [{"contentDetails": {"videoId": "vid"}}], "nextPageToken": "next"},
)
seen = []
class RecordingClient(FakeClient):
def get(self, url, params=None, headers=None):
seen.append(params)
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: RecordingClient([page] * 100))
result = youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10000)
assert len(result) == youtube_client.MAX_PLAYLIST_PAGES
assert len(seen) == youtube_client.MAX_PLAYLIST_PAGES
assert "pageToken" not in seen[0]
assert seen[1]["pageToken"] == "next"
def _page_items(video_ids, next_token=None):
payload = {"items": [{"contentDetails": {"videoId": vid}} for vid in video_ids]}
if next_token:
payload["nextPageToken"] = next_token
return FakeResponse(200, payload)
def test_fetch_playlist_video_ids_incremental_returns_only_unknown(monkeypatch):
page = _page_items(["new1", "known1", "new2", "known2"])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([page]))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids={"known1", "known2"}, max_results=10, stop_threshold=50
)
assert result == ["new1", "new2"]
def test_fetch_playlist_video_ids_incremental_stops_on_known_run(monkeypatch):
page1 = _page_items([f"known{i}" for i in range(50)], next_token="page2")
page2 = _page_items(["new1", "new2"], next_token="page3")
seen = []
class RecordingClient(FakeClient):
def get(self, url, params=None, headers=None):
seen.append(params)
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: RecordingClient([page1, page2]))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids={f"known{i}" for i in range(50)}, max_results=10, stop_threshold=50
)
assert result == []
assert len(seen) == 1 # page2 never requested
def test_fetch_playlist_video_ids_incremental_known_run_crosses_pages(monkeypatch):
page1 = _page_items(["new1"] + [f"known{i}" for i in range(49)], next_token="page2")
page2 = _page_items([f"known{i}" for i in range(49, 60)], next_token="page3")
seen = []
class RecordingClient(FakeClient):
def get(self, url, params=None, headers=None):
seen.append(params)
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: RecordingClient([page1, page2]))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids={f"known{i}" for i in range(60)}, max_results=10, stop_threshold=50
)
assert result == ["new1"]
assert len(seen) == 2 # stopped mid-page-2, page3 never requested
def test_fetch_playlist_video_ids_incremental_stops_at_cap(monkeypatch):
pages = [_page_items([f"vid{page * 50 + i}" for i in range(50)], next_token=f"p{page + 2}") for page in range(6)]
seen = []
class RecordingClient(FakeClient):
def get(self, url, params=None, headers=None):
seen.append(params)
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: RecordingClient(pages))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids=set(), max_results=200, stop_threshold=50
)
assert len(result) == 200
assert len(seen) == 4
def test_fetch_playlist_video_ids_incremental_empty_on_404(monkeypatch):
response = FakeResponse(404, {"error": {"message": "playlist not found"}})
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids=set(), max_results=10, stop_threshold=50
)
assert result == []
def test_fetch_playlist_video_ids_incremental_dedupes_new_ids(monkeypatch):
page = _page_items(["new1", "new1", "known1"])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([page]))
result = youtube_client.fetch_playlist_video_ids_incremental(
_credentials(), "UUplaylist", known_ids={"known1"}, max_results=10, stop_threshold=50
)
assert result == ["new1"]
def test_fetch_videos_details(monkeypatch):
response = FakeResponse(
200,
{
"items": [
{
"id": "vid1",
"snippet": {
"channelId": "chanA",
"title": "Title 1",
"description": "desc",
"publishedAt": "2026-09-10T12:00:00Z",
"thumbnails": {"high": {"url": "http://thumb/1.jpg"}},
},
"contentDetails": {"duration": "PT10M5S"},
}
]
},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_videos_details(_credentials(), ["vid1"])
assert len(result) == 1
assert result[0]["youtube_video_id"] == "vid1"
assert result[0]["youtube_channel_id"] == "chanA"
assert result[0]["duration_iso8601"] == "PT10M5S"
assert result[0]["thumbnail_url"] == "http://thumb/1.jpg"
def test_unsubscribe_success(monkeypatch):
response = FakeResponse(204, {})
seen = {}
class FakeDeleteClient(FakeClient):
def delete(self, url, params=None, headers=None):
seen["params"] = params
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeDeleteClient([response]))
youtube_client.unsubscribe(_credentials(), "sub123")
assert seen["params"] == {"id": "sub123"}
def test_unsubscribe_already_gone_is_not_an_error(monkeypatch):
response = FakeResponse(404, {"error": {"message": "not found"}})
class FakeDeleteClient(FakeClient):
def delete(self, url, params=None, headers=None):
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeDeleteClient([response]))
youtube_client.unsubscribe(_credentials(), "sub123") # must not raise
def test_unsubscribe_insufficient_scope(monkeypatch):
response = FakeResponse(
403,
{"error": {"message": "Request had insufficient authentication scopes.", "errors": [{"reason": "forbidden"}]}},
)
class FakeDeleteClient(FakeClient):
def delete(self, url, params=None, headers=None):
return self._responses.pop(0)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeDeleteClient([response]))
with pytest.raises(youtube_client.YouTubeInsufficientScope):
youtube_client.unsubscribe(_credentials(), "sub123")
def test_fetch_uploads_playlists_batches(monkeypatch):
response = FakeResponse(
200,
{
"items": [
{
"id": "chanA",
"contentDetails": {"relatedPlaylists": {"uploads": "UUchanA"}},
"statistics": {"subscriberCount": "12345"},
},
{
"id": "chanB",
"contentDetails": {"relatedPlaylists": {"uploads": "UUchanB"}},
"statistics": {"hiddenSubscriberCount": True},
},
{"id": "chanC", "contentDetails": {"relatedPlaylists": {"uploads": "UUchanC"}}},
{"id": "chanD", "contentDetails": {"relatedPlaylists": {}}, "statistics": {"subscriberCount": "42"}},
]
},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_uploads_playlists(_credentials(), ["chanA", "chanB", "chanC", "chanD"])
assert result == {
"chanA": {"uploads_playlist_id": "UUchanA", "subscriber_count": 12345},
"chanB": {"uploads_playlist_id": "UUchanB", "subscriber_count": None},
"chanC": {"uploads_playlist_id": "UUchanC", "subscriber_count": None},
"chanD": {"uploads_playlist_id": None, "subscriber_count": 42},
}
def test_fetch_uploads_playlists_subscriber_count_not_an_int_is_none(monkeypatch):
response = FakeResponse(
200,
{
"items": [
{
"id": "chanA",
"contentDetails": {"relatedPlaylists": {"uploads": "UUchanA"}},
"statistics": {"subscriberCount": "not-a-number"},
}
]
},
)
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([response]))
result = youtube_client.fetch_uploads_playlists(_credentials(), ["chanA"])
assert result == {"chanA": {"uploads_playlist_id": "UUchanA", "subscriber_count": None}}
# --- retry / shared-client behaviour -----------------------------------------
class FlakyClient(FakeClient):
"""Raises the given transport errors first, then serves canned responses."""
def __init__(self, failures, responses):
super().__init__(responses)
self._failures = list(failures)
self.calls = 0
def get(self, url, params=None, headers=None):
self.calls += 1
if self._failures:
raise self._failures.pop(0)
return self._responses.pop(0)
@pytest.fixture
def no_sleep(monkeypatch):
slept = []
monkeypatch.setattr(youtube_client.time, "sleep", lambda s: slept.append(s))
return slept
def test_retries_transport_error_then_succeeds(monkeypatch, no_sleep):
ok = FakeResponse(200, {"items": [{"contentDetails": {"videoId": "vid1"}}]})
client = FlakyClient([youtube_client.httpx.ConnectError("[SSL: UNEXPECTED_EOF_WHILE_READING]")], [ok])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: client)
result = youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10)
assert result == ["vid1"]
assert client.calls == 2
assert no_sleep == [youtube_client.RETRY_BACKOFF_SECONDS[0]]
def test_retries_5xx_then_succeeds(monkeypatch, no_sleep):
client = FakeClient([FakeResponse(503, {"error": {"message": "backend"}}), FakeResponse(200, {"items": []})])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: client)
assert youtube_client.fetch_videos_details(_credentials(), ["vid1"]) == []
assert len(no_sleep) == 1
def test_gives_up_after_max_attempts(monkeypatch, no_sleep):
errors = [youtube_client.httpx.ConnectError("eof")] * youtube_client.MAX_ATTEMPTS
client = FlakyClient(errors, [])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: client)
with pytest.raises(youtube_client.httpx.ConnectError):
youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10)
assert client.calls == youtube_client.MAX_ATTEMPTS
assert len(no_sleep) == youtube_client.MAX_ATTEMPTS - 1
def test_4xx_is_not_retried(monkeypatch, no_sleep):
quota = FakeResponse(403, {"error": {"message": "Quota exceeded", "errors": [{"reason": "quotaExceeded"}]}})
client = FlakyClient([], [quota])
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: client)
with pytest.raises(youtube_client.YouTubeQuotaExceeded):
youtube_client.fetch_subscriptions(_credentials())
assert client.calls == 1
assert no_sleep == []
def test_youtube_session_shares_one_client_across_calls(monkeypatch):
constructed = []
def make_client(timeout):
client = FakeClient([FakeResponse(200, {"items": []})] * 10)
constructed.append(client)
return client
monkeypatch.setattr(youtube_client.httpx, "Client", make_client)
with youtube_client.youtube_session():
for _ in range(3):
youtube_client.fetch_playlist_video_ids(_credentials(), "UUplaylist", 10)
youtube_client.fetch_videos_details(_credentials(), ["vid1"])
assert len(constructed) == 1
# Outside a session every call still gets its own client, as before.
youtube_client.fetch_videos_details(_credentials(), ["vid1"])
assert len(constructed) == 2
def test_youtube_session_is_cleared_after_block(monkeypatch):
monkeypatch.setattr(youtube_client.httpx, "Client", lambda timeout: FakeClient([]))
with youtube_client.youtube_session():
assert youtube_client._thread_state.client is not None
assert youtube_client._thread_state.client is None
@pytest.mark.parametrize(
"channel_id,expected",
[
("UCRds47MZ1Ng7KCLseg2TkWA", "UURds47MZ1Ng7KCLseg2TkWA"),
("UC", None),
("HC123", None),
("", None),
],
)
def test_derive_uploads_playlist_id(channel_id, expected):
assert youtube_client.derive_uploads_playlist_id(channel_id) == expected