diff --git a/podme_api/client.py b/podme_api/client.py index d50fef3..a190805 100644 --- a/podme_api/client.py +++ b/podme_api/client.py @@ -202,7 +202,7 @@ async def _request( # noqa: C901 raise PodMeApiConnectionTimeoutError( "Timeout occurred while connecting to the PodMe API" ) from exception - except (ClientError, socket.gaierror) as exception: # pragma: no cover + except (ClientError, socket.gaierror) as exception: raise PodMeApiConnectionError( "Error occurred while communicating with the PodMe API" ) from exception diff --git a/tests/conftest.py b/tests/conftest.py index 582e94c..995b8cb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -95,13 +95,6 @@ def expired_credentials(): return SchibstedCredentials.from_dict(data) -@pytest.fixture -def invalid_credentials(): - data = load_fixture_json("default_credentials") - data["access_token"] = data["access_token"] + "_refreshed" - return SchibstedCredentials.from_dict(data) - - @pytest.fixture def refreshed_credentials(default_credentials): data = load_fixture_json("default_credentials") diff --git a/tests/test_client.py b/tests/test_client.py index 6867af5..b41c182 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,8 +6,9 @@ from datetime import time import logging from pathlib import Path +import socket import tempfile -from unittest.mock import AsyncMock, Mock, call +from unittest.mock import AsyncMock, Mock, call, patch import aiohttp from aiohttp.web_response import Response, json_response @@ -111,6 +112,24 @@ async def test_credentials_storage( result = await client.get_username() assert result == "testuser@example.com" + # Test loading and saving credentials with specified filename + async with podme_client( + load_default_user_credentials=False, + conf_dir=tempdir, + ) as client: + client: PodMeClient + # Loading + creds_file = Path(tempdir) / "credentials.json" + stored_credentials = SchibstedCredentials.from_json(creds_file.read_text(encoding="utf-8")) + await client.load_credentials(creds_file) + assert client.auth_client.get_credentials() == stored_credentials.to_dict() + + # Saving + creds_file_alt = creds_file.with_name("credentials2") + await client.save_credentials(creds_file_alt) + stored_credentials = SchibstedCredentials.from_json(creds_file_alt.read_text(encoding="utf-8")) + assert client.auth_client.get_credentials() == stored_credentials.to_dict() + async def test_get_user_subscription(aresponses: ResponsesMockServer, podme_client): fixture = load_fixture_json("subscription") @@ -897,6 +916,17 @@ async def test_json_error(aresponses: ResponsesMockServer, podme_client): assert await client._request("user") +async def test_network_error(podme_client): + """Test network error handling.""" + async with podme_client() as client: + client: PodMeClient + client.session = AsyncMock(spec=aiohttp.ClientSession) + with patch.object(client.session, "request", side_effect=socket.gaierror), pytest.raises( + PodMeApiConnectionError + ): + assert await client._request("user") + + async def test_unexpected_error(aresponses: ResponsesMockServer, podme_client): """Test unexpected error handling.""" aresponses.add(