67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
import requests
|
||
import json
|
||
|
||
def direct_api_test():
|
||
"""
|
||
直接测试管理员创建API(绕过登录验证)
|
||
"""
|
||
base_url = "http://127.0.0.1:5000"
|
||
|
||
print("=== 直接测试管理员创建API ===")
|
||
|
||
# 测试数据
|
||
admin_data = {
|
||
"username": "direct_api_test_user",
|
||
"email": "direct_api_test@example.com",
|
||
"password": "direct_api_test123",
|
||
"role": 0,
|
||
"status": 1
|
||
}
|
||
|
||
print(f"发送数据: {json.dumps(admin_data, ensure_ascii=False)}")
|
||
|
||
try:
|
||
# 直接发送POST请求到API端点(绕过登录验证)
|
||
response = requests.post(
|
||
f"{base_url}/api/v1/admins",
|
||
json=admin_data,
|
||
headers={
|
||
"Content-Type": "application/json"
|
||
}
|
||
)
|
||
|
||
print(f"状态码: {response.status_code}")
|
||
print(f"响应内容: {response.text}")
|
||
|
||
# 尝试解析JSON响应
|
||
try:
|
||
result = response.json()
|
||
print(f"解析结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
|
||
|
||
if response.status_code == 200 and result.get('success'):
|
||
print("✅ 管理员创建成功!")
|
||
elif response.status_code == 400:
|
||
print("❌ 400错误 - 请求数据有问题")
|
||
# 检查是否是我们修复的问题
|
||
if "密码不能为空" in result.get('message', ''):
|
||
print(" 这是原始的密码验证错误,应该已经修复了")
|
||
else:
|
||
print(f" 其他400错误: {result.get('message')}")
|
||
elif response.status_code == 401:
|
||
print("⚠️ 401错误 - 需要登录(这是预期的安全行为)")
|
||
elif response.status_code == 403:
|
||
print("❌ 403错误 - 权限不足")
|
||
else:
|
||
print(f"❌ 其他错误: {response.status_code}")
|
||
|
||
except Exception as e:
|
||
print(f"无法解析JSON响应: {e}")
|
||
|
||
except Exception as e:
|
||
print(f"API请求失败: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
direct_api_test() |