diff --git a/src/main.py b/src/main.py index 5c9c51b..047d5f0 100644 --- a/src/main.py +++ b/src/main.py @@ -75,6 +75,8 @@ async def context() -> JSONResponse: asyncio.gather(current_user(), repos(), issues(), pull_requests()), timeout=CONTEXT_TIMEOUT_SECONDS, ) + if not isinstance(user_data, dict): + raise ValueError("Gitea current-user response was not an object") except Exception as e: error_message = ( f"Gitea context request timed out after {CONTEXT_TIMEOUT_SECONDS:g}s" diff --git a/tests/test_context_timeout.py b/tests/test_context_timeout.py index eed6c71..ea7bff2 100644 --- a/tests/test_context_timeout.py +++ b/tests/test_context_timeout.py @@ -167,3 +167,24 @@ async def test_context_normalizes_nullable_user_profile_fields(monkeypatch): assert response.status_code == 200 assert context["user"]["full_name"] == "" assert context["user"]["email"] == "" + + +@pytest.mark.anyio +async def test_context_returns_fallback_for_null_current_user_payload(monkeypatch): + async def null_user(): + return None + + async def empty_collection(): + return [] + + monkeypatch.setattr(main, "current_user", null_user) + monkeypatch.setattr(main, "repos", empty_collection) + monkeypatch.setattr(main, "issues", empty_collection) + monkeypatch.setattr(main, "pull_requests", empty_collection) + + response = await main.context() + context = json.loads(response.body) + + assert response.status_code == 200 + assert context["repos"] == [] + assert context["error"] == "Gitea current-user response was not an object"