AP0.3: Capability Registry, Entitlements-Snapshot und require_capability.
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Failing after 4s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 1s
Test Suite / compose-smoke (push) Has been skipped
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Failing after 4s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 1s
Test Suite / compose-smoke (push) Has been skipped
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
f9c207c4f6
commit
910b1d7e07
19 files changed
+788
-8
No files matched your search
@@ -9,6 +9,7 @@ import pytest
|
||||
os.environ.setdefault("SKIP_DB_MIGRATE", "1")
|
||||
os.environ.setdefault("SKIP_SEEDS", "1")
|
||||
os.environ.setdefault("SKIP_BOOTSTRAP", "1")
|
||||
os.environ.setdefault("SKIP_RIGHTS_SYNC", "1")
|
||||
|
||||
|
||||
def _db_available() -> bool:
|
||||
@@ -48,6 +49,14 @@ def _seed_hygiene():
|
||||
run_seeds.main(["--only", "seed_001_cleanup_pytest_artifacts"])
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def _sync_rights_registry():
|
||||
import rights_registrations # noqa: F401
|
||||
from rights_registry import sync_rights_registry_to_db
|
||||
|
||||
assert sync_rights_registry_to_db() == 0
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client():
|
||||
import importlib
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Capability resolution and require_capability tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from auth import AUTH_HEADER
|
||||
from tests.factories import provision_user_in_tenant
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def enforce_capabilities(monkeypatch):
|
||||
monkeypatch.setenv("CAPABILITY_ENFORCE", "enforce")
|
||||
|
||||
|
||||
def test_portal_admin_has_admin_capability(client, enforce_capabilities):
|
||||
admin = provision_user_in_tenant(tenant_role="member", portal_role="admin")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": admin["email"], "password": admin["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
res = client.get("/api/me/admin/demo", headers={AUTH_HEADER: token})
|
||||
assert res.status_code == 200
|
||||
assert res.json()["ok"] is True
|
||||
|
||||
|
||||
def test_tenant_member_denied_admin_capability(client, enforce_capabilities):
|
||||
member = provision_user_in_tenant(tenant_role="member", portal_role="user")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": member["email"], "password": member["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
res = client.get("/api/me/admin/demo", headers={AUTH_HEADER: token})
|
||||
assert res.status_code == 403
|
||||
|
||||
|
||||
def test_tenant_owner_has_manage_capabilities(client, enforce_capabilities):
|
||||
owner = provision_user_in_tenant(tenant_role="owner", portal_role="user")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": owner["email"], "password": owner["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
ctx = client.get("/api/me/context", headers={AUTH_HEADER: token}).json()
|
||||
|
||||
assert "kairo.tenant.manage" in ctx["capabilities"]
|
||||
assert "kairo.actor.manage" in ctx["capabilities"]
|
||||
assert "kairo.admin.access" not in ctx["capabilities"]
|
||||
|
||||
|
||||
def test_probe_mode_allows_without_capability(client, monkeypatch):
|
||||
monkeypatch.setenv("CAPABILITY_ENFORCE", "probe")
|
||||
member = provision_user_in_tenant(tenant_role="member", portal_role="user")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": member["email"], "password": member["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
res = client.get("/api/me/admin/demo", headers={AUTH_HEADER: token})
|
||||
assert res.status_code == 200
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Entitlements snapshot tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from auth import AUTH_HEADER
|
||||
from tests.factories import provision_user_in_tenant
|
||||
|
||||
|
||||
def test_entitlements_snapshot_shape(client):
|
||||
user = provision_user_in_tenant(tenant_role="owner", portal_role="admin")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
res = client.get("/api/me/entitlements", headers={AUTH_HEADER: token})
|
||||
assert res.status_code == 200
|
||||
body = res.json()
|
||||
|
||||
assert "account" in body
|
||||
assert body["account"]["portal_role"] == "admin"
|
||||
assert "capabilities" in body["account"]
|
||||
assert body["tenant"]["tenant_id"] == user["tenant_id"]
|
||||
assert body["actor"]["actor_id"] == user["actor_id"]
|
||||
assert body["roles"]["portal"] == "admin"
|
||||
assert body["roles"]["tenant"] == "owner"
|
||||
assert "kairo.admin.access" in body["capabilities"]
|
||||
assert body["enforcement"]["capabilities"] in ("probe", "enforce")
|
||||
assert body["enforcement"]["features"] == "probe"
|
||||
assert body["features"] == {}
|
||||
@@ -34,6 +34,7 @@ def test_migration_runner_finds_migrations():
|
||||
assert "001_init_core" in names
|
||||
assert "002_auth_identity_tenant_actor" in names
|
||||
assert "003_data_seeds_tracking" in names
|
||||
assert "004_capabilities_registry" in names
|
||||
|
||||
|
||||
def test_migration_runner_is_idempotent():
|
||||
@@ -46,6 +47,7 @@ def test_migration_runner_is_idempotent():
|
||||
assert "001_init_core" in executed
|
||||
assert "002_auth_identity_tenant_actor" in executed
|
||||
assert "003_data_seeds_tracking" in executed
|
||||
assert "004_capabilities_registry" in executed
|
||||
|
||||
|
||||
def test_core_table_exists():
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Rights registry and DB sync tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import rights_registrations # noqa: F401
|
||||
from db import get_connection
|
||||
from rights_registry import get_registered_capabilities, sync_rights_registry_to_db
|
||||
|
||||
|
||||
def test_registry_contains_initial_capabilities():
|
||||
keys = {reg.key for reg in get_registered_capabilities()}
|
||||
assert keys == {
|
||||
"kairo.admin.access",
|
||||
"kairo.tenant.manage",
|
||||
"kairo.actor.manage",
|
||||
"kairo.context.read",
|
||||
"kairo.entitlements.read",
|
||||
}
|
||||
|
||||
|
||||
def test_sync_is_idempotent():
|
||||
assert sync_rights_registry_to_db() == 0
|
||||
assert sync_rights_registry_to_db() == 0
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT COUNT(*) FROM capabilities")
|
||||
assert cur.fetchone()[0] == 5
|
||||
cur.execute("SELECT COUNT(*) FROM role_capability_grants")
|
||||
assert cur.fetchone()[0] >= 5
|
||||
finally:
|
||||
conn.close()
|
||||
Reference in new issue
Block a user