64 行
2.2 KiB
Python
64 行
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from backend.services.generation_service import _permission_context
|
|
from core.identity import RequestContext
|
|
|
|
|
|
def _ctx(user: str) -> RequestContext:
|
|
return RequestContext.from_astrbot(
|
|
username=user, user_id=user, client_ip="127.0.0.1", source="test", permissions=frozenset()
|
|
)
|
|
|
|
|
|
CONFIG = {"chat_access": {"adult_user_allowlist": ["user-allowed"]}}
|
|
|
|
|
|
def test_client_cannot_grant_itself_adult_permission() -> None:
|
|
"""The bypass: POST content_permissions.adult_user_allowed=true and walk through."""
|
|
|
|
result = _permission_context(
|
|
{"content_permissions": {"adult_user_allowed": True}},
|
|
_ctx("user-not-allowed"),
|
|
CONFIG,
|
|
)
|
|
assert result["adult_user_allowed"] is False
|
|
|
|
|
|
def test_allowlisted_user_keeps_adult_permission() -> None:
|
|
"""The legitimate chat path must not regress."""
|
|
|
|
result = _permission_context(
|
|
{"content_permissions": {"adult_user_allowed": True}},
|
|
_ctx("user-allowed"),
|
|
CONFIG,
|
|
)
|
|
assert result["adult_user_allowed"] is True
|
|
|
|
|
|
def test_allowlisted_user_who_does_not_ask_stays_off() -> None:
|
|
"""Being on the allowlist is necessary, not sufficient — the claim still gates."""
|
|
|
|
result = _permission_context({}, _ctx("user-allowed"), CONFIG)
|
|
assert result["adult_user_allowed"] is False
|
|
|
|
|
|
def test_missing_context_or_config_fails_closed() -> None:
|
|
payload = {"content_permissions": {"adult_user_allowed": True}}
|
|
|
|
assert _permission_context(payload, None, CONFIG)["adult_user_allowed"] is False
|
|
assert _permission_context(payload, _ctx("user-allowed"), None)["adult_user_allowed"] is False
|
|
assert _permission_context(payload, _ctx("user-allowed"), {})["adult_user_allowed"] is False
|
|
|
|
|
|
def test_top_level_adult_flag_is_also_verified() -> None:
|
|
"""The alternate ``payload['adult_user_allowed']`` spelling is gated too."""
|
|
|
|
result = _permission_context({"adult_user_allowed": True}, _ctx("user-not-allowed"), CONFIG)
|
|
assert result["adult_user_allowed"] is False
|
|
|
|
|
|
def test_quota_allowed_default_is_unchanged() -> None:
|
|
assert _permission_context({}, _ctx("u"), CONFIG)["quota_allowed"] is True
|
|
assert (
|
|
_permission_context({"quota_allowed": False}, _ctx("u"), CONFIG)["quota_allowed"] is False
|
|
)
|