AP0.7: Tenant Hardening, Actor Directory und Data Layer Minimum
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 42s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 12s
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 42s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 12s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
36487d2869
commit
db06af0489
32 files changed
+1424
-43
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Kairo Data Layer — read-oriented aggregation for workspace and dashboards."""
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Action read-models for workspace — tenant-scoped, actor-assignment based."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from services.actions import OPEN_ACTION_STATUSES, _attach_assignments, _serialize_row
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def _require_actor(ctx: TenantContext) -> str:
|
||||
if not ctx.actor_id:
|
||||
raise ValueError("Kein Actor im TenantContext")
|
||||
return ctx.actor_id
|
||||
|
||||
|
||||
def _list_assigned_actions(
|
||||
*,
|
||||
tenant_id: str,
|
||||
actor_id: str,
|
||||
statuses: list[str],
|
||||
) -> list[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT a.id, a.tenant_id, a.initiative_id, a.title, a.description,
|
||||
a.status, a.priority, a.created_at, a.updated_at,
|
||||
i.title AS initiative_title
|
||||
FROM actions a
|
||||
JOIN action_assignments aa
|
||||
ON aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
JOIN initiatives i
|
||||
ON i.id = a.initiative_id AND i.tenant_id = a.tenant_id
|
||||
WHERE a.tenant_id = %s
|
||||
AND aa.actor_id = %s
|
||||
AND a.status = ANY(%s)
|
||||
ORDER BY
|
||||
CASE a.priority
|
||||
WHEN 'high' THEN 0
|
||||
WHEN 'normal' THEN 1
|
||||
WHEN 'low' THEN 2
|
||||
END,
|
||||
a.updated_at DESC
|
||||
""",
|
||||
(tenant_id, actor_id, statuses),
|
||||
)
|
||||
actions = [_serialize_row(dict(row)) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
return _attach_assignments(actions, tenant_id=tenant_id)
|
||||
|
||||
|
||||
def get_my_open_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Open + in_progress actions assigned to current actor (excludes blocked)."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=["open", "in_progress"],
|
||||
)
|
||||
|
||||
|
||||
def get_my_blocked_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Blocked actions assigned to current actor."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=["blocked"],
|
||||
)
|
||||
|
||||
|
||||
def get_all_my_open_actions(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""All non-terminal open actions (open, in_progress, blocked) — legacy compat."""
|
||||
actor_id = _require_actor(ctx)
|
||||
return _list_assigned_actions(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=actor_id,
|
||||
statuses=list(OPEN_ACTION_STATUSES),
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Actor workload read-models — tenant-scoped aggregation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_actor_workload(ctx: TenantContext) -> list[dict[str, Any]]:
|
||||
"""Open/blocked/in_progress action counts per active actor in tenant."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
a.id AS actor_id,
|
||||
a.name AS display_name,
|
||||
a.actor_type,
|
||||
COUNT(*) FILTER (WHERE act.status = 'open') AS open_actions,
|
||||
COUNT(*) FILTER (WHERE act.status = 'blocked') AS blocked_actions,
|
||||
COUNT(*) FILTER (WHERE act.status = 'in_progress') AS in_progress_actions
|
||||
FROM actors a
|
||||
LEFT JOIN action_assignments aa
|
||||
ON aa.actor_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
LEFT JOIN actions act
|
||||
ON act.id = aa.action_id
|
||||
AND act.tenant_id = aa.tenant_id
|
||||
AND act.status IN ('open', 'in_progress', 'blocked')
|
||||
WHERE a.tenant_id = %s AND a.is_active = TRUE
|
||||
GROUP BY a.id, a.name, a.actor_type
|
||||
ORDER BY
|
||||
CASE a.actor_type
|
||||
WHEN 'human' THEN 0
|
||||
WHEN 'working_group' THEN 1
|
||||
WHEN 'agent' THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
a.name
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
result = []
|
||||
for row in cur.fetchall():
|
||||
result.append(
|
||||
{
|
||||
"actor_id": str(row["actor_id"]),
|
||||
"display_name": row["display_name"],
|
||||
"actor_type": row["actor_type"],
|
||||
"open_actions": int(row["open_actions"] or 0),
|
||||
"blocked_actions": int(row["blocked_actions"] or 0),
|
||||
"in_progress_actions": int(row["in_progress_actions"] or 0),
|
||||
}
|
||||
)
|
||||
return result
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Initiative read-models for workspace."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from services.initiatives import _serialize_row
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_active_initiatives(
|
||||
ctx: TenantContext,
|
||||
*,
|
||||
limit: Optional[int] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Active and paused initiatives in the current tenant."""
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
sql = """
|
||||
SELECT id, tenant_id, title, goal, status, priority,
|
||||
owner_actor_id, created_at, updated_at
|
||||
FROM initiatives
|
||||
WHERE tenant_id = %s AND status IN ('active', 'paused')
|
||||
ORDER BY updated_at DESC, title
|
||||
"""
|
||||
params: list[Any] = [ctx.tenant_id]
|
||||
if limit is not None:
|
||||
sql += " LIMIT %s"
|
||||
params.append(limit)
|
||||
cur.execute(sql, params)
|
||||
return [_serialize_row(dict(row)) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Workspace summary read-model — tenant-scoped KPIs for current actor."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from db import get_connection
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def get_workspace_summary(ctx: TenantContext) -> dict[str, Any]:
|
||||
"""
|
||||
Summary for workspace dashboard.
|
||||
|
||||
Personal counts (current actor): open, blocked, recently done.
|
||||
Tenant-wide: active initiatives count.
|
||||
"""
|
||||
if not ctx.actor_id:
|
||||
return {
|
||||
"open_actions_count": 0,
|
||||
"blocked_actions_count": 0,
|
||||
"active_initiatives_count": 0,
|
||||
"done_actions_recent_count": 0,
|
||||
}
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status IN ('open', 'in_progress')
|
||||
) AS open_actions_count,
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status = 'blocked'
|
||||
) AS blocked_actions_count,
|
||||
COUNT(*) FILTER (
|
||||
WHERE a.status = 'done'
|
||||
AND a.updated_at >= NOW() - INTERVAL '7 days'
|
||||
) AS done_actions_recent_count
|
||||
FROM actions a
|
||||
JOIN action_assignments aa
|
||||
ON aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
WHERE a.tenant_id = %s AND aa.actor_id = %s
|
||||
""",
|
||||
(ctx.tenant_id, ctx.actor_id),
|
||||
)
|
||||
row = dict(cur.fetchone())
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT COUNT(*) AS active_initiatives_count
|
||||
FROM initiatives
|
||||
WHERE tenant_id = %s AND status IN ('active', 'paused')
|
||||
""",
|
||||
(ctx.tenant_id,),
|
||||
)
|
||||
initiatives_row = dict(cur.fetchone())
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
"open_actions_count": int(row["open_actions_count"] or 0),
|
||||
"blocked_actions_count": int(row["blocked_actions_count"] or 0),
|
||||
"active_initiatives_count": int(initiatives_row["active_initiatives_count"] or 0),
|
||||
"done_actions_recent_count": int(row["done_actions_recent_count"] or 0),
|
||||
}
|
||||
+3
-1
@@ -53,7 +53,7 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
from routers import actions, auth, config, features, initiatives, me, prompts # noqa: E402
|
||||
from routers import actions, actors, auth, config, features, initiatives, me, prompts, workspace # noqa: E402
|
||||
|
||||
app.include_router(auth.router)
|
||||
app.include_router(me.router)
|
||||
@@ -62,6 +62,8 @@ app.include_router(prompts.router)
|
||||
app.include_router(config.router)
|
||||
app.include_router(initiatives.router)
|
||||
app.include_router(actions.router)
|
||||
app.include_router(actors.router)
|
||||
app.include_router(workspace.router)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""Import all module registrations — side effect registers capabilities."""
|
||||
|
||||
from . import initiative_ops, platform, registry_ops, tenant_ops # noqa: F401
|
||||
from . import initiative_ops, platform, registry_ops, tenant_ops, workspace_ops # noqa: F401
|
||||
|
||||
__all__ = ["initiative_ops", "platform", "registry_ops", "tenant_ops"]
|
||||
__all__ = ["initiative_ops", "platform", "registry_ops", "tenant_ops", "workspace_ops"]
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Workspace read capabilities (AP0.7)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from rights_registry import CapabilityRegistration, register_capability
|
||||
|
||||
register_capability(
|
||||
CapabilityRegistration(
|
||||
key="kairo.actor.read",
|
||||
module="tenant",
|
||||
description="Actors im aktiven Tenant lesen (Directory)",
|
||||
default_grants=(
|
||||
("portal", "admin"),
|
||||
("portal", "user"),
|
||||
("tenant", "owner"),
|
||||
("tenant", "admin"),
|
||||
("tenant", "member"),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
register_capability(
|
||||
CapabilityRegistration(
|
||||
key="kairo.workspace.read",
|
||||
module="workspace",
|
||||
description="Workspace-Übersichten und aggregierte Read-Models lesen",
|
||||
default_grants=(
|
||||
("portal", "admin"),
|
||||
("portal", "user"),
|
||||
("tenant", "owner"),
|
||||
("tenant", "admin"),
|
||||
("tenant", "member"),
|
||||
),
|
||||
)
|
||||
)
|
||||
@@ -1,10 +1,11 @@
|
||||
"""Action (Maßnahme) API — AP0.5."""
|
||||
"""Action (Maßnahme) API — AP0.5 / AP0.7."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal, Optional
|
||||
|
||||
from capabilities import require_capability
|
||||
from data_layer import actions as dl_actions
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from services import actions as action_service
|
||||
@@ -28,12 +29,10 @@ class ActionAssignmentsRequest(BaseModel):
|
||||
def list_my_open_actions(
|
||||
ctx: TenantContext = Depends(require_capability("kairo.action.read")),
|
||||
):
|
||||
if not ctx.actor_id:
|
||||
raise HTTPException(status_code=400, detail="Kein Actor im TenantContext")
|
||||
return action_service.list_open_actions_for_actor(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_id=ctx.actor_id,
|
||||
)
|
||||
try:
|
||||
return dl_actions.get_all_my_open_actions(ctx)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/{action_id}")
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Actor directory API — AP0.7."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal, Optional
|
||||
|
||||
from capabilities import require_capability
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from services import actors as actor_service
|
||||
from tenant_context import TenantContext
|
||||
|
||||
router = APIRouter(prefix="/api/actors", tags=["actors"])
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_actors(
|
||||
actor_type: Optional[Literal["human", "agent", "working_group", "external_system"]] = None,
|
||||
include_inactive: bool = Query(default=False),
|
||||
q: Optional[str] = Query(default=None, max_length=100),
|
||||
ctx: TenantContext = Depends(require_capability("kairo.actor.read")),
|
||||
):
|
||||
try:
|
||||
return actor_service.list_actors(
|
||||
tenant_id=ctx.tenant_id,
|
||||
actor_type=actor_type,
|
||||
include_inactive=include_inactive,
|
||||
q=q,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/{actor_id}")
|
||||
def get_actor(
|
||||
actor_id: str,
|
||||
ctx: TenantContext = Depends(require_capability("kairo.actor.read")),
|
||||
):
|
||||
item = actor_service.get_actor(tenant_id=ctx.tenant_id, actor_id=actor_id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Actor nicht gefunden")
|
||||
return item
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Workspace read API — delegates to Data Layer (AP0.7)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from capabilities import require_capability
|
||||
from data_layer import actions as dl_actions
|
||||
from data_layer import actors as dl_actors
|
||||
from data_layer import initiatives as dl_initiatives
|
||||
from data_layer import workspace as dl_workspace
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from tenant_context import TenantContext
|
||||
|
||||
router = APIRouter(prefix="/api/workspace", tags=["workspace"])
|
||||
|
||||
|
||||
def _require_actor_ctx(ctx: TenantContext) -> TenantContext:
|
||||
if not ctx.actor_id:
|
||||
raise HTTPException(status_code=400, detail="Kein Actor im TenantContext")
|
||||
return ctx
|
||||
|
||||
|
||||
@router.get("/summary")
|
||||
def workspace_summary(
|
||||
ctx: TenantContext = Depends(require_capability("kairo.workspace.read")),
|
||||
):
|
||||
return dl_workspace.get_workspace_summary(ctx)
|
||||
|
||||
|
||||
@router.get("/actions/open")
|
||||
def workspace_open_actions(
|
||||
ctx: TenantContext = Depends(require_capability("kairo.workspace.read")),
|
||||
):
|
||||
try:
|
||||
return dl_actions.get_my_open_actions(_require_actor_ctx(ctx))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/actions/blocked")
|
||||
def workspace_blocked_actions(
|
||||
ctx: TenantContext = Depends(require_capability("kairo.workspace.read")),
|
||||
):
|
||||
try:
|
||||
return dl_actions.get_my_blocked_actions(_require_actor_ctx(ctx))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/initiatives/active")
|
||||
def workspace_active_initiatives(
|
||||
limit: Optional[int] = Query(default=None, ge=1, le=100),
|
||||
ctx: TenantContext = Depends(require_capability("kairo.workspace.read")),
|
||||
):
|
||||
return dl_initiatives.get_active_initiatives(ctx, limit=limit)
|
||||
|
||||
|
||||
@router.get("/actors/workload")
|
||||
def workspace_actor_workload(
|
||||
ctx: TenantContext = Depends(require_capability("kairo.workspace.read")),
|
||||
):
|
||||
return dl_actors.get_actor_workload(ctx)
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Actor creation helpers — User and Actor remain separate concepts."""
|
||||
"""Actor creation and directory helpers — User and Actor remain separate concepts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -10,6 +10,20 @@ from db import get_connection
|
||||
|
||||
ActorType = Literal["human", "agent", "working_group", "external_system"]
|
||||
|
||||
ACTOR_TYPES = frozenset({"human", "agent", "working_group", "external_system"})
|
||||
|
||||
|
||||
def _serialize_actor(row: dict[str, Any], *, include_user_id: bool = False) -> dict[str, Any]:
|
||||
result = {
|
||||
"id": str(row["id"]),
|
||||
"name": row["name"],
|
||||
"actor_type": row["actor_type"],
|
||||
"is_active": bool(row["is_active"]),
|
||||
}
|
||||
if include_user_id and row.get("user_id"):
|
||||
result["user_id"] = str(row["user_id"])
|
||||
return result
|
||||
|
||||
|
||||
def create_actor(
|
||||
*,
|
||||
@@ -66,3 +80,70 @@ def get_human_actor(tenant_id: str, user_id: str) -> Optional[dict[str, Any]]:
|
||||
return result
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def list_actors(
|
||||
*,
|
||||
tenant_id: str,
|
||||
actor_type: Optional[str] = None,
|
||||
include_inactive: bool = False,
|
||||
q: Optional[str] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
if actor_type is not None and actor_type not in ACTOR_TYPES:
|
||||
raise ValueError(f"Ungültiger actor_type: {actor_type}")
|
||||
|
||||
conditions = ["tenant_id = %s"]
|
||||
params: list[Any] = [tenant_id]
|
||||
|
||||
if not include_inactive:
|
||||
conditions.append("is_active = TRUE")
|
||||
if actor_type:
|
||||
conditions.append("actor_type = %s")
|
||||
params.append(actor_type)
|
||||
if q:
|
||||
conditions.append("name ILIKE %s")
|
||||
params.append(f"%{q.strip()}%")
|
||||
|
||||
where = " AND ".join(conditions)
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT id, actor_type, name, user_id, is_active
|
||||
FROM actors
|
||||
WHERE {where}
|
||||
ORDER BY
|
||||
CASE actor_type
|
||||
WHEN 'human' THEN 0
|
||||
WHEN 'working_group' THEN 1
|
||||
WHEN 'agent' THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
name
|
||||
""",
|
||||
params,
|
||||
)
|
||||
return [_serialize_actor(dict(row)) for row in cur.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_actor(*, tenant_id: str, actor_id: str) -> Optional[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, actor_type, name, user_id, is_active
|
||||
FROM actors
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(actor_id, tenant_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
return _serialize_actor(dict(row))
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,247 @@
|
||||
"""AP0.7 — Actor directory, workspace data layer, tenant hardening tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from auth import AUTH_HEADER
|
||||
from services.actors import create_actor
|
||||
from tests.factories import provision_user_in_tenant
|
||||
from tests.test_initiatives_actions import (
|
||||
_auth,
|
||||
_create_action,
|
||||
_create_initiative,
|
||||
_login,
|
||||
)
|
||||
|
||||
|
||||
def test_list_actors_tenant_scoped(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
agent = create_actor(tenant_id=user["tenant_id"], actor_type="agent", name="Bot Alpha")
|
||||
|
||||
res = client.get("/api/actors", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
body = res.json()
|
||||
ids = {a["id"] for a in body}
|
||||
assert user["actor_id"] in ids
|
||||
assert agent["id"] in ids
|
||||
types = {a["actor_type"] for a in body}
|
||||
assert "human" in types
|
||||
assert "agent" in types
|
||||
for actor in body:
|
||||
assert "user_id" not in actor
|
||||
|
||||
|
||||
def test_list_actors_filter_by_type(client):
|
||||
user = provision_user_in_tenant(tenant_role="admin")
|
||||
token = _login(client, user)
|
||||
create_actor(tenant_id=user["tenant_id"], actor_type="agent", name="Filter Agent")
|
||||
|
||||
res = client.get("/api/actors?actor_type=agent", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
assert all(a["actor_type"] == "agent" for a in res.json())
|
||||
|
||||
|
||||
def test_list_actors_excludes_inactive_by_default(client):
|
||||
user = provision_user_in_tenant(tenant_role="admin")
|
||||
token = _login(client, user)
|
||||
inactive = create_actor(
|
||||
tenant_id=user["tenant_id"], actor_type="external_system", name="Legacy"
|
||||
)
|
||||
conn = __import__("db").get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("UPDATE actors SET is_active = FALSE WHERE id = %s", (inactive["id"],))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
res = client.get("/api/actors", headers=_auth(token))
|
||||
assert inactive["id"] not in {a["id"] for a in res.json()}
|
||||
|
||||
res_active = client.get("/api/actors?include_inactive=true", headers=_auth(token))
|
||||
assert inactive["id"] in {a["id"] for a in res_active.json()}
|
||||
|
||||
|
||||
def test_get_actor_detail(client):
|
||||
user = provision_user_in_tenant()
|
||||
token = _login(client, user)
|
||||
agent = create_actor(tenant_id=user["tenant_id"], actor_type="working_group", name="Team A")
|
||||
|
||||
res = client.get(f"/api/actors/{agent['id']}", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
assert res.json()["name"] == "Team A"
|
||||
assert res.json()["actor_type"] == "working_group"
|
||||
|
||||
|
||||
def test_cross_tenant_actor_directory_isolation(client):
|
||||
user_a = provision_user_in_tenant()
|
||||
user_b = provision_user_in_tenant()
|
||||
token_a = _login(client, user_a)
|
||||
token_b = _login(client, user_b)
|
||||
agent_a = create_actor(tenant_id=user_a["tenant_id"], actor_type="agent", name="Tenant A Agent")
|
||||
|
||||
list_b = client.get("/api/actors", headers=_auth(token_b)).json()
|
||||
assert agent_a["id"] not in {a["id"] for a in list_b}
|
||||
assert user_a["actor_id"] not in {a["id"] for a in list_b}
|
||||
|
||||
detail = client.get(f"/api/actors/{agent_a['id']}", headers=_auth(token_b))
|
||||
assert detail.status_code == 404
|
||||
|
||||
|
||||
def test_member_has_actor_and_workspace_capabilities(client):
|
||||
member = provision_user_in_tenant(tenant_role="member", portal_role="user")
|
||||
token = _login(client, member)
|
||||
ctx = client.get("/api/me/context", headers=_auth(token)).json()
|
||||
assert "kairo.actor.read" in ctx["capabilities"]
|
||||
assert "kairo.workspace.read" in ctx["capabilities"]
|
||||
|
||||
|
||||
def test_workspace_summary(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
|
||||
open_action = _create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
assigned_actor_ids=[user["actor_id"]],
|
||||
).json()
|
||||
blocked = _create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
title="Blocked",
|
||||
assigned_actor_ids=[user["actor_id"]],
|
||||
).json()
|
||||
client.patch(
|
||||
f"/api/actions/{blocked['id']}",
|
||||
json={"status": "blocked"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
done = _create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
title="Done",
|
||||
assigned_actor_ids=[user["actor_id"]],
|
||||
).json()
|
||||
client.patch(
|
||||
f"/api/actions/{done['id']}",
|
||||
json={"status": "done"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
|
||||
res = client.get("/api/workspace/summary", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
summary = res.json()
|
||||
assert summary["open_actions_count"] >= 1
|
||||
assert summary["blocked_actions_count"] >= 1
|
||||
assert summary["active_initiatives_count"] >= 1
|
||||
assert summary["done_actions_recent_count"] >= 1
|
||||
assert open_action["id"] # used
|
||||
|
||||
|
||||
def test_workspace_open_and_blocked_actions(client):
|
||||
user = provision_user_in_tenant(tenant_role="member")
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
|
||||
open_id = _create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
title="Open",
|
||||
assigned_actor_ids=[user["actor_id"]],
|
||||
).json()["id"]
|
||||
blocked_id = _create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
title="Blocked",
|
||||
assigned_actor_ids=[user["actor_id"]],
|
||||
).json()["id"]
|
||||
client.patch(
|
||||
f"/api/actions/{blocked_id}",
|
||||
json={"status": "blocked"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
|
||||
open_res = client.get("/api/workspace/actions/open", headers=_auth(token))
|
||||
blocked_res = client.get("/api/workspace/actions/blocked", headers=_auth(token))
|
||||
assert open_res.status_code == 200
|
||||
assert blocked_res.status_code == 200
|
||||
assert open_id in {a["id"] for a in open_res.json()}
|
||||
assert blocked_id in {a["id"] for a in blocked_res.json()}
|
||||
assert blocked_id not in {a["id"] for a in open_res.json()}
|
||||
|
||||
|
||||
def test_workspace_active_initiatives(client):
|
||||
user = provision_user_in_tenant()
|
||||
token = _login(client, user)
|
||||
initiative_id = _create_initiative(client, token, title="Active One").json()["id"]
|
||||
|
||||
res = client.get("/api/workspace/initiatives/active?limit=5", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
ids = {i["id"] for i in res.json()}
|
||||
assert initiative_id in ids
|
||||
|
||||
|
||||
def test_workspace_actor_workload(client):
|
||||
user = provision_user_in_tenant(tenant_role="admin")
|
||||
token = _login(client, user)
|
||||
agent = create_actor(tenant_id=user["tenant_id"], actor_type="agent", name="Workload Bot")
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
_create_action(
|
||||
client,
|
||||
token,
|
||||
initiative_id,
|
||||
assigned_actor_ids=[agent["id"]],
|
||||
)
|
||||
|
||||
res = client.get("/api/workspace/actors/workload", headers=_auth(token))
|
||||
assert res.status_code == 200
|
||||
workload = {row["actor_id"]: row for row in res.json()}
|
||||
assert agent["id"] in workload
|
||||
assert workload[agent["id"]]["open_actions"] >= 1
|
||||
|
||||
|
||||
def test_cross_tenant_workspace_summary_isolation(client):
|
||||
user_a = provision_user_in_tenant()
|
||||
user_b = provision_user_in_tenant()
|
||||
token_a = _login(client, user_a)
|
||||
token_b = _login(client, user_b)
|
||||
initiative_id = _create_initiative(client, token_a).json()["id"]
|
||||
_create_action(
|
||||
client,
|
||||
token_a,
|
||||
initiative_id,
|
||||
assigned_actor_ids=[user_a["actor_id"]],
|
||||
)
|
||||
|
||||
summary_b = client.get("/api/workspace/summary", headers=_auth(token_b)).json()
|
||||
assert summary_b["open_actions_count"] == 0
|
||||
|
||||
open_b = client.get("/api/workspace/actions/open", headers=_auth(token_b)).json()
|
||||
assert open_b == []
|
||||
|
||||
|
||||
def test_assign_action_to_tenant_actor(client):
|
||||
user = provision_user_in_tenant(tenant_role="admin")
|
||||
token = _login(client, user)
|
||||
agent = create_actor(tenant_id=user["tenant_id"], actor_type="agent", name="Assignee")
|
||||
initiative_id = _create_initiative(client, token).json()["id"]
|
||||
action_id = _create_action(client, token, initiative_id).json()["id"]
|
||||
|
||||
res = client.put(
|
||||
f"/api/actions/{action_id}/assignments",
|
||||
json={"actor_ids": [agent["id"]]},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert res.status_code == 200
|
||||
assert res.json()["assigned_actor_ids"] == [agent["id"]]
|
||||
|
||||
|
||||
def test_unauthenticated_workspace_rejected(client):
|
||||
assert client.get("/api/workspace/summary").status_code == 401
|
||||
assert client.get("/api/actors").status_code == 401
|
||||
@@ -13,6 +13,7 @@ def test_registry_contains_initial_capabilities():
|
||||
"kairo.admin.access",
|
||||
"kairo.tenant.manage",
|
||||
"kairo.actor.manage",
|
||||
"kairo.actor.read",
|
||||
"kairo.context.read",
|
||||
"kairo.entitlements.read",
|
||||
"kairo.feature.registry.read",
|
||||
@@ -26,6 +27,7 @@ def test_registry_contains_initial_capabilities():
|
||||
"kairo.initiative.manage",
|
||||
"kairo.action.read",
|
||||
"kairo.action.manage",
|
||||
"kairo.workspace.read",
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +39,7 @@ def test_sync_is_idempotent():
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT COUNT(*) FROM capabilities")
|
||||
assert cur.fetchone()[0] == 16
|
||||
assert cur.fetchone()[0] == 18
|
||||
cur.execute("SELECT COUNT(*) FROM role_capability_grants")
|
||||
assert cur.fetchone()[0] >= 5
|
||||
finally:
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
APP_VERSION = "0.6.0-ap0.6"
|
||||
APP_VERSION = "0.7.0-ap0.7"
|
||||
DB_SCHEMA_VERSION = "006"
|
||||
APP_NAME = "jinkendo-kairo"
|
||||
Reference in new issue
Block a user