forked from xiaohei/taiji-AI-PAD
更新接口
This commit is contained in:
@@ -0,0 +1,330 @@
|
||||
"""
|
||||
API端点测试用例
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
|
||||
class TestUserAPIs:
|
||||
"""用户端API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_dashboard_stats(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试获取用户仪表板统计"""
|
||||
token = auth_tokens.get("user")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/user/dashboard/stats", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_billing_records(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试获取用户计费记录"""
|
||||
token = auth_tokens.get("user")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/user/billing/records", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_user_cannot_access_admin_apis(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试普通用户无法访问管理员API"""
|
||||
token = auth_tokens.get("user")
|
||||
headers = auth_headers(token)
|
||||
|
||||
admin_endpoints = [
|
||||
"/api/admin/dashboard/stats",
|
||||
"/api/admin/channels",
|
||||
"/api/admin/tenants",
|
||||
"/api/admin/billing/recharge",
|
||||
]
|
||||
|
||||
for endpoint in admin_endpoints:
|
||||
response = await client.get(endpoint, headers=headers)
|
||||
assert response.status_code == 403, \
|
||||
f"普通用户不应该能访问管理员端点: {endpoint}"
|
||||
|
||||
|
||||
class TestChannelAPIs:
|
||||
"""渠道端API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_channel_dashboard_stats(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试获取渠道仪表板统计"""
|
||||
token = auth_tokens.get("channel_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/channel/dashboard/stats", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_channel_tenants(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试获取渠道租户列表"""
|
||||
token = auth_tokens.get("channel_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/channel/tenants", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
|
||||
class TestAdminAPIs:
|
||||
"""管理员API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_super_admin_get_channels(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试超级管理员获取渠道列表"""
|
||||
token = auth_tokens.get("super_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/channels", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_get_dashboard_stats(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试管理员获取仪表板统计"""
|
||||
token = auth_tokens.get("admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/dashboard/stats", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
|
||||
class TestBillingAdminAPIs:
|
||||
"""计费管理员API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_billing_admin_view_billing(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试计费管理员查看计费记录"""
|
||||
token = auth_tokens.get("billing_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/billing/records", headers=headers)
|
||||
|
||||
# 计费管理员应该能查看计费记录
|
||||
assert response.status_code != 403
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_billing_admin_manage_billing(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers,
|
||||
test_users
|
||||
):
|
||||
"""测试计费管理员执行充值操作"""
|
||||
token = auth_tokens.get("billing_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
user = test_users["user"]
|
||||
|
||||
response = await client.post(
|
||||
"/api/admin/billing/recharge",
|
||||
json={
|
||||
"user_id": str(user.id),
|
||||
"amount": 100.0,
|
||||
"payment_method": "alipay"
|
||||
},
|
||||
headers=headers
|
||||
)
|
||||
|
||||
# 计费管理员应该能执行充值
|
||||
assert response.status_code != 403
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_billing_admin_cannot_manage_tenants(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试计费管理员无法管理租户"""
|
||||
token = auth_tokens.get("billing_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/tenants", headers=headers)
|
||||
|
||||
# 计费管理员不应该能管理租户
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
class TestOperationsAdminAPIs:
|
||||
"""运营管理员API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_operations_admin_manage_tenants(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试运营管理员管理租户"""
|
||||
token = auth_tokens.get("operations_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/tenants", headers=headers)
|
||||
|
||||
# 运营管理员应该能管理租户
|
||||
assert response.status_code != 403
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_operations_admin_manage_resources(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试运营管理员管理资源"""
|
||||
token = auth_tokens.get("operations_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/admin/resources", headers=headers)
|
||||
|
||||
# 运营管理员应该能管理资源
|
||||
assert response.status_code != 403
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_operations_admin_cannot_recharge(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers,
|
||||
test_users
|
||||
):
|
||||
"""测试运营管理员无法执行充值"""
|
||||
token = auth_tokens.get("operations_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
user = test_users["user"]
|
||||
|
||||
response = await client.post(
|
||||
"/api/admin/billing/recharge",
|
||||
json={
|
||||
"user_id": str(user.id),
|
||||
"amount": 100.0,
|
||||
"payment_method": "alipay"
|
||||
},
|
||||
headers=headers
|
||||
)
|
||||
|
||||
# 运营管理员不应该能执行充值
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
class TestProviderAPIs:
|
||||
"""供应商API测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_provider_admin_get_models(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试供应商管理员获取模型列表"""
|
||||
token = auth_tokens.get("provider_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = await client.get("/api/provider/models", headers=headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "data" in data
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_provider_admin_cannot_access_admin_apis(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
auth_tokens,
|
||||
auth_headers
|
||||
):
|
||||
"""测试供应商管理员无法访问管理员API"""
|
||||
token = auth_tokens.get("provider_admin")
|
||||
headers = auth_headers(token)
|
||||
|
||||
admin_endpoints = [
|
||||
"/api/admin/dashboard/stats",
|
||||
"/api/admin/channels",
|
||||
"/api/admin/tenants",
|
||||
]
|
||||
|
||||
for endpoint in admin_endpoints:
|
||||
response = await client.get(endpoint, headers=headers)
|
||||
assert response.status_code == 403, \
|
||||
f"供应商管理员不应该能访问: {endpoint}"
|
||||
|
||||
|
||||
class TestHealthCheck:
|
||||
"""健康检查测试"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check(self, client: AsyncClient):
|
||||
"""测试健康检查端点"""
|
||||
response = await client.get("/health")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "status" in data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
|
||||
Reference in New Issue
Block a user