fix: handle malformed Gitea collections (#99)
This commit is contained in:
parent
7d5d1181c0
commit
f4e2969fec
|
|
@ -81,6 +81,11 @@ async def context() -> JSONResponse:
|
||||||
raise ValueError("Gitea current-user response was not an object")
|
raise ValueError("Gitea current-user response was not an object")
|
||||||
if not all(field in user_data for field in ("id", "login")):
|
if not all(field in user_data for field in ("id", "login")):
|
||||||
raise ValueError("Gitea current-user response did not include id and login")
|
raise ValueError("Gitea current-user response did not include id and login")
|
||||||
|
if any(
|
||||||
|
data is not None and not isinstance(data, list)
|
||||||
|
for data in (repo_data, issues_data, prs_data)
|
||||||
|
):
|
||||||
|
raise ValueError("Gitea collection response was not a list")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_message = (
|
error_message = (
|
||||||
f"Gitea context request timed out after {CONTEXT_TIMEOUT_SECONDS:g}s"
|
f"Gitea context request timed out after {CONTEXT_TIMEOUT_SECONDS:g}s"
|
||||||
|
|
|
||||||
|
|
@ -203,6 +203,29 @@ async def test_context_normalizes_null_collection_payloads(monkeypatch):
|
||||||
assert context["pull_requests"] == []
|
assert context["pull_requests"] == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_context_returns_fallback_for_malformed_collection_payloads(monkeypatch):
|
||||||
|
async def user():
|
||||||
|
return {"id": 1, "login": "timmy"}
|
||||||
|
|
||||||
|
async def malformed_collection():
|
||||||
|
return {"message": "unexpected upstream shape"}
|
||||||
|
|
||||||
|
monkeypatch.setattr(main, "current_user", user)
|
||||||
|
monkeypatch.setattr(main, "repos", malformed_collection)
|
||||||
|
monkeypatch.setattr(main, "issues", malformed_collection)
|
||||||
|
monkeypatch.setattr(main, "pull_requests", malformed_collection)
|
||||||
|
|
||||||
|
response = await main.context()
|
||||||
|
context = json.loads(response.body)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert context["repos"] == []
|
||||||
|
assert context["issues"] == []
|
||||||
|
assert context["pull_requests"] == []
|
||||||
|
assert context["error"] == "Gitea collection response was not a list"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_context_normalizes_nullable_user_profile_fields(monkeypatch):
|
async def test_context_normalizes_nullable_user_profile_fields(monkeypatch):
|
||||||
async def user():
|
async def user():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user