Skip to content

Commit

Permalink
Stubs for Gigya client (#32)
Browse files Browse the repository at this point in the history
* Stubs for Gigya

* Fix mypy

* Remove exceptions amends

* Reset stubs
  • Loading branch information
epenet authored Nov 18, 2020
1 parent ba1ead3 commit 47f7ef2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/renault_api/gigya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Gigya client for authentication."""
import logging
from typing import Dict

from aiohttp import ClientSession

from .const import CONF_GIGYA_APIKEY
from .const import CONF_GIGYA_URL
from renault_api.model import gigya as model
from renault_api.model.gigya import GigyaGetAccountInfoData
from renault_api.model.gigya import GigyaLoginSessionInfo

_LOGGER = logging.getLogger(__name__)


class Gigya(object):
"""Gigya client for authentication."""

def __init__(
self, websession: ClientSession, locale_details: Dict[str, str]
) -> None:
"""Initialise Gigya."""
self._websession = websession

self._api_key = locale_details[CONF_GIGYA_APIKEY]
self._root_url = locale_details[CONF_GIGYA_URL]

async def login(self, login_id: str, password: str) -> model.GigyaLoginResponse:
"""POST to /accounts.login."""
return model.GigyaLoginResponse(
0,
None,
GigyaLoginSessionInfo("cookieValue"),
)

async def get_account_info(
self, login_token: str
) -> model.GigyaGetAccountInfoResponse:
"""POST to /accounts.getAccountInfo."""
return model.GigyaGetAccountInfoResponse(
0,
None,
GigyaGetAccountInfoData("person-id"),
)

async def get_jwt(self, login_token: str) -> model.GigyaGetJWTResponse:
"""POST to /accounts.getAccountInfo."""
return model.GigyaGetJWTResponse(
0,
None,
"id_token",
)
17 changes: 17 additions & 0 deletions src/renault_api/model/gigya.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import marshmallow_dataclass

from . import BaseSchema
from renault_api.exceptions import GigyaException
from renault_api.exceptions import GigyaResponseException


Expand Down Expand Up @@ -34,6 +35,14 @@ class GigyaLoginResponse(GigyaResponse):

sessionInfo: Optional[GigyaLoginSessionInfo] # noqa: N815

def get_session_cookie(self) -> str:
"""Return cookie value from session information."""
if not self.sessionInfo: # pragma: no cover
raise GigyaException("`sessionInfo` is None in Login response.")
if not self.sessionInfo.cookieValue: # pragma: no cover
raise GigyaException("`sessionInfo.cookieValue` is None in Login response.")
return self.sessionInfo.cookieValue


@dataclass
class GigyaGetAccountInfoData:
Expand All @@ -48,6 +57,14 @@ class GigyaGetAccountInfoResponse(GigyaResponse):

data: Optional[GigyaGetAccountInfoData]

def get_person_id(self) -> str:
"""Return person id."""
if not self.data: # pragma: no cover
raise GigyaException("`data` is None in Login response.")
if not self.data.personId: # pragma: no cover
raise GigyaException("`data.personId` is None in Login response.")
return self.data.personId


@dataclass
class GigyaGetJWTResponse(GigyaResponse):
Expand Down
12 changes: 12 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Constants for the test suite fixtures."""
from renault_api.const import AVAILABLE_LOCALES
from renault_api.const import CONF_GIGYA_URL


TEST_LOCALE = "fr_FR"
TEST_PASSWORD = "test_password"
TEST_PERSON_ID = "person-id-1"
TEST_USERNAME = "test@example.com"

TEST_LOCALE_DETAILS = AVAILABLE_LOCALES[TEST_LOCALE]
TEST_GIGYA_URL = TEST_LOCALE_DETAILS[CONF_GIGYA_URL]
43 changes: 43 additions & 0 deletions tests/test_gigya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Test cases for the Renault client API keys."""
import pytest
from aiohttp.client import ClientSession
from tests.const import TEST_LOCALE_DETAILS
from tests.const import TEST_PASSWORD
from tests.const import TEST_USERNAME

from renault_api.gigya import Gigya


@pytest.fixture
def gigya(websession: ClientSession) -> Gigya:
"""Fixture for testing Gigya."""
return Gigya(websession=websession, locale_details=TEST_LOCALE_DETAILS)


@pytest.mark.asyncio
async def test_login(gigya: Gigya) -> None:
"""Test valid login response."""
login_response = await gigya.login(TEST_USERNAME, TEST_PASSWORD)
assert login_response.get_session_cookie()


@pytest.mark.asyncio
async def test_login_failed(gigya: Gigya) -> None:
"""Test failed login response."""
pass


@pytest.mark.asyncio
async def test_person_id(gigya: Gigya) -> None:
"""Test valid getAccountInfo response."""
login_token = "mock"
account_info_response = await gigya.get_account_info(login_token)
assert account_info_response.get_person_id()


@pytest.mark.asyncio
async def test_get_jwt_token(gigya: Gigya) -> None:
"""Test valid getJWT response."""
login_token = "mock"
jwt_response = await gigya.get_jwt(login_token)
assert jwt_response.id_token

0 comments on commit 47f7ef2

Please sign in to comment.