Skip to content

Commit

Permalink
tests(client): Improve tests coverage + remove unused fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
bendikrb committed Oct 24, 2024
1 parent 7c510d2 commit efd14db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion podme_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
32 changes: 31 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,6 +112,24 @@ async def test_credentials_storage(
result = await client.get_username()
assert result == "[email protected]"

# 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")
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit efd14db

Please sign in to comment.