1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
"""Tests for Kamereon models."""
import pytest
from marshmallow.schema import Schema
from tests import fixtures
from renault_api.kamereon import exceptions
from renault_api.kamereon import models
from renault_api.kamereon import schemas
RESPONSE_SCHEMAS = [
schemas.KamereonResponseSchema,
schemas.KamereonPersonResponseSchema,
schemas.KamereonVehiclesResponseSchema,
schemas.KamereonVehicleContractsResponseSchema,
schemas.KamereonVehicleDetailsResponseSchema,
schemas.KamereonVehicleDataResponseSchema,
]
@pytest.mark.parametrize(
"filename", fixtures.get_json_files(f"{fixtures.KAMEREON_FIXTURE_PATH}/error")
)
def test_vehicle_error_response(filename: str) -> None:
"""Test vehicle error response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
filename, schemas.KamereonVehicleDataResponseSchema
)
with pytest.raises(exceptions.KamereonResponseException):
response.raise_for_error_code()
assert response.errors is not None
def test_vehicle_error_quota_limit() -> None:
"""Test vehicle quota_limit response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/quota_limit.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.QuotaLimitException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.func.wired.overloaded"
assert excinfo.value.error_details == "You have reached your quota limit"
def test_vehicle_error_invalid_date() -> None:
"""Test vehicle invalid_date response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/invalid_date.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.InvalidInputException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.func.400"
assert (
excinfo.value.error_details
== "/data/attributes/startDateTime must be a future date"
)
def test_vehicle_error_invalid_upstream() -> None:
"""Test vehicle invalid_upstream response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/invalid_upstream.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.InvalidUpstreamException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.tech.500"
assert (
excinfo.value.error_details
== "Invalid response from the upstream server (The request sent to the GDC"
" is erroneous) ; 502 Bad Gateway"
)
def test_vehicle_error_not_supported() -> None:
"""Test vehicle not_supported response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/not_supported.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.NotSupportedException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.tech.501"
assert (
excinfo.value.error_details
== "This feature is not technically supported by this gateway"
)
def test_vehicle_error_resource_not_found() -> None:
"""Test vehicle resource_not_found response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/resource_not_found.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.ResourceNotFoundException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.func.wired.notFound"
assert excinfo.value.error_details == "Resource not found"
def test_vehicle_error_access_denied() -> None:
"""Test vehicle access_denied response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/access_denied.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.AccessDeniedException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.func.403"
assert excinfo.value.error_details == "Access is denied for this resource"
def test_vehicle_error_failed_foward() -> None:
"""Test vehicle access_denied response."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/failed_forward.json",
schemas.KamereonVehicleDataResponseSchema,
)
with pytest.raises(exceptions.FailedForwardException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.tech.wired.kamereon-proxy"
assert excinfo.value.error_details == "Failed to forward request to remote service."
@pytest.mark.parametrize("target_schema", RESPONSE_SCHEMAS)
def test_error_on_schema(target_schema: Schema) -> None:
"""Test vehicle access_denied response."""
response: models.KamereonResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/error/access_denied.json",
target_schema,
)
with pytest.raises(exceptions.AccessDeniedException) as excinfo:
response.raise_for_error_code()
assert excinfo.value.error_code == "err.func.403"
assert excinfo.value.error_details == "Access is denied for this resource"
|