ForcePilot/backend/test/integration/api/test_channels_router.py

79 lines
3.1 KiB
Python
Raw Normal View History

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