diff --git a/app/services/workflow_service.py b/app/services/workflow_service.py index a460329..dabd8ad 100644 --- a/app/services/workflow_service.py +++ b/app/services/workflow_service.py @@ -401,14 +401,25 @@ async def list_instances( page: int = 1, page_size: int = 20, status_filter: str | None = None, + user_id: uuid.UUID | None = None, + is_system_admin: bool = False, ) -> dict[str, Any]: - """List workflow instances with optional status filter.""" + """List workflow instances with optional status filter. + + BUG-036 fix: the route passed ``user_id``/``is_system_admin`` which this + signature did not accept -> TypeError -> 500 on every call. + + Non-admin users see only instances they initiated; admins/system admins + see all instances of the tenant. + """ page = max(1, page) page_size = max(1, min(100, page_size)) base = select(WorkflowInstance).where(WorkflowInstance.tenant_id == tenant_id) if status_filter: base = base.where(WorkflowInstance.status == status_filter) + if not is_system_admin and user_id is not None: + base = base.where(WorkflowInstance.initiated_by == user_id) count_q = select(func.count()).select_from(base.subquery()) total_result = await db.execute(count_q) diff --git a/docs/test-bugs.md b/docs/test-bugs.md index a77aa1c..1d1b374 100644 --- a/docs/test-bugs.md +++ b/docs/test-bugs.md @@ -353,7 +353,7 @@ Jeder Bug wird wie folgt dokumentiert: - **Response:** `{"code":"internal_error","detail":"Internal server error","trace_id":"bbdf0698"}` - **Schweregrad:** High - **Ursache:** Unbekannt — muss Backend-Log prüfen -- **Status:** ⏳ Nicht gefixt +- **Status:** ✅ Gefixt 2026-08-24 (Block E/I-C): Root-Cause bewiesen — Route übergab user_id/is_system_admin an list_instances, Service-Signatur akzeptierte beide nicht → TypeError → 500 auf JEDEN Aufruf. Fix: Service-Signatur erweitert um optionale user_id/is_system_admin; Nicht-Admins sehen nur eigene Instanzen (initiated_by-Filter), Admins alle; agent_runner-Caller bleibt kompatibel. Beweistest test_bug036_instances.py 2/2 grün (200 plain + 200 mit status-filter); ruff clean. ### BUG-037: Compliance Incident POST gibt 500 Internal Server Error - **Kategorie:** API diff --git a/tests/test_bug036_instances.py b/tests/test_bug036_instances.py new file mode 100644 index 0000000..1ad0489 --- /dev/null +++ b/tests/test_bug036_instances.py @@ -0,0 +1,35 @@ +"""Tests for BUG-036 fix — GET /api/v1/workflows/instances returned 500. + +Root cause: the route passed ``user_id``/``is_system_admin`` which the service +signature did not accept -> TypeError -> 500 on every call. +""" + +from __future__ import annotations + +import pytest + +from tests.conftest import ORIGIN_HEADER, login_client, seed_tenant_and_users + + +@pytest.mark.asyncio +async def test_list_instances_returns_200(client, db_session): + """GET /api/v1/workflows/instances must return 200 (was 500 before fix).""" + await seed_tenant_and_users(db_session) + await login_client(client, "admin@tenanta.com") + + resp = await client.get("/api/v1/workflows/instances", headers=ORIGIN_HEADER) + assert resp.status_code == 200, resp.text + data = resp.json() + assert "items" in data and "total" in data + + +@pytest.mark.asyncio +async def test_list_instances_status_filter_returns_200(client, db_session): + """Status filter variant also works after the signature fix.""" + await seed_tenant_and_users(db_session) + await login_client(client, "admin@tenanta.com") + + resp = await client.get( + "/api/v1/workflows/instances?status=running", headers=ORIGIN_HEADER + ) + assert resp.status_code == 200, resp.text