| 1 |
"""Unit tests for cookidoo-api.""" |
| 2 |
|
| 3 |
from collections.abc import AsyncGenerator, Generator |
| 4 |
|
| 5 |
from aiohttp import ClientSession, CookieJar |
| 6 |
from aioresponses import aioresponses |
| 7 |
from dotenv import load_dotenv |
| 8 |
import pytest |
| 9 |
|
| 10 |
from cookidoo_api.cookidoo import Cookidoo |
| 11 |
from cookidoo_api.types import CookidooConfig |
| 12 |
from cookidoo_api.well_known import ENDPOINT_RELS |
| 13 |
|
| 14 |
load_dotenv() |
| 15 |
|
| 16 |
UUID = "00000000-00000000-00000000-00000000" |
| 17 |
|
| 18 |
# Dummy OAuth2 client, so the tests do not depend on the shipped defaults. |
| 19 |
TEST_CLIENT_ID = "test-client-id" |
| 20 |
TEST_REDIRECT_URI = "test.cookidoo.api://code-grant" |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(autouse=True) |
| 24 |
def mock_endpoint_discovery(monkeypatch: pytest.MonkeyPatch) -> None: |
| 25 |
"""Short-circuit .well-known/home discovery with the hardcoded shapes. |
| 26 |
|
| 27 |
Endpoint discovery itself is covered by tests/test_well_known.py; tests |
| 28 |
in this module exercise everything downstream of a successful |
| 29 |
resolution, so they get the const.py templates verbatim without |
| 30 |
needing to mock every service's .well-known/home HTTP call. |
| 31 |
""" |
| 32 |
|
| 33 |
async def _resolve(*_args: object, **_kwargs: object) -> dict[str, str]: |
| 34 |
return {rel: template for rel, (_service, template) in ENDPOINT_RELS.items()} |
| 35 |
|
| 36 |
monkeypatch.setattr( |
| 37 |
"cookidoo_api.cookidoo.resolve_endpoint_paths", |
| 38 |
_resolve, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture(name="session") |
| 43 |
async def aiohttp_client_session() -> AsyncGenerator[ClientSession]: |
| 44 |
"""Create a client session.""" |
| 45 |
jar = CookieJar(unsafe=True) |
| 46 |
async with ClientSession(cookie_jar=jar) as session: |
| 47 |
yield session |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture(name="cookidoo") |
| 51 |
async def bring_api_client(session: ClientSession) -> Cookidoo: |
| 52 |
"""Create Cookidoo instance.""" |
| 53 |
cookidoo = Cookidoo( |
| 54 |
session, |
| 55 |
cfg=CookidooConfig( |
| 56 |
client_id=TEST_CLIENT_ID, |
| 57 |
redirect_uri=TEST_REDIRECT_URI, |
| 58 |
), |
| 59 |
) |
| 60 |
return cookidoo |
| 61 |
|
| 62 |
|
| 63 |
@pytest.fixture(name="mocked") |
| 64 |
def aioclient_mock() -> Generator[aioresponses]: |
| 65 |
"""Mock Aiohttp client requests.""" |
| 66 |
with aioresponses() as m: |
| 67 |
yield m |