Skip to content

Commit

Permalink
Adjust error handling for Gigya JSONDecodeError (#789)
Browse files Browse the repository at this point in the history
* Adjust error handling for Gigya JSONDecodeError

* isort
  • Loading branch information
epenet authored Feb 9, 2023
1 parent c7f2370 commit c36f053
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/renault_api/gigya/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Gigya API."""
import logging
from json import JSONDecodeError
from typing import Any
from typing import cast
from typing import Dict
Expand All @@ -9,6 +10,7 @@

from . import models
from . import schemas
from .exceptions import GigyaException

GIGYA_JWT = "gigya_jwt"
GIGYA_LOGIN_TOKEN = "gigya_login_token" # noqa: S105
Expand All @@ -35,7 +37,10 @@ async def request(
# url,
# response_text,
# )
gigya_response: models.GigyaResponse = schema.loads(response_text)
try:
gigya_response: models.GigyaResponse = schema.loads(response_text)
except JSONDecodeError as err:
raise GigyaException("Gigya responded with invalid JSON") from err
# Check for Gigya error
gigya_response.raise_for_error_code()
# Check for HTTP error
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ def inject_gigya_login(mocked_responses: aioresponses) -> str:
)


def inject_gigya_login_invalid(mocked_responses: aioresponses) -> str:
"""Inject Gigya login data."""
return inject_gigya(
mocked_responses,
"accounts.login",
"login_invalid.txt",
)


def inject_gigya_account_info(mocked_responses: aioresponses) -> str:
"""Inject Gigya getAccountInfo data."""
return inject_gigya(
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions tests/gigya/test_gigya.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ async def test_login(
assert response.get_session_cookie() == TEST_LOGIN_TOKEN


@pytest.mark.asyncio
async def test_login_error(
websession: aiohttp.ClientSession, mocked_responses: aioresponses
) -> None:
"""Test login response."""
fixtures.inject_gigya_login_invalid(mocked_responses)

with pytest.raises(gigya.exceptions.GigyaException):
await gigya.login(
websession,
TEST_GIGYA_URL,
TEST_GIGYA_APIKEY,
TEST_USERNAME,
TEST_PASSWORD,
)


@pytest.mark.asyncio
async def test_person_id(
websession: aiohttp.ClientSession, mocked_responses: aioresponses
Expand Down

0 comments on commit c36f053

Please sign in to comment.