Skip to content

Commit

Permalink
Adjust error handling for Gigya JSONDecodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Feb 8, 2023
1 parent c15eabc commit 56e5c8e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
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
25 changes: 17 additions & 8 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down 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.
24 changes: 21 additions & 3 deletions tests/gigya/test_gigya.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand All @@ -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
Expand Down

0 comments on commit 56e5c8e

Please sign in to comment.