mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Problem
Overnight alert (2026-09-15): `Exception in ASGI application` +
`request_audit_error` on `POST /api/admin/jobs/{id}/reset-worker-state`.
Root cause:
```
File "backend/api/routes/admin.py", line 688, in reset_worker_state
logger.info(f"Admin {auth_data[0]} reset worker state for job {job_id}")
TypeError: 'AuthResult' object is not subscriptable
```
`require_admin` returns an `AuthResult` object, but three endpoints
logged `auth_data[0]` (tuple-style) → 500 on **every** call. (The reset
endpoint's partial run also caused a downstream screens *"prerequisites
not met"* because it cleared progress state before the log line blew
up.)
Affected: `reset_worker_state` (:688), `clear_audio_search_cache`
(:638), `clear_all_flacfetch_cache` (:721). Every other admin endpoint
already uses `auth_data.user_email` — these three were outliers.
## Fix
- `auth_data[0]` → `auth_data.user_email` in the 3 log lines.
- **Regression tests** (these endpoints had *no* coverage, which is why
it shipped): `reset-worker-state` and `DELETE /cache` now assert **200,
not 500**. The require_admin test mock already returns an `AuthResult`,
so these tests fail on the old code.
The stale `auth_data: Tuple[str, UserType, int]` type hints elsewhere
are harmless (nothing else subscripts `auth_data`) and left untouched to
keep the diff minimal — noted in
`docs/archive/2026-09-15-e2e-flakiness-error-triage-handoff.md` for a
later cleanup.
## Not related to the E2E orchestration fixes
This is item 2 from the overnight triage — an admin endpoint bug
triggered by a manual/admin reset of a failed job, unrelated to the
prep→screens / review-submit fixes (#1002/#1003).
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Bug Fixes**
- Fixed administrator cache-clearing and worker-state reset actions that
could fail with a server error.
- These admin operations now correctly record the authenticated
administrator and complete successfully.
- Improved reliability for full cache clearing, audio-cache clearing,
and worker-state reset requests.
- **Tests**
- Added regression coverage for administrator authentication and audit
logging across affected endpoints.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
"""Regression: admin endpoints that log the caller must not 500.
|
|
|
|
`require_admin` returns an `AuthResult` object (not a tuple). Three admin endpoints
|
|
logged `auth_data[0]`, which raised `TypeError: 'AuthResult' object is not
|
|
subscriptable` and returned 500 (prod: POST /jobs/{id}/reset-worker-state on
|
|
2026-09-15). These endpoints had no test coverage, so the bug shipped. This test
|
|
locks in the fix (`auth_data.user_email`).
|
|
"""
|
|
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from backend.api.routes.admin import router
|
|
from backend.api.dependencies import require_admin, AuthResult, UserType
|
|
|
|
app = FastAPI()
|
|
app.include_router(router, prefix="/api")
|
|
|
|
|
|
def _mock_admin():
|
|
return AuthResult(
|
|
is_valid=True,
|
|
user_type=UserType.ADMIN,
|
|
remaining_uses=999,
|
|
message="Admin authenticated",
|
|
user_email="admin@example.com",
|
|
is_admin=True,
|
|
)
|
|
|
|
|
|
app.dependency_overrides[require_admin] = _mock_admin
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return TestClient(app)
|
|
|
|
|
|
def test_reset_worker_state_does_not_500(client):
|
|
"""POST /jobs/{id}/reset-worker-state must succeed — previously raised
|
|
TypeError on `auth_data[0]` because require_admin returns an AuthResult."""
|
|
mock_jm = Mock()
|
|
mock_jm.get_job.return_value = Mock() # truthy job
|
|
|
|
with patch("backend.services.job_manager.JobManager", return_value=mock_jm):
|
|
resp = client.post("/api/admin/jobs/test-123/reset-worker-state")
|
|
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["status"] == "success"
|
|
# video/render/screens progress states reset
|
|
assert mock_jm.update_state_data.call_count == 3
|
|
|
|
|
|
def test_clear_all_flacfetch_cache_does_not_500(client):
|
|
"""DELETE /cache must succeed — same `auth_data[0]` bug on the admin log line."""
|
|
mock_client = Mock()
|
|
|
|
async def _clear():
|
|
return 7
|
|
|
|
mock_client.clear_all_cache = _clear
|
|
|
|
with patch("backend.api.routes.admin.get_flacfetch_client", return_value=mock_client):
|
|
resp = client.delete("/api/admin/cache")
|
|
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["deleted_count"] == 7
|