from __future__ import annotations import pytest from yuxi.channels.adapters.telegram.security import TelegramSecurityPolicy class TestTelegramSecurityPolicy: @pytest.fixture def policy_open(self): return TelegramSecurityPolicy({"dm_policy": "open", "group_policy": "open"}) @pytest.fixture def policy_disabled(self): return TelegramSecurityPolicy({"dm_policy": "disabled", "group_policy": "disabled"}) @pytest.fixture def policy_allowlist(self): return TelegramSecurityPolicy({ "dm_policy": "allowlist", "allow_from": ["tg:111"], "group_policy": "allowlist", "group_allow_from": ["tg:222"], "groups": { "-100": { "allow_from": ["tg:333"], "require_mention": True, } }, }) def test_dm_open(self, policy_open): allowed, msg = policy_open.check_dm_policy("any_user") assert allowed is True assert msg == "" def test_dm_disabled(self, policy_disabled): allowed, msg = policy_disabled.check_dm_policy("any_user") assert allowed is False assert "disabled" in msg.lower() def test_dm_allowlist_approved(self, policy_allowlist): allowed, msg = policy_allowlist.check_dm_policy("111") assert allowed is True def test_dm_allowlist_denied(self, policy_allowlist): allowed, msg = policy_allowlist.check_dm_policy("999") assert allowed is False def test_group_open(self, policy_open): allowed, msg = policy_open.check_group_policy("-100", "any_user") assert allowed is True def test_group_disabled(self, policy_disabled): allowed, msg = policy_disabled.check_group_policy("-100", "any_user") assert allowed is False def test_group_global_allowlist_approved(self, policy_allowlist): allowed, msg = policy_allowlist.check_group_policy("-200", "222") assert allowed is True def test_group_per_group_allowlist(self, policy_allowlist): allowed, msg = policy_allowlist.check_group_policy("-100", "333") assert allowed is True def test_group_allowlist_denied(self, policy_allowlist): allowed, msg = policy_allowlist.check_group_policy("-200", "999") assert allowed is False def test_require_mention(self, policy_allowlist): assert policy_allowlist.check_require_mention("-100") is True def test_get_group_config_default(self, policy_allowlist): config = policy_allowlist.get_group_config("-200") assert "allow_from" not in config def test_get_group_config_specific(self, policy_allowlist): config = policy_allowlist.get_group_config("-100") assert "tg:333" in config["allow_from"]