diff --git a/src/renault_api/gigya/__init__.py b/src/renault_api/gigya/__init__.py index e08eb586..fb2db201 100644 --- a/src/renault_api/gigya/__init__.py +++ b/src/renault_api/gigya/__init__.py @@ -1,5 +1,6 @@ """Gigya API.""" import logging +from json import JSONDecodeError from typing import Any from typing import cast from typing import Dict @@ -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 @@ -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 diff --git a/tests/fixtures.py b/tests/fixtures.py index 0fa0b4d4..f32a931b 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -5,14 +5,6 @@ import json from glob import glob from os import path -from typing import Any -from typing import List -from typing import Mapping -from typing import Optional - -import jwt -from aioresponses import aioresponses -from marshmallow.schema import Schema from tests.const import REDACTED from tests.const import TEST_ACCOUNT_ID from tests.const import TEST_COUNTRY @@ -21,6 +13,14 @@ from tests.const import TEST_PERSON_ID from tests.const import TEST_VIN from tests.const import TO_REDACT +from typing import Any +from typing import List +from typing import Mapping +from typing import Optional + +import jwt +from aioresponses import aioresponses +from marshmallow.schema import Schema GIGYA_FIXTURE_PATH = "tests/fixtures/gigya" KAMEREON_FIXTURE_PATH = "tests/fixtures/kamereon" @@ -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( diff --git a/tests/fixtures/gigya/login_invalid.txt b/tests/fixtures/gigya/login_invalid.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/gigya/test_gigya.py b/tests/gigya/test_gigya.py index ba7725bb..0fc2f126 100644 --- a/tests/gigya/test_gigya.py +++ b/tests/gigya/test_gigya.py @@ -1,7 +1,4 @@ """Tests for Gigya API.""" -import aiohttp -import pytest -from aioresponses import aioresponses from tests import fixtures from tests.const import TEST_GIGYA_APIKEY from tests.const import TEST_GIGYA_URL @@ -10,6 +7,10 @@ from tests.const import TEST_PERSON_ID from tests.const import TEST_USERNAME +import aiohttp +import pytest +from aioresponses import aioresponses + from renault_api import gigya @@ -30,6 +31,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