"""``integrations.youtube_data`` (PLAN #6): mocked HTTP, never live OAuth. Every call goes through an ``httpx.Client`` built on a ``MockTransport``, so no network is touched. The unconfigured path must be a hard no-op: not one request. """ import json import httpx import pytest from clipwright.integrations import youtube_data def _configure(monkeypatch): monkeypatch.setenv(youtube_data.CLIENT_ID_ENV, "cid") monkeypatch.setenv(youtube_data.CLIENT_SECRET_ENV, "secret") monkeypatch.setenv(youtube_data.REFRESH_TOKEN_ENV, "refresh") monkeypatch.setenv(youtube_data.TOKEN_URI_ENV, "https://oauth.test/token") def _unconfigure(monkeypatch): for name in (youtube_data.CLIENT_ID_ENV, youtube_data.CLIENT_SECRET_ENV, youtube_data.REFRESH_TOKEN_ENV): monkeypatch.delenv(name, raising=False) class _Recorder: """Records requests and answers the token + comment calls.""" def __init__(self, token_status=200, comment_status=200, comment_json=None): self.requests = [] self.token_status = token_status self.comment_status = comment_status self.comment_json = comment_json or { "id": "comment-1", "snippet": {"videoId": "short-1"}, } def handler(self, request): self.requests.append(request) if request.url.path.endswith("/token"): return httpx.Response(self.token_status, json={"access_token": "tok"}) return httpx.Response(self.comment_status, json=self.comment_json) def client(self): return httpx.Client(transport=httpx.MockTransport(self.handler)) def test_is_configured_tracks_env(monkeypatch): _unconfigure(monkeypatch) assert youtube_data.is_configured() is False _configure(monkeypatch) assert youtube_data.is_configured() is True def test_unconfigured_is_a_noop_and_makes_no_request(monkeypatch): _unconfigure(monkeypatch) recorder = _Recorder() with pytest.raises(youtube_data.YoutubeDataError) as exc: youtube_data.post_first_comment("short-1", "hello", client=recorder.client()) assert exc.value.status_code == 400 assert "not configured" in exc.value.detail assert recorder.requests == [] def test_posts_first_comment_with_refresh_token(monkeypatch): _configure(monkeypatch) recorder = _Recorder() result = youtube_data.post_first_comment( "short-1", "watch the full video", client=recorder.client()) assert result["id"] == "comment-1" assert [r.method for r in recorder.requests] == ["POST", "POST"] token_req, comment_req = recorder.requests assert token_req.url == "https://oauth.test/token" assert b"grant_type=refresh_token" in token_req.content assert b"refresh_token=refresh" in token_req.content assert comment_req.url.path.endswith("/commentThreads") assert comment_req.url.params["part"] == "snippet" assert comment_req.headers["authorization"] == "Bearer tok" body = json.loads(comment_req.content) assert body["snippet"]["videoId"] == "short-1" assert (body["snippet"]["topLevelComment"]["snippet"]["textOriginal"] == "watch the full video") def test_token_failure_is_reported_without_posting(monkeypatch): _configure(monkeypatch) recorder = _Recorder(token_status=401) with pytest.raises(youtube_data.YoutubeDataError) as exc: youtube_data.post_first_comment("short-1", "hi", client=recorder.client()) assert exc.value.status_code == 502 assert "token refresh failed" in exc.value.detail assert len(recorder.requests) == 1 # never reached commentThreads def test_comment_error_carries_status_and_truncated_body(monkeypatch): _configure(monkeypatch) recorder = _Recorder(comment_status=403) with pytest.raises(youtube_data.YoutubeDataError) as exc: youtube_data.post_first_comment("short-1", "hi", client=recorder.client()) assert exc.value.status_code == 403 assert "commentThreads.insert failed" in exc.value.detail def test_server_error_maps_to_502(monkeypatch): _configure(monkeypatch) recorder = _Recorder(comment_status=500) with pytest.raises(youtube_data.YoutubeDataError) as exc: youtube_data.post_first_comment("short-1", "hi", client=recorder.client()) assert exc.value.status_code == 502 def test_missing_inputs_are_refused(monkeypatch): _configure(monkeypatch) recorder = _Recorder() client = recorder.client() with pytest.raises(youtube_data.YoutubeDataError): youtube_data.post_first_comment("", "hi", client=client) with pytest.raises(youtube_data.YoutubeDataError): youtube_data.post_first_comment("short-1", " ", client=client) assert recorder.requests == [] def test_scope_is_force_ssl(): # commentThreads.insert requires force-ssl; documented so a refactor cannot # silently drop it. assert youtube_data.SCOPE == "https://www.googleapis.com/auth/youtube.force-ssl"