Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add models for Gigya #34

Merged
merged 13 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
722 changes: 403 additions & 319 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Changelog = "https://github.com/hacf-fr/renault-api/releases"
python = "^3.7.1"
click = "^7.0"
aiohttp = "3.7.2"
marshmallow-dataclass = "^8.2.0"

[tool.poetry.dev-dependencies]
pytest = "^6.1.2"
Expand Down
17 changes: 17 additions & 0 deletions src/renault_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
"""Exceptions for Renault API."""
from typing import Optional


class RenaultException(Exception):
"""Base class for Renault API errors."""

pass


# Gigya exceptions
class GigyaException(RenaultException):
"""Base exception for Gigya errors."""

pass


class GigyaResponseException(GigyaException):
"""Gigya returned a parsable errors."""

def __init__(self, error_code: int, error_details: Optional[str]):
"""Initialise GigyaResponseException."""
self.error_code = error_code
self.error_details = error_details
11 changes: 11 additions & 0 deletions src/renault_api/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Models for Renault API."""
import marshmallow


class BaseSchema(marshmallow.Schema):
"""Base schema for Gigya models to exclude unknown fields."""

class Meta:
"""Force unknown fields to 'exclude'."""

unknown = marshmallow.EXCLUDE
67 changes: 67 additions & 0 deletions src/renault_api/model/gigya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Gigya models."""
from dataclasses import dataclass
from typing import Optional

import marshmallow_dataclass

from . import BaseSchema
from renault_api.exceptions import GigyaResponseException


@dataclass
class GigyaResponse:
"""Gigya response."""

errorCode: int # noqa: N815
errorDetails: Optional[str] # noqa: N815

def raise_for_error_code(self) -> None:
"""Checks the response information."""
if self.errorCode > 0:
raise GigyaResponseException(self.errorCode, self.errorDetails)


@dataclass
class GigyaLoginSessionInfo:
"""Gigya Login sessionInfo data."""

cookieValue: Optional[str] # noqa: N815


@dataclass
class GigyaLoginResponse(GigyaResponse):
"""Gigya response to POST on /accounts.login."""

sessionInfo: Optional[GigyaLoginSessionInfo] # noqa: N815


@dataclass
class GigyaGetAccountInfoData:
"""Gigya Login sessionInfo data."""

personId: Optional[str] # noqa: N815


@dataclass
class GigyaGetAccountInfoResponse(GigyaResponse):
"""Gigya response to POST on /accounts.getAccountInfo."""

data: Optional[GigyaGetAccountInfoData]


@dataclass
class GigyaGetJWTResponse(GigyaResponse):
"""Gigya response to POST on /accounts.getJWT."""

id_token: Optional[str]


GigyaLoginResponseSchema = marshmallow_dataclass.class_schema(
GigyaLoginResponse, base_schema=BaseSchema
)()
GigyaGetAccountInfoResponseSchema = marshmallow_dataclass.class_schema(
GigyaGetAccountInfoResponse, base_schema=BaseSchema
)()
GigyaGetJWTResponseSchema = marshmallow_dataclass.class_schema(
GigyaGetJWTResponse, base_schema=BaseSchema
)()
40 changes: 40 additions & 0 deletions tests/fixtures/gigya/account_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"callId": "callId",
"errorCode": 0,
"apiVersion": 2,
"statusCode": 200,
"statusReason": "OK",
"time": "2020-11-10T08:31:02.748Z",
"registeredTimestamp": 1556570377000,
"UID": "UID",
"UIDSignature": "UIDSignature=",
"signatureTimestamp": "1604997062",
"created": "2019-04-29T20:39:36.880Z",
"createdTimestamp": 1556570376000,
"data": {
"personId": "person-id-1",
"gigyaDataCenter": "gigyaDataCenter"
},
"preferences": {},
"emails": {
"verified": ["email@email.com"],
"unverified": []
},
"isActive": true,
"isRegistered": true,
"isVerified": true,
"lastLogin": "2020-11-10T08:31:02.595Z",
"lastLoginTimestamp": 1604997062000,
"lastUpdated": "2019-04-29T20:40:39.441Z",
"lastUpdatedTimestamp": 1556570439441,
"loginProvider": "site",
"oldestDataUpdated": "2019-04-29T20:39:36.880Z",
"oldestDataUpdatedTimestamp": 1556570376880,
"profile": {
"email": "email@email.com"
},
"registered": "2019-04-29T20:39:37.022Z",
"socialProviders": "site",
"verified": "2019-04-29T20:40:39.441Z",
"verifiedTimestamp": 1556570439441
}
1 change: 0 additions & 1 deletion tests/fixtures/gigya/account_info.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/gigya/account_info_corrupted.txt

