ForcePilot/backend/test/unit/channels/test_telegram_security.py
Kris 3264900bc9 test: 新增多渠道单元测试用例并配置测试环境变量
新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块
同时在测试配置中添加了测试用的OpenAI API密钥环境变量
2026-05-12 00:56:47 +08:00

79 lines
2.8 KiB
Python

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"]