Enhance Tenant Context and Access Control Features
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Failing after 0s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Failing after 4m0s
Test Suite / playwright-tests (push) Failing after 3m41s
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Failing after 0s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Failing after 4m0s
Test Suite / playwright-tests (push) Failing after 3m41s
- Introduced `email_verified` and `account_state` attributes in the `TenantContext` to improve user state management. - Updated the `resolve_tenant_context` function to dynamically fetch `email_verified` status from the database and determine `account_state` based on user roles and memberships. - Implemented `assert_min_account_state` checks across various endpoints to enforce access control based on user account status. - Incremented version to 1.1.0 in version.py to reflect these enhancements in tenant context management and access control.
This commit is contained in:
1 parent
7cfbca40bb
commit
30dc30c7aa
21 files changed
+1124
-22
No files matched your search
@@ -92,6 +92,7 @@ def test_resolve_platform_admin_uses_stored_club_without_header(monkeypatch):
|
||||
header_raw=None,
|
||||
memberships=[{"id": 10}],
|
||||
stored_active_club_id=99,
|
||||
email_verified=True,
|
||||
)
|
||||
assert ctx.effective_club_id == 99
|
||||
|
||||
@@ -110,6 +111,7 @@ def test_resolve_platform_admin_header_overrides_stored(monkeypatch):
|
||||
header_raw="5",
|
||||
memberships=[{"id": 10}],
|
||||
stored_active_club_id=99,
|
||||
email_verified=True,
|
||||
)
|
||||
assert ctx.effective_club_id == 5
|
||||
|
||||
@@ -124,6 +126,7 @@ def test_resolve_platform_admin_no_header_stored_invalid(monkeypatch):
|
||||
header_raw=None,
|
||||
memberships=[{"id": 1}],
|
||||
stored_active_club_id=123,
|
||||
email_verified=True,
|
||||
)
|
||||
assert ctx.effective_club_id is None
|
||||
|
||||
@@ -142,6 +145,7 @@ def test_resolve_trainer_club_ids_excludes_inactive_memberships():
|
||||
],
|
||||
stored_active_club_id=None,
|
||||
invalid_header_policy="ignore",
|
||||
email_verified=True,
|
||||
)
|
||||
assert ctx.club_ids == frozenset({20})
|
||||
assert ctx.effective_club_id == 20
|
||||
@@ -157,6 +161,7 @@ def test_resolve_all_memberships_inactive_no_effective_club():
|
||||
memberships=[{"id": 10, "membership_status": "inactive"}],
|
||||
stored_active_club_id=10,
|
||||
invalid_header_policy="ignore",
|
||||
email_verified=True,
|
||||
)
|
||||
assert ctx.club_ids == frozenset()
|
||||
assert ctx.effective_club_id is None
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Unit-Tests für Account-Lifecycle und Capability-Helfer (ohne DB)."""
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from account_lifecycle import (
|
||||
account_state_satisfies,
|
||||
assert_min_account_state,
|
||||
resolve_account_state,
|
||||
)
|
||||
from capabilities import club_roles_in_club
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def test_resolve_account_state_platform_admin():
|
||||
assert (
|
||||
resolve_account_state(email_verified=False, global_role="superadmin", has_active_membership=False)
|
||||
== "platform_admin"
|
||||
)
|
||||
|
||||
|
||||
def test_resolve_account_state_unverified():
|
||||
assert (
|
||||
resolve_account_state(email_verified=False, global_role="trainer", has_active_membership=True)
|
||||
== "unverified"
|
||||
)
|
||||
|
||||
|
||||
def test_resolve_account_state_pending_club():
|
||||
assert (
|
||||
resolve_account_state(email_verified=True, global_role="user", has_active_membership=False)
|
||||
== "verified_pending_club"
|
||||
)
|
||||
|
||||
|
||||
def test_resolve_account_state_active_member():
|
||||
assert (
|
||||
resolve_account_state(email_verified=True, global_role="trainer", has_active_membership=True)
|
||||
== "active_member"
|
||||
)
|
||||
|
||||
|
||||
def test_account_state_satisfies():
|
||||
assert account_state_satisfies("active_member", "active_member")
|
||||
assert account_state_satisfies("active_member", "verified_pending_club")
|
||||
assert not account_state_satisfies("verified_pending_club", "active_member")
|
||||
assert account_state_satisfies("platform_admin", "active_member")
|
||||
|
||||
|
||||
def test_assert_min_account_state_blocks(monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_GATE_ENFORCE", "1")
|
||||
tenant = TenantContext(
|
||||
profile_id=1,
|
||||
global_role="user",
|
||||
effective_club_id=None,
|
||||
club_ids=frozenset(),
|
||||
memberships=[],
|
||||
account_state="verified_pending_club",
|
||||
)
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
assert_min_account_state(tenant, "active_member")
|
||||
assert exc.value.status_code == 403
|
||||
|
||||
|
||||
def test_assert_min_account_state_off(monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_GATE_ENFORCE", "0")
|
||||
tenant = TenantContext(
|
||||
profile_id=1,
|
||||
global_role="user",
|
||||
effective_club_id=None,
|
||||
club_ids=frozenset(),
|
||||
memberships=[],
|
||||
account_state="verified_pending_club",
|
||||
)
|
||||
assert_min_account_state(tenant, "active_member")
|
||||
|
||||
|
||||
def test_club_roles_in_club():
|
||||
tenant = TenantContext(
|
||||
profile_id=1,
|
||||
global_role="trainer",
|
||||
effective_club_id=5,
|
||||
club_ids=frozenset({5}),
|
||||
memberships=[{"id": 5, "roles": ["trainer", "club_admin"]}],
|
||||
)
|
||||
assert club_roles_in_club(tenant, 5) == ["trainer", "club_admin"]
|
||||
assert club_roles_in_club(tenant, 99) == []
|
||||
@@ -0,0 +1,75 @@
|
||||
"""Tests für GET /me/entitlements Zusammenstellung."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from entitlements import _serialize_reset_at, build_me_entitlements
|
||||
from tenant_context import TenantContext
|
||||
|
||||
|
||||
def test_serialize_reset_at():
|
||||
dt = datetime(2026, 7, 1, tzinfo=timezone.utc)
|
||||
assert _serialize_reset_at(dt) == "2026-07-01T00:00:00+00:00"
|
||||
assert _serialize_reset_at(None) is None
|
||||
|
||||
|
||||
def test_build_me_entitlements_no_club(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
"entitlements.resolve_capabilities_map",
|
||||
lambda cur, tenant, club_id=None: {"exercises.read": False},
|
||||
)
|
||||
|
||||
tenant = TenantContext(
|
||||
profile_id=1,
|
||||
global_role="user",
|
||||
effective_club_id=None,
|
||||
club_ids=frozenset(),
|
||||
memberships=[],
|
||||
account_state="verified_pending_club",
|
||||
)
|
||||
out = build_me_entitlements(object(), tenant)
|
||||
assert out["account_state"] == "verified_pending_club"
|
||||
assert out["club_id"] is None
|
||||
assert out["features"] == {}
|
||||
assert out["capabilities"]["exercises.read"] is False
|
||||
|
||||
|
||||
def test_build_me_entitlements_with_club(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
"entitlements.resolve_capabilities_map",
|
||||
lambda cur, tenant, club_id=None: {
|
||||
"exercises.read": True,
|
||||
"exercises.ai.suggest": True,
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"entitlements.club_features_map",
|
||||
lambda cur, club_id, conn=None: {
|
||||
"plan_id": "free",
|
||||
"club_id": club_id,
|
||||
"features": {
|
||||
"ai_calls": {
|
||||
"allowed": False,
|
||||
"used": 0,
|
||||
"limit": 0,
|
||||
"remaining": 0,
|
||||
"reason": "feature_disabled",
|
||||
"reset_at": None,
|
||||
}
|
||||
},
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr("entitlements._club_exists", lambda cur, cid: True)
|
||||
|
||||
tenant = TenantContext(
|
||||
profile_id=3,
|
||||
global_role="trainer",
|
||||
effective_club_id=1,
|
||||
club_ids=frozenset({1}),
|
||||
memberships=[{"id": 1, "roles": ["trainer"]}],
|
||||
account_state="active_member",
|
||||
)
|
||||
out = build_me_entitlements(object(), tenant, club_id=1)
|
||||
assert out["club_id"] == 1
|
||||
assert out["plan_id"] == "free"
|
||||
assert out["club_roles"] == ["trainer"]
|
||||
assert out["features"]["ai_calls"]["limit"] == 0
|
||||
assert out["capabilities"]["exercises.ai.suggest"] is True
|
||||
Reference in new issue
Block a user