This file was deleted.

10 changes: 10 additions & 0 deletions tests/fixtures/gigya/get_jwt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"callId": "callId",
"errorCode": 0,
"apiVersion": 2,
"statusCode": 200,
"statusReason": "OK",
"time": "2020-11-10T08:31:02.872Z",
"ignoredFields": "",
"id_token": "sample-jwt-token"
}
1 change: 0 additions & 1 deletion tests/fixtures/gigya/get_jwt.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/gigya/get_jwt_corrupted.txt

This file was deleted.

36 changes: 36 additions & 0 deletions tests/fixtures/gigya/login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"callId": "callId",
"errorCode": 0,
"apiVersion": 2,
"statusCode": 200,
"statusReason": "OK",
"time": "2020-11-10T08:31:02.637Z",
"registeredTimestamp": 1556570377,
"UID": "UID",
"UIDSignature": "UIDSignature=",
"signatureTimestamp": "1604997062",
"created": "2019-04-29T20:39:36.880Z",
"createdTimestamp": 1556570376,
"isActive": true,
"isRegistered": true,
"isVerified": true,
"lastLogin": "2020-11-10T08:31:02.595Z",
"lastLoginTimestamp": 1604997062,
"lastUpdated": "2019-04-29T20:40:39.441Z",
"lastUpdatedTimestamp": 1556570439441,
"loginProvider": "site",
"oldestDataUpdated": "2019-04-29T20:39:36.880Z",
"oldestDataUpdatedTimestamp": 1556570376880,
"profile": {
"email": "email@email.com"
},
"registered": "2019-04-29T20:39:37.022Z",
"socialProviders": "site",
"verified": "2019-04-29T20:40:39.441Z",
"verifiedTimestamp": 1556570439441,
"newUser": false,
"sessionInfo": {
"cookieName": "cookieName",
"cookieValue": "sample-cookie-value"
}
}
1 change: 0 additions & 1 deletion tests/fixtures/gigya/login.txt

This file was deleted.

10 changes: 10 additions & 0 deletions tests/fixtures/gigya/login_failed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"callId": "callId",
"errorCode": 403042,
"errorDetails": "invalid loginID or password",
"errorMessage": "Invalid LoginID",
"apiVersion": 2,
"statusCode": 403,
"statusReason": "Forbidden",
"time": "2020-11-17T08:22:36.561Z"
}
1 change: 0 additions & 1 deletion tests/fixtures/gigya/login_failed.txt

This file was deleted.

58 changes: 58 additions & 0 deletions tests/model/test_gigya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Tests for RenaultClient."""
from typing import Any
from typing import Type

import pytest
from marshmallow.schema import Schema

from renault_api.exceptions import GigyaResponseException
from renault_api.model.gigya import GigyaGetAccountInfoResponse
from renault_api.model.gigya import GigyaGetAccountInfoResponseSchema
from renault_api.model.gigya import GigyaGetJWTResponse
from renault_api.model.gigya import GigyaGetJWTResponseSchema
from renault_api.model.gigya import GigyaLoginResponse
from renault_api.model.gigya import GigyaLoginResponseSchema


def get_response_content(path: str, schema: Type[Schema]) -> Any:
"""Read fixture text file as string."""
with open(f"tests/fixtures/gigya/{path}", "r") as file:
content = file.read()
return schema.loads(content)


def test_login_response() -> None:
"""Test login response."""
response: GigyaLoginResponse = get_response_content(
"login.json", GigyaLoginResponseSchema
)
assert response.sessionInfo.cookieValue == "sample-cookie-value"


def test_login_failed_response() -> None:
"""Test login response."""
response: GigyaLoginResponse = get_response_content(
"login_failed.json", GigyaLoginResponseSchema
)
with pytest.raises(GigyaResponseException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == 403042
assert excinfo.value.error_details == "invalid loginID or password"


def test_get_account_info_response() -> None:
"""Test login response."""
response: GigyaGetAccountInfoResponse = get_response_content(
"account_info.json", GigyaGetAccountInfoResponseSchema
)
response.raise_for_error_code()
assert response.data.personId == "person-id-1"


def test_get_jwt_response() -> None:
"""Test login response."""
response: GigyaGetJWTResponse = get_response_content(
"get_jwt.json", GigyaGetJWTResponseSchema
)
response.raise_for_error_code()
assert response.id_token == "sample-jwt-token"