Files
taiji-AI-PAD/test_allocate_platform_agent.py
T
2026-01-05 13:03:15 +00:00

93 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""测试分配平台 Agent 给租户的接口"""
import requests
import json
BASE_URL = "http://localhost:8002"
def test_allocate_platform_agent():
# 1. 渠道管理员登录
print("=" * 50)
print("1. 渠道管理员登录")
login_resp = requests.post(
f"{BASE_URL}/api/channel/auth/login",
json={"email": "66@66.com", "password": "66"}
)
print(f"状态码: {login_resp.status_code}")
print(f"响应: {json.dumps(login_resp.json(), indent=2, ensure_ascii=False)}")
if login_resp.status_code != 200:
print("登录失败!")
return
token = login_resp.json().get("token")
headers = {"Authorization": f"Bearer {token}"}
# 解析 JWT token 查看内容
import base64
parts = token.split(".")
if len(parts) >= 2:
payload = parts[1]
# 添加填充
payload += "=" * (4 - len(payload) % 4)
decoded = base64.urlsafe_b64decode(payload)
print(f"\nJWT Payload: {decoded.decode('utf-8')}")
# 2. 获取渠道下的租户列表
print("\n" + "=" * 50)
print("2. 获取渠道下的租户列表")
tenants_resp = requests.get(
f"{BASE_URL}/api/channel/tenants",
headers=headers
)
print(f"状态码: {tenants_resp.status_code}")
print(f"响应: {json.dumps(tenants_resp.json(), indent=2, ensure_ascii=False)}")
# 3. 查看可用的平台 Agent 模板
print("\n" + "=" * 50)
print("3. 查看可用的平台 Agent 模板")
templates_resp = requests.get(
f"{BASE_URL}/api/channel/available-platform-agents",
headers=headers
)
print(f"状态码: {templates_resp.status_code}")
print(f"响应: {json.dumps(templates_resp.json(), indent=2, ensure_ascii=False)}")
# 4. 查看渠道的平台 Agent 配额
print("\n" + "=" * 50)
print("4. 查看渠道的平台 Agent 配额")
quotas_resp = requests.get(
f"{BASE_URL}/api/channel/platform-agents",
headers=headers
)
print(f"状态码: {quotas_resp.status_code}")
print(f"响应: {json.dumps(quotas_resp.json(), indent=2, ensure_ascii=False)}")
# 5. 如果有租户,尝试分配平台 Agent
if tenants_resp.status_code == 200:
tenants_data = tenants_resp.json()
tenants = tenants_data.get("data", {}).get("tenants", [])
if tenants:
tenant_id = tenants[0]["id"]
print("\n" + "=" * 50)
print(f"5. 分配平台 Agent 给租户 {tenant_id}")
allocate_resp = requests.post(
f"{BASE_URL}/api/channel/tenants/{tenant_id}/platform-agents",
headers=headers,
json={
"templateName": "gpt-assistant",
"podQuota": 2
}
)
print(f"状态码: {allocate_resp.status_code}")
print(f"响应: {json.dumps(allocate_resp.json(), indent=2, ensure_ascii=False)}")
else:
print("\n没有租户,跳过分配测试")
print("请先创建一个租户")
if __name__ == "__main__":
test_allocate_platform_agent()