From 42b659e807359b14445e473c536d1842364c4aea Mon Sep 17 00:00:00 2001 From: timmy Date: Thu, 6 Aug 2026 09:17:16 +0000 Subject: [PATCH] fix: fallback on incomplete current user (#91) --- src/main.py | 2 ++ tests/test_context_timeout.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main.py b/src/main.py index bbd83a6..d38465c 100644 --- a/src/main.py +++ b/src/main.py @@ -79,6 +79,8 @@ async def context() -> JSONResponse: ) if not isinstance(user_data, dict): raise ValueError("Gitea current-user response was not an object") + if not all(field in user_data for field in ("id", "login")): + raise ValueError("Gitea current-user response did not include id and login") 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 6c95292..fdb0358 100644 --- a/tests/test_context_timeout.py +++ b/tests/test_context_timeout.py @@ -250,6 +250,27 @@ async def test_context_returns_fallback_for_null_current_user_payload(monkeypatc assert context["error"] == "Gitea current-user response was not an object" +@pytest.mark.anyio +async def test_context_returns_fallback_for_incomplete_current_user_payload(monkeypatch): + async def incomplete_user(): + return {"id": 1} + + async def empty_collection(): + return [] + + monkeypatch.setattr(main, "current_user", incomplete_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 did not include id and login" + + @pytest.mark.anyio async def test_context_skips_malformed_repository_entries(monkeypatch): async def user(): -- 2.43.0