ForcePilot/backend/test/integration/api/channels/test_dashboard_router.py
Kris 696f5f44ec chore: 完成多模块迭代优化与测试覆盖
这一批提交包含:
1. 配置项敏感字段标记与测试用例修复
2. 路由绑定乐观锁支持与静态路径校验
3. 微信WOC插件能力适配与新增单元测试
4. 多个适配器的接口对齐与测试补全
5. 新增定时任务清理处理器与依赖注入容器测试
6. 错误码体系扩展与整合测试
7. 删除临时验证脚本与代码清理
2026-07-08 12:58:43 +08:00

201 lines
6.6 KiB
Python

"""Integration tests for channels dashboard_router endpoints."""
from __future__ import annotations
import httpx
import pytest
from .conftest import BASE_URL, DEFAULT_CHANNEL_TYPE, ISO_TIME_END, ISO_TIME_START
pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
DASHBOARD_URL = f"{BASE_URL}/dashboard"
# =============================================================================
# === Auth three-tier for /dashboard/overview ===
# =============================================================================
async def test_get_dashboard_overview_requires_auth(test_client: httpx.AsyncClient):
# Act
response = await test_client.get(f"{DASHBOARD_URL}/overview")
# Assert
assert response.status_code == 401, response.text
async def test_get_dashboard_overview_requires_admin(test_client: httpx.AsyncClient, standard_user):
# Act
response = await test_client.get(f"{DASHBOARD_URL}/overview", headers=standard_user["headers"])
# Assert
assert response.status_code == 403, response.text
async def test_admin_can_get_dashboard_overview(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(f"{DASHBOARD_URL}/overview", headers=admin_headers)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
# =============================================================================
# === All dashboard endpoints with admin return 200 ===
# =============================================================================
async def test_admin_can_get_dashboard_accounts(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/accounts",
params={"channel_type": DEFAULT_CHANNEL_TYPE},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
async def test_admin_can_get_dashboard_messages(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/messages",
params={
"channel_type": DEFAULT_CHANNEL_TYPE,
"start_time": ISO_TIME_START,
"end_time": ISO_TIME_END,
},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
async def test_admin_can_get_dashboard_sessions(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/sessions",
params={"channel_type": DEFAULT_CHANNEL_TYPE},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
async def test_admin_can_get_dashboard_delivery(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/delivery",
params={
"channel_type": DEFAULT_CHANNEL_TYPE,
"start_time": ISO_TIME_START,
"end_time": ISO_TIME_END,
},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
async def test_admin_can_get_dashboard_realtime(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/realtime",
params={"channel_type": DEFAULT_CHANNEL_TYPE},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
# =============================================================================
# === /dashboard/todos — DSB-TODOS workbench todo counts ===
# =============================================================================
async def test_get_dashboard_todos_requires_auth(test_client: httpx.AsyncClient):
# Act
response = await test_client.get(f"{DASHBOARD_URL}/todos")
# Assert
assert response.status_code == 401, response.text
async def test_get_dashboard_todos_requires_admin(test_client: httpx.AsyncClient, standard_user):
# Act
response = await test_client.get(f"{DASHBOARD_URL}/todos", headers=standard_user["headers"])
# Assert
assert response.status_code == 403, response.text
async def test_admin_can_get_dashboard_todos(test_client: httpx.AsyncClient, admin_headers):
# Act
response = await test_client.get(
f"{DASHBOARD_URL}/todos",
params={"channel_type": DEFAULT_CHANNEL_TYPE},
headers=admin_headers,
)
# Assert
assert response.status_code == 200, response.text
payload = response.json()
assert payload["success"] is True
assert "data" in payload
# =============================================================================
# === /dashboard/realtime window_seconds validation ===
# =============================================================================
async def test_dashboard_realtime_rejects_window_below_min(test_client: httpx.AsyncClient, admin_headers):
# Act — window_seconds=9 violates ge=10
response = await test_client.get(
f"{DASHBOARD_URL}/realtime",
params={"window_seconds": 9},
headers=admin_headers,
)
# Assert
assert response.status_code == 422, response.text
async def test_dashboard_realtime_rejects_window_above_max(test_client: httpx.AsyncClient, admin_headers):
# Act — window_seconds=301 violates le=300
response = await test_client.get(
f"{DASHBOARD_URL}/realtime",
params={"window_seconds": 301},
headers=admin_headers,
)
# Assert
assert response.status_code == 422, response.text
# =============================================================================
# === /dashboard/delivery invalid time format ===
# =============================================================================
async def test_dashboard_delivery_rejects_invalid_start_time_format(test_client: httpx.AsyncClient, admin_headers):
# Act — "not-a-date" is not ISO 8601, parse_datetime raises ValidationError (400)
response = await test_client.get(
f"{DASHBOARD_URL}/delivery",
params={"start_time": "not-a-date"},
headers=admin_headers,
)
# Assert
assert response.status_code == 400, response.text