78 行
2.3 KiB
Python
78 行
2.3 KiB
Python
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
from access_policy import AccessPolicy, PlatformIdentity, identity_from_event
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class FakeEvent:
|
||
|
|
platform_name: str = "aiocqhttp"
|
||
|
|
platform_id: str = "bot-1"
|
||
|
|
user_id: str = "100"
|
||
|
|
group_id: str = ""
|
||
|
|
unified_msg_origin: str = "aiocqhttp:friend:100"
|
||
|
|
|
||
|
|
def get_platform_name(self) -> str:
|
||
|
|
return self.platform_name
|
||
|
|
|
||
|
|
def get_platform_id(self) -> str:
|
||
|
|
return self.platform_id
|
||
|
|
|
||
|
|
def get_sender_id(self) -> str:
|
||
|
|
return self.user_id
|
||
|
|
|
||
|
|
def get_group_id(self) -> str:
|
||
|
|
return self.group_id
|
||
|
|
|
||
|
|
|
||
|
|
def policy() -> AccessPolicy:
|
||
|
|
return AccessPolicy(
|
||
|
|
{
|
||
|
|
"allowed_platforms": ["aiocqhttp", "telegram"],
|
||
|
|
"private_user_ids": ["100"],
|
||
|
|
"group_permissions": [
|
||
|
|
{"group_id": "200", "user_ids": ["100", "101"]}
|
||
|
|
],
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_identity_uses_astrbot_event_fields() -> None:
|
||
|
|
identity = identity_from_event(FakeEvent())
|
||
|
|
|
||
|
|
assert identity == PlatformIdentity(
|
||
|
|
platform_name="aiocqhttp",
|
||
|
|
platform_id="bot-1",
|
||
|
|
user_id="100",
|
||
|
|
group_id="",
|
||
|
|
session_origin="aiocqhttp:friend:100",
|
||
|
|
)
|
||
|
|
assert identity.is_private
|
||
|
|
|
||
|
|
|
||
|
|
def test_private_user_whitelist() -> None:
|
||
|
|
access_policy = policy()
|
||
|
|
assert access_policy.is_allowed(FakeEvent(user_id="100"))
|
||
|
|
assert not access_policy.is_allowed(FakeEvent(user_id="999"))
|
||
|
|
|
||
|
|
|
||
|
|
def test_group_requires_group_and_user_whitelist() -> None:
|
||
|
|
access_policy = policy()
|
||
|
|
assert access_policy.is_allowed(FakeEvent(user_id="100", group_id="200"))
|
||
|
|
assert access_policy.is_allowed(FakeEvent(user_id="101", group_id="200"))
|
||
|
|
assert not access_policy.is_allowed(FakeEvent(user_id="999", group_id="200"))
|
||
|
|
assert not access_policy.is_allowed(FakeEvent(user_id="100", group_id="999"))
|
||
|
|
|
||
|
|
|
||
|
|
def test_platform_and_missing_identity_are_rejected() -> None:
|
||
|
|
access_policy = policy()
|
||
|
|
assert not access_policy.is_allowed(FakeEvent(platform_name="discord"))
|
||
|
|
assert not access_policy.is_allowed(FakeEvent(user_id=""))
|
||
|
|
|
||
|
|
|
||
|
|
def test_mapping_group_config_is_supported() -> None:
|
||
|
|
access_policy = AccessPolicy(
|
||
|
|
{"private_user_ids": [], "group_permissions": {"200": ["100"]}}
|
||
|
|
)
|
||
|
|
|
||
|
|
assert access_policy.is_allowed(FakeEvent(user_id="100", group_id="200"))
|