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
|
"""Asynchronous Python client for Peblar EV chargers."""
from __future__ import annotations
import pytest
from aiohttp import ClientResponse, ClientSession
from aresponses import Response, ResponsesMockServer
from peblar import Peblar
from peblar.exceptions import (
PeblarAuthenticationError,
PeblarError,
)
async def test_identify(aresponses: ResponsesMockServer) -> None:
"""Test the identify method."""
async def response_handler(request: ClientResponse) -> Response:
"""Response handler for this test."""
assert not await request.text()
return aresponses.Response(status=200)
aresponses.add(
"example.com",
"/api/v1/system/identify",
"PUT",
response_handler,
)
async with Peblar(host="example.com") as peblar:
await peblar.identify()
async def test_request_with_shared_session(aresponses: ResponsesMockServer) -> None:
"""Test a passed in shared session works as expected."""
aresponses.add(
"example.com",
"/api/v1/system/identify",
"PUT",
aresponses.Response(status=200),
)
async with ClientSession() as session:
peblar = Peblar(host="example.com", session=session)
await peblar.identify()
await peblar.close()
async def test_http_error400(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 404 response handling."""
aresponses.add(
"example.com",
"/api/v1/system/identify",
"PUT",
aresponses.Response(text="OMG PUPPIES!", status=400),
)
async with Peblar(host="example.com") as peblar:
with pytest.raises(PeblarError):
await peblar.identify()
async def test_unauthenticated_response(aresponses: ResponsesMockServer) -> None:
"""Test authentication failure."""
aresponses.add(
"example.com",
"/api/v1/system/identify",
"PUT",
aresponses.Response(status=401),
)
async with Peblar(host="example.com") as peblar:
with pytest.raises(PeblarAuthenticationError):
await peblar.identify()
|