forked from xiaohei/taiji-AI-PAD
更新管理员
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试账号注册和登录功能
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
from typing import Dict, Any
|
||||
|
||||
BASE_URL = "http://localhost:8002"
|
||||
|
||||
def print_section(title: str):
|
||||
"""打印分节标题"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f" {title}")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
def print_result(title: str, data: Any):
|
||||
"""打印结果"""
|
||||
print(f"✓ {title}")
|
||||
print(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
print()
|
||||
|
||||
def test_health():
|
||||
"""测试健康检查"""
|
||||
print_section("1. 健康检查")
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/health")
|
||||
response.raise_for_status()
|
||||
print_result("Health Check", response.json())
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"✗ 健康检查失败: {e}")
|
||||
return False
|
||||
|
||||
def test_admin_login():
|
||||
"""测试管理员登录(使用frontend-integration端点)"""
|
||||
print_section("2. 测试管理员登录(自动创建)")
|
||||
|
||||
# 使用frontend-integration的admin login端点,会自动创建用户
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/admin/auth/login",
|
||||
json={
|
||||
"email": "admin@test.com",
|
||||
"password": "admin123"
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print_result("管理员登录成功", result)
|
||||
return result.get("token")
|
||||
except Exception as e:
|
||||
print(f"✗ 管理员登录失败: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text}")
|
||||
return None
|
||||
|
||||
def test_channel_login():
|
||||
"""测试渠道管理员登录"""
|
||||
print_section("3. 测试渠道管理员登录(自动创建)")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/channel/auth/login",
|
||||
json={
|
||||
"email": "channel@test.com",
|
||||
"password": "channel123"
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print_result("渠道管理员登录成功", result)
|
||||
return result.get("token")
|
||||
except Exception as e:
|
||||
print(f"✗ 渠道管理员登录失败: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text}")
|
||||
return None
|
||||
|
||||
def test_provider_login():
|
||||
"""测试供应商登录"""
|
||||
print_section("4. 测试供应商登录(自动创建)")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/providers/auth/login",
|
||||
json={
|
||||
"email": "provider@test.com",
|
||||
"password": "provider123"
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print_result("供应商登录成功", result)
|
||||
return result.get("token")
|
||||
except Exception as e:
|
||||
print(f"✗ 供应商登录失败: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text}")
|
||||
return None
|
||||
|
||||
def test_user_login():
|
||||
"""测试普通用户登录"""
|
||||
print_section("5. 测试普通用户登录")
|
||||
|
||||
# 先尝试使用标准auth端点登录
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/auth/login",
|
||||
json={
|
||||
"email": "user@test.com",
|
||||
"password": "user123",
|
||||
"role": "user"
|
||||
}
|
||||
)
|
||||
|
||||
if response.status_code == 401:
|
||||
print("用户不存在,需要先创建")
|
||||
return None
|
||||
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print_result("用户登录成功", result)
|
||||
return result.get("data", {}).get("token")
|
||||
except Exception as e:
|
||||
print(f"✗ 用户登录失败: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text}")
|
||||
return None
|
||||
|
||||
def test_authenticated_request(token: str, endpoint: str, title: str):
|
||||
"""测试需要认证的请求"""
|
||||
print_section(f"测试认证请求: {title}")
|
||||
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{BASE_URL}{endpoint}",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print_result(f"{title} - 成功", result)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"✗ {title} 失败: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""主测试流程"""
|
||||
print("\n" + "="*60)
|
||||
print(" Taiji AI-PAD 账号注册和登录功能测试")
|
||||
print("="*60)
|
||||
|
||||
# 1. 健康检查
|
||||
if not test_health():
|
||||
print("\n服务未就绪,退出测试")
|
||||
return
|
||||
|
||||
# 2. 测试管理员登录
|
||||
admin_token = test_admin_login()
|
||||
if admin_token:
|
||||
test_authenticated_request(
|
||||
admin_token,
|
||||
"/api/admin/dashboard/stats",
|
||||
"管理员仪表板"
|
||||
)
|
||||
|
||||
# 3. 测试渠道管理员登录
|
||||
channel_token = test_channel_login()
|
||||
if channel_token:
|
||||
test_authenticated_request(
|
||||
channel_token,
|
||||
"/api/channel/dashboard/stats",
|
||||
"渠道仪表板"
|
||||
)
|
||||
|
||||
# 4. 测试供应商登录
|
||||
provider_token = test_provider_login()
|
||||
if provider_token:
|
||||
test_authenticated_request(
|
||||
provider_token,
|
||||
"/api/providers/models",
|
||||
"供应商模型列表"
|
||||
)
|
||||
|
||||
# 5. 测试普通用户登录
|
||||
user_token = test_user_login()
|
||||
|
||||
# 总结
|
||||
print_section("测试总结")
|
||||
results = {
|
||||
"管理员登录": "✓" if admin_token else "✗",
|
||||
"渠道管理员登录": "✓" if channel_token else "✗",
|
||||
"供应商登录": "✓" if provider_token else "✗",
|
||||
"普通用户登录": "✓" if user_token else "✗ (需要先创建)",
|
||||
}
|
||||
|
||||
for test_name, status in results.items():
|
||||
print(f"{status} {test_name}")
|
||||
|
||||
print("\n" + "="*60)
|
||||
print(" 测试完成")
|
||||
print("="*60 + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
全面测试账号注册和登录功能
|
||||
包括标准auth端点和frontend-integration端点
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
BASE_URL = "http://localhost:8002"
|
||||
|
||||
def print_section(title: str):
|
||||
"""打印分节标题"""
|
||||
print(f"\n{'='*70}")
|
||||
print(f" {title}")
|
||||
print(f"{'='*70}\n")
|
||||
|
||||
def print_result(title: str, success: bool, data: Any = None):
|
||||
"""打印结果"""
|
||||
icon = "✓" if success else "✗"
|
||||
print(f"{icon} {title}")
|
||||
if data:
|
||||
print(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
print()
|
||||
|
||||
def test_standard_auth_login(email: str, password: str, role: str) -> Optional[str]:
|
||||
"""测试标准auth端点登录"""
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/auth/login",
|
||||
json={
|
||||
"email": email,
|
||||
"password": password,
|
||||
"role": role
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
token = result.get("data", {}).get("token")
|
||||
print_result(f"标准登录 ({role}): {email}", True, result.get("data"))
|
||||
return token
|
||||
except Exception as e:
|
||||
print_result(f"标准登录 ({role}): {email}", False)
|
||||
print(f" 错误: {e}")
|
||||
if hasattr(e, 'response') and e.response:
|
||||
print(f" 响应: {e.response.text[:200]}")
|
||||
return None
|
||||
|
||||
def main():
|
||||
"""主测试流程"""
|
||||
print("\n" + "="*70)
|
||||
print(" Taiji AI-PAD 全面账号认证测试")
|
||||
print("="*70)
|
||||
|
||||
# 测试结果统计
|
||||
results = {}
|
||||
|
||||
# 1. 测试健康检查
|
||||
print_section("1. 健康检查")
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/health")
|
||||
response.raise_for_status()
|
||||
print_result("Health Check", True, response.json())
|
||||
except Exception as e:
|
||||
print_result("Health Check", False)
|
||||
print("服务未就绪,退出测试")
|
||||
return
|
||||
|
||||
# 2. 通过frontend-integration端点创建账号
|
||||
print_section("2. 通过Frontend Integration端点创建账号")
|
||||
|
||||
# 创建管理员
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/admin/auth/login",
|
||||
json={"email": "admin@test.com", "password": "admin123"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
admin_token = response.json().get("token")
|
||||
results["管理员创建"] = True
|
||||
print_result("管理员账号创建", True, {"email": "admin@test.com"})
|
||||
except Exception as e:
|
||||
results["管理员创建"] = False
|
||||
print_result("管理员账号创建", False)
|
||||
|
||||
# 创建渠道管理员
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/channel/auth/login",
|
||||
json={"email": "channel@test.com", "password": "channel123"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
results["渠道管理员创建"] = True
|
||||
print_result("渠道管理员账号创建", True, {"email": "channel@test.com"})
|
||||
except Exception as e:
|
||||
results["渠道管理员创建"] = False
|
||||
print_result("渠道管理员账号创建", False)
|
||||
|
||||
# 创建供应商
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/providers/auth/login",
|
||||
json={"email": "provider@test.com", "password": "provider123"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
results["供应商创建"] = True
|
||||
print_result("供应商账号创建", True, {"email": "provider@test.com"})
|
||||
except Exception as e:
|
||||
results["供应商创建"] = False
|
||||
print_result("供应商账号创建", False)
|
||||
|
||||
# 3. 测试标准auth端点登录
|
||||
print_section("3. 测试标准Auth端点登录")
|
||||
|
||||
# 测试管理员登录
|
||||
admin_token = test_standard_auth_login("admin@test.com", "admin123", "admin")
|
||||
results["管理员登录"] = admin_token is not None
|
||||
|
||||
# 测试超级管理员登录(使用同一账号,但role不同)
|
||||
# super_admin_token = test_standard_auth_login("admin@test.com", "admin123", "super_admin")
|
||||
# results["超级管理员登录"] = super_admin_token is not None
|
||||
|
||||
# 测试渠道登录
|
||||
channel_token = test_standard_auth_login("channel@test.com", "channel123", "channel")
|
||||
results["渠道管理员登录"] = channel_token is not None
|
||||
|
||||
# 测试供应商登录
|
||||
provider_token = test_standard_auth_login("provider@test.com", "provider123", "provider")
|
||||
results["供应商登录"] = provider_token is not None
|
||||
|
||||
# 4. 测试错误情况
|
||||
print_section("4. 测试错误情况")
|
||||
|
||||
# 错误的密码
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/auth/login",
|
||||
json={"email": "admin@test.com", "password": "wrongpassword", "role": "admin"}
|
||||
)
|
||||
if response.status_code == 401:
|
||||
print_result("错误密码拒绝", True, {"status": "正确拒绝"})
|
||||
results["错误密码拒绝"] = True
|
||||
else:
|
||||
print_result("错误密码拒绝", False)
|
||||
results["错误密码拒绝"] = False
|
||||
except Exception as e:
|
||||
print_result("错误密码拒绝", False)
|
||||
results["错误密码拒绝"] = False
|
||||
|
||||
# 不存在的用户
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/auth/login",
|
||||
json={"email": "nonexistent@test.com", "password": "test123", "role": "user"}
|
||||
)
|
||||
if response.status_code == 401:
|
||||
print_result("不存在用户拒绝", True, {"status": "正确拒绝"})
|
||||
results["不存在用户拒绝"] = True
|
||||
else:
|
||||
print_result("不存在用户拒绝", False)
|
||||
results["不存在用户拒绝"] = False
|
||||
except Exception as e:
|
||||
print_result("不存在用户拒绝", False)
|
||||
results["不存在用户拒绝"] = False
|
||||
|
||||
# 5. 测试认证后的API访问
|
||||
print_section("5. 测试认证后的API访问")
|
||||
|
||||
if admin_token:
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{BASE_URL}/api/admin/dashboard/stats",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
print_result("管理员API访问", True, response.json())
|
||||
results["管理员API访问"] = True
|
||||
except Exception as e:
|
||||
print_result("管理员API访问", False)
|
||||
results["管理员API访问"] = False
|
||||
|
||||
# 6. 测试登出
|
||||
print_section("6. 测试登出功能")
|
||||
|
||||
if admin_token:
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/api/auth/logout",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
response.raise_for_status()
|
||||
print_result("登出功能", True, response.json())
|
||||
results["登出功能"] = True
|
||||
except Exception as e:
|
||||
print_result("登出功能", False)
|
||||
results["登出功能"] = False
|
||||
|
||||
# 总结
|
||||
print_section("测试总结")
|
||||
|
||||
total = len(results)
|
||||
passed = sum(1 for v in results.values() if v)
|
||||
|
||||
print(f"总测试数: {total}")
|
||||
print(f"通过: {passed}")
|
||||
print(f"失败: {total - passed}")
|
||||
print(f"通过率: {passed/total*100:.1f}%\n")
|
||||
|
||||
for test_name, success in results.items():
|
||||
icon = "✓" if success else "✗"
|
||||
print(f"{icon} {test_name}")
|
||||
|
||||
print("\n" + "="*70)
|
||||
print(" 测试完成")
|
||||
print("="*70 + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user