ForcePilot/backend/test/unit/channels/test_channels_slack_scopes.py
Kris 69fe97a90d test: 批量修复并新增单元测试用例
1. 移除Telegram格式化测试中未使用的导入项
2. 修复Teams测试用例,添加monkeypatch参数并配置通配符开关
3. 更新钉钉适配器测试,替换弃用的流属性检查
4. 修正Twitch规范化测试,更新ROOMSTATE测试逻辑
5. 重构会话映射测试,完善数据库执行结果模拟
6. 格式化Slack块构建测试的长参数调用
7. 修复LINE适配器测试,更新能力断言和异步锁使用
8. 修正Slack会话解析测试,修复聊天类型判断错误
9. 更新能力测试,补充缺失的字段检查
10. 修复Matrix适配器测试,修正位置参数和配置校验逻辑
11. 为飞书分析模块测试添加跳过标记
12. 新增微信能力、限流、链接格式、会话路由等模块的单元测试
13. 修复Twitch适配器导入路径和测试断言
14. 新增Discord Webhook、Nextcloud Talk、Signal多账户等模块的单元测试
15. 修复Manager阶段测试的导入路径
16. 新增iMessage异常和命令处理的单元测试
17. 新增Nostr健康检查和相关模块的单元测试
18. 新增Signal守护进程和SSE重连相关测试
2026-05-13 16:43:01 +08:00

162 lines
5.8 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
from slack_sdk.errors import SlackApiError
from yuxi.channels.adapters.slack.scopes import (
ScopesInfo,
fetch_scopes,
validate_required_scopes,
)
class TestScopesInfo:
def test_defaults(self):
info = ScopesInfo()
assert info.bot_scopes == []
assert info.user_scopes == []
assert info.error == ""
def test_has_chat_write(self):
info = ScopesInfo(bot_scopes=["chat:write", "files:read"])
assert info.has_chat_write is True
assert info.has_chat_write_customize is False
assert info.has_files_write is False
def test_has_chat_write_customize(self):
info = ScopesInfo(bot_scopes=["chat:write.customize"])
assert info.has_chat_write_customize is True
assert info.has_chat_write is False
def test_has_files_write(self):
info = ScopesInfo(bot_scopes=["files:write"])
assert info.has_files_write is True
def test_has_channels_manage(self):
info = ScopesInfo(bot_scopes=["channels:manage"])
assert info.has_channels_manage is True
def test_empty_scopes(self):
info = ScopesInfo(bot_scopes=[])
assert info.has_chat_write is False
assert info.has_chat_write_customize is False
assert info.has_files_write is False
assert info.has_channels_manage is False
def test_to_dict(self):
info = ScopesInfo(
bot_scopes=["chat:write", "files:write"],
user_scopes=["identity.basic"],
error="",
)
d = info.to_dict()
assert d["bot_scopes"] == ["chat:write", "files:write"]
assert d["has_chat_write"] is True
assert d["has_files_write"] is True
assert d["has_channels_manage"] is False
class TestFetchScopes:
@pytest.mark.asyncio
async def test_fetch_scopes_success(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(
return_value={"headers": {"x-oauth-scopes": "chat:write, files:read, channels:read"}}
)
result = await fetch_scopes(mock_client)
assert result.error == ""
assert "chat:write" in result.bot_scopes
assert "files:read" in result.bot_scopes
assert "channels:read" in result.bot_scopes
@pytest.mark.asyncio
async def test_fetch_scopes_auth_test_fails(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": False, "error": "invalid_auth"})
result = await fetch_scopes(mock_client)
assert "invalid_auth" in result.error
assert result.bot_scopes == []
@pytest.mark.asyncio
async def test_fetch_scopes_slack_api_error(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(side_effect=SlackApiError("error", {"ok": False, "error": "token_revoked"}))
result = await fetch_scopes(mock_client)
assert result.error != ""
assert result.bot_scopes == []
@pytest.mark.asyncio
async def test_fetch_scopes_generic_exception(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(side_effect=Exception("network error"))
result = await fetch_scopes(mock_client)
assert "network error" in result.error
@pytest.mark.asyncio
async def test_fetch_scopes_scopes_as_list(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(return_value={"headers": {"x-oauth-scopes": ["chat:write", "files:read"]}})
result = await fetch_scopes(mock_client)
assert result.bot_scopes == ["chat:write", "files:read"]
@pytest.mark.asyncio
async def test_fetch_scopes_empty_scopes(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(return_value={"headers": {"x-oauth-scopes": ""}})
result = await fetch_scopes(mock_client)
assert result.bot_scopes == []
class TestValidateRequiredScopes:
@pytest.mark.asyncio
async def test_all_present(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(
return_value={"headers": {"x-oauth-scopes": "chat:write, files:write, channels:manage"}}
)
ok, missing = await validate_required_scopes(mock_client, ["chat:write", "files:write"])
assert ok is True
assert missing == []
@pytest.mark.asyncio
async def test_some_missing(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(return_value={"headers": {"x-oauth-scopes": "chat:write"}})
ok, missing = await validate_required_scopes(mock_client, ["chat:write", "files:write"])
assert ok is False
assert "files:write" in missing
@pytest.mark.asyncio
async def test_all_missing(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(return_value={"headers": {"x-oauth-scopes": "chat:write"}})
ok, missing = await validate_required_scopes(mock_client, ["admin", "audit:read"])
assert ok is False
assert set(missing) == {"admin", "audit:read"}
@pytest.mark.asyncio
async def test_empty_required(self):
mock_client = AsyncMock()
mock_client.auth_test = AsyncMock(return_value={"ok": True})
mock_client.api_call = AsyncMock(return_value={"headers": {"x-oauth-scopes": "chat:write"}})
ok, missing = await validate_required_scopes(mock_client, [])
assert ok is True
assert missing == []