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

79 lines
3.1 KiB
Python

from __future__ import annotations
import pytest
from httpx import AsyncClient
@pytest.mark.integration
class TestChannelsStatusAPI:
async def test_list_channels_status(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.get("/api/channels/status", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert data["code"] == 0
assert "channels" in data["data"]
async def test_get_single_channel_status_not_found(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.get("/api/channels/nonexistent/status", headers=admin_headers)
assert response.status_code == 404
async def test_list_channels_requires_admin(self, test_client: AsyncClient, standard_user: dict):
response = await test_client.get("/api/channels/status", headers=standard_user["headers"])
assert response.status_code in (401, 403)
@pytest.mark.integration
class TestChannelStartStop:
async def test_start_nonexistent_channel(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.post("/api/channels/nonexistent/start", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert data["code"] == -1
async def test_stop_not_running_channel(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.post("/api/channels/telegram/stop", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert data["code"] == 409
@pytest.mark.integration
class TestChannelConfig:
async def test_update_config_nonexistent_channel(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.put(
"/api/channels/nonexistent/config",
json={"enabled": True},
headers=admin_headers,
)
assert response.status_code == 200
data = response.json()
assert data["code"] == -1
@pytest.mark.integration
class TestChannelStats:
async def test_get_channel_stats(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.get("/api/channels/telegram/stats", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert data["code"] == 0
assert "summary" in data["data"]
async def test_get_channel_stats_with_params(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.get(
"/api/channels/telegram/stats",
params={"days": 30, "granularity": "day"},
headers=admin_headers,
)
assert response.status_code == 200
data = response.json()
assert data["code"] == 0
@pytest.mark.integration
class TestChannelTestConnection:
async def test_test_channel_nonexistent(self, test_client: AsyncClient, admin_headers: dict):
response = await test_client.post("/api/channels/nonexistent/test", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert data["code"] == -1