TT-007.1: establish module-owned authorization boundary

This commit is contained in:
2026-08-10 16:41:52 +03:00
parent 4a26268ede
commit 98fd823513
16 changed files with 3223 additions and 27 deletions
+19 -12
View File
@@ -15,14 +15,24 @@ router = APIRouter(prefix="/auth", tags=["Auth"])
def _build_user_response(user: User, db: Session) -> dict:
"""Build user response for /auth/me.
Platform is authority for:
- User identity
- Platform-level permissions (platform.*)
- Module access list (which modules user can access)
Module is authority for:
- Module permissions (request.*, etc.)
- Effective policy computation
"""
global_roles = [ur.role.name for ur in user.user_roles if ur.role]
mod_roles = {}
# Collect permissions from global roles
# Collect PLATFORM permissions only (Platform is authority for platform.*)
permissions = set()
for ur in user.user_roles:
if ur.role:
# Map role names to permissions
role = ur.role
if role.name == "platform_admin":
permissions.update([
@@ -40,19 +50,15 @@ def _build_user_response(user: User, db: Session) -> dict:
"platform.users.write",
])
# Collect module roles
# Collect module roles (for module access, NOT for permission computation)
# Module permissions are computed by Module, not Platform
module_access = []
for umr in user.user_module_roles:
if umr.module_role and umr.module_role.module:
slug = umr.module_role.module.slug
mod_roles.setdefault(slug, []).append(umr.module_role.role_name)
# Add module permissions based on role
role_name = umr.module_role.role_name
if role_name == "admin":
permissions.update([f"{slug}.read", f"{slug}.write", f"{slug}.delete"])
elif role_name in ("support_agent", "manager"):
permissions.update([f"{slug}.read", f"{slug}.write"])
elif role_name == "customer":
permissions.add(f"{slug}.read")
if slug not in module_access:
module_access.append(slug)
return {
"id": user.id,
@@ -63,7 +69,8 @@ def _build_user_response(user: User, db: Session) -> dict:
"is_superadmin": user.is_superadmin,
"roles": global_roles,
"module_roles": mod_roles,
"permissions": list(permissions),
"permissions": list(permissions), # Platform permissions only
"module_access": module_access, # Which modules user can access
}
@@ -0,0 +1,75 @@
"""Authorization Boundary Tests — TT-007.1
Tests for Module-Owned Permission Boundary.
Verifies that:
- Module is authority for its own permissions
- Platform does not compute module permissions
- /auth/me returns identity + module_access, not module permissions
"""
import pytest
def test_auth_me_returns_module_access(client, admin_token):
"""Test that /auth/me returns module_access field."""
response = client.get("/api/v1/auth/me", headers={
"Authorization": f"Bearer {admin_token}"
})
assert response.status_code == 200
data = response.json()
assert "module_access" in data
assert isinstance(data["module_access"], list)
def test_auth_me_no_module_permissions(client, admin_token):
"""Test that /auth/me does NOT return module permissions (request.*, etc.)."""
response = client.get("/api/v1/auth/me", headers={
"Authorization": f"Bearer {admin_token}"
})
assert response.status_code == 200
data = response.json()
permissions = data.get("permissions", [])
# Module permissions should NOT be in /auth/me
module_perms = [p for p in permissions if p.startswith("request.")]
assert len(module_perms) == 0, f"Module permissions found in /auth/me: {module_perms}"
def test_auth_me_has_platform_permissions(client, admin_token):
"""Test that /auth/me still returns platform permissions."""
response = client.get("/api/v1/auth/me", headers={
"Authorization": f"Bearer {admin_token}"
})
assert response.status_code == 200
data = response.json()
# Platform permissions should still be there
assert "platform.dashboard.read" in data["permissions"]
assert "platform.users.read" in data["permissions"]
def test_auth_me_identity_fields(client, admin_token):
"""Test that /auth/me returns identity fields."""
response = client.get("/api/v1/auth/me", headers={
"Authorization": f"Bearer {admin_token}"
})
assert response.status_code == 200
data = response.json()
# Identity fields
assert "id" in data
assert "email" in data
assert "first_name" in data
assert "last_name" in data
assert "is_active" in data
assert "is_superadmin" in data
assert "roles" in data
def test_login_returns_user_with_module_access(client, admin_user):
"""Test that login response includes module_access."""
response = client.post("/api/v1/auth/login", json={
"email": "admin@test.com",
"password": "admin123",
})
assert response.status_code == 200
data = response.json()
assert "user" in data
assert "module_access" in data["user"]