|
| 1 | +"""GET /api/printers?slug=... filtert auf einzelnen Printer.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +import pytest |
| 7 | +import pytest_asyncio |
| 8 | +from httpx import ASGITransport, AsyncClient |
| 9 | + |
| 10 | +from app.auth.dependencies import AuthContext |
| 11 | +from app.auth.scope_deps import require_admin, require_print, require_read |
| 12 | +from app.models.printer import Printer |
| 13 | +from app.repositories import printers as printers_repo |
| 14 | + |
| 15 | + |
| 16 | +@pytest_asyncio.fixture |
| 17 | +async def slug_client(): |
| 18 | + """AsyncClient mit gefakter Auth + korrekt gepatchter DB-Session. |
| 19 | +
|
| 20 | + Propagiert den _temp_db_engine-Patch (autouse, setzt eng_mod.async_session) |
| 21 | + in app.db.session (Name-Binding, wird NICHT automatisch aktualisiert). |
| 22 | + Analog zu tests/integration/api/conftest.py::api_client_with_seed. |
| 23 | + """ |
| 24 | + import app.db.engine as _eng |
| 25 | + import app.db.session as _sess |
| 26 | + from app.integrations import ( # type: ignore[attr-defined] |
| 27 | + IntegrationRegistry, |
| 28 | + _discover_plugins, |
| 29 | + ) |
| 30 | + from app.main import create_app |
| 31 | + |
| 32 | + # Propagate the monkeypatched engine into session.py's name binding |
| 33 | + _sess.async_session = _eng.async_session |
| 34 | + |
| 35 | + if not IntegrationRegistry.names(): |
| 36 | + _discover_plugins() |
| 37 | + |
| 38 | + fake = AuthContext(source="api-key", scope="admin", api_key_id=uuid4(), ip="127.0.0.1") |
| 39 | + app = create_app() |
| 40 | + inner = app._app |
| 41 | + for dep in (require_read, require_print, require_admin): |
| 42 | + inner.dependency_overrides[dep] = lambda _c=fake: _c |
| 43 | + |
| 44 | + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c: |
| 45 | + yield c |
| 46 | + |
| 47 | + |
| 48 | +@pytest_asyncio.fixture |
| 49 | +async def slug_db_session(): |
| 50 | + """DB-Session gegen die per-test temp-Engine (analog zu conftest.db_session).""" |
| 51 | + import app.db.engine as eng_mod |
| 52 | + |
| 53 | + async with eng_mod.async_session() as s: |
| 54 | + yield s |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def read_auth_headers() -> dict: |
| 59 | + """Leere Dict — Auth ist via dependency_overrides gefakt.""" |
| 60 | + return {} |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_filter_by_slug_returns_one(slug_client: AsyncClient, slug_db_session, read_auth_headers): |
| 65 | + await printers_repo.create(slug_db_session, |
| 66 | + Printer(name="Brother PT-P750W", slug="brother-p750w", |
| 67 | + model="PT-P750W", backend="ptouch")) |
| 68 | + await printers_repo.create(slug_db_session, |
| 69 | + Printer(name="Brother QL-820NWB", slug="brother-ql820nwb", |
| 70 | + model="QL-820NWB", backend="ptouch")) |
| 71 | + |
| 72 | + resp = await slug_client.get("/api/printers?slug=brother-p750w", headers=read_auth_headers) |
| 73 | + assert resp.status_code == 200 |
| 74 | + body = resp.json() |
| 75 | + assert len(body) == 1 |
| 76 | + assert body[0]["slug"] == "brother-p750w" |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_filter_by_slug_returns_404_when_missing(slug_client, read_auth_headers): |
| 81 | + resp = await slug_client.get("/api/printers?slug=unknown", headers=read_auth_headers) |
| 82 | + assert resp.status_code == 404 |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_no_filter_returns_all(slug_client: AsyncClient, slug_db_session, read_auth_headers): |
| 87 | + await printers_repo.create(slug_db_session, |
| 88 | + Printer(name="A", slug="a", model="X", backend="mock")) |
| 89 | + await printers_repo.create(slug_db_session, |
| 90 | + Printer(name="B", slug="b", model="X", backend="mock")) |
| 91 | + |
| 92 | + resp = await slug_client.get("/api/printers", headers=read_auth_headers) |
| 93 | + assert resp.status_code == 200 |
| 94 | + assert len(resp.json()) >= 2 |
0 commit comments