from __future__ import annotations import httpx from conduit import Client, MegaAPIHTTPError def test_health_is_open(): def handler(request: httpx.Request) -> httpx.Response: assert request.url.path == "/health" assert "authorization" not in request.headers return httpx.Response(200, text="ok") client = Client(transport=httpx.MockTransport(handler)) assert client.health() == "ok" def test_auth_header_and_services(): seen: list[str | None] = [] def handler(request: httpx.Request) -> httpx.Response: seen.append(request.headers.get("authorization")) return httpx.Response(200, json={"services": [{"id": "sms"}]}) client = Client(api_key="test-key", transport=httpx.MockTransport(handler)) assert client.services.list() == [{"id": "sms"}] assert seen == ["Bearer test-key"] def test_key_create_payload(): captured = {} def handler(request: httpx.Request) -> httpx.Response: captured["path"] = request.url.path captured["body"] = request.read() return httpx.Response( 200, json={ "id": "k1", "key": "mega_sk_live_public_secret", "key_prefix": "public", "display_name": "bot", "scopes": ["sms:read"], "expires_at": None, }, ) client = Client(api_key="test-key", transport=httpx.MockTransport(handler)) created = client.keys.create(display_name="bot", scopes=["sms:read"]) assert captured["path"] == "/keys" assert created["key_prefix"] == "public" def test_http_error_detail(): def handler(request: httpx.Request) -> httpx.Response: return httpx.Response(403, json={"detail": "missing required scope"}) client = Client(api_key="test-key", transport=httpx.MockTransport(handler)) try: client.auth.me() except MegaAPIHTTPError as exc: assert exc.status_code == 403 assert exc.detail == "missing required scope" else: raise AssertionError("expected MegaAPIHTTPError")