Skip to content

Commit

Permalink
fix: update tests and added raise ValueError for 401 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
MountainGod2 committed Apr 11, 2024
1 parent c8ed3be commit 6956095
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 3 additions & 1 deletion examples/simple_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@


async def main() -> None: # noqa: D103
async with ChaturbateClient(username, token, 20) as client:
async with ChaturbateClient(
username, token, 20, "https://events.testbed.cb.dev/events/{username}/{token}/"
) as client:
response = await client.fetch_events()
for event in response.events:
logging.info(event.dict())
Expand Down
9 changes: 8 additions & 1 deletion src/chaturbate_poller/chaturbate_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ async def fetch_events(self, url: str | None = None) -> EventsAPIResponse:
if url is None:
url = self._construct_url()
response = await self.client.get(url, timeout=self.timeout)
response.raise_for_status()
try:
response.raise_for_status()
except httpx.HTTPStatusError as e:
if e.response.status_code == HttpStatusCode.UNAUTHORIZED:
error_message = "Unauthorized access. Verify the username and token."
logger.exception(error_message)
raise ValueError(error_message) from e
raise
return EventsAPIResponse.model_validate(response.json())

def _construct_url(self) -> str:
Expand Down
35 changes: 30 additions & 5 deletions tests/test_chaturbate_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Response,
TimeoutException,
)
from pydantic import ValidationError
from pytest_mock import MockerFixture

USERNAME = "testuser"
Expand Down Expand Up @@ -571,13 +572,21 @@ async def test_fetch_events_no_url(
http_client_mock.assert_called_once_with(TEST_URL, timeout=None)

@pytest.mark.asyncio()
async def test_fetch_events_malformed_json(
self, chaturbate_client: ChaturbateClient
async def test_fetch_events_undefined_json(
self,
chaturbate_client: ChaturbateClient,
http_client_mock, # noqa: ANN001
) -> None:
"""Test fetching events with malformed JSON."""
"""Test fetching events with undefined JSON."""
request = Request("GET", TEST_URL)
self.return_value = Response(200, content=b"{not: 'json'}", request=request)
with pytest.raises(HTTPStatusError):
# Create a Response object with undefined JSON content
response_content = b'{"not": "json", "nextUrl": "https://example.com"}'
http_client_mock.return_value = Response(
200, content=response_content, request=request
)
with pytest.raises(
ValidationError, match="1 validation error for EventsAPIResponse"
):
await chaturbate_client.fetch_events(TEST_URL)

@pytest.mark.asyncio()
Expand Down Expand Up @@ -607,3 +616,19 @@ async def test_fetch_events_http_statuses(
else:
with pytest.raises(HTTPStatusError):
await chaturbate_client.fetch_events(TEST_URL)

@pytest.mark.asyncio()
async def test_unauthorized_access(
self,
http_client_mock, # noqa: ANN001
chaturbate_client: ChaturbateClient,
) -> None:
"""Test unauthorized access."""
request = Request("GET", TEST_URL)
http_client_mock.return_value = Response(
HttpStatusCode.UNAUTHORIZED, request=request
)
with pytest.raises(
ValueError, match="Unauthorized access. Verify the username"
):
await chaturbate_client.fetch_events(TEST_URL)

0 comments on commit 6956095

Please sign in to comment.