164 lines
5.4 KiB
Python
164 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
处分记录选项API测试脚本
|
||
测试 /xskh/disciplineRecord/options 接口是否正常工作
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
import sys
|
||
|
||
# 测试配置
|
||
BASE_URL = "http://localhost:8080"
|
||
DISCIPLINE_RECORD_OPTIONS_URL = f"{BASE_URL}/xskh/disciplineRecord/options"
|
||
|
||
# 测试Token (请替换为真实的Token)
|
||
TOKEN = "your_actual_token_here"
|
||
|
||
# 请求头
|
||
HEADERS = {
|
||
"Authorization": f"Bearer {TOKEN}",
|
||
"Content-Type": "application/json"
|
||
}
|
||
|
||
def test_discipline_record_options():
|
||
"""测试获取处分记录选项"""
|
||
print("=" * 60)
|
||
print("🧪 测试处分记录选项API")
|
||
print("=" * 60)
|
||
|
||
try:
|
||
print(f"📍 请求地址: {DISCIPLINE_RECORD_OPTIONS_URL}")
|
||
print(f"📋 请求方法: GET")
|
||
print(f"🔑 Token: {TOKEN[:20]}..." if TOKEN != "your_actual_token_here" else "🔑 Token: 未设置")
|
||
|
||
# 发送请求
|
||
response = requests.get(
|
||
DISCIPLINE_RECORD_OPTIONS_URL,
|
||
headers=HEADERS,
|
||
timeout=10
|
||
)
|
||
|
||
print(f"📊 响应状态码: {response.status_code}")
|
||
print(f"📄 响应头: {dict(response.headers)}")
|
||
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
print("✅ 请求成功!")
|
||
print("📈 响应数据:")
|
||
print(json.dumps(data, indent=2, ensure_ascii=False))
|
||
|
||
# 验证响应结构
|
||
if data.get("code") == 200:
|
||
options_data = data.get("data", {})
|
||
expected_keys = [
|
||
"violationTypes",
|
||
"punishmentTypes",
|
||
"recordStatuses",
|
||
"punishmentStatuses",
|
||
"appealStatuses",
|
||
"approvalStatuses"
|
||
]
|
||
|
||
print("\n🔍 数据结构验证:")
|
||
for key in expected_keys:
|
||
if key in options_data:
|
||
count = len(options_data[key])
|
||
print(f" ✅ {key}: {count} 个选项")
|
||
else:
|
||
print(f" ❌ {key}: 缺失")
|
||
|
||
print("\n🎯 修复验证: options接口已正常工作!")
|
||
return True
|
||
else:
|
||
print(f"❌ 业务逻辑错误: {data.get('msg', '未知错误')}")
|
||
return False
|
||
|
||
elif response.status_code == 401:
|
||
print("❌ 认证失败: 请检查Token是否正确")
|
||
print(f"📄 响应内容: {response.text}")
|
||
return False
|
||
|
||
elif response.status_code == 403:
|
||
print("❌ 权限不足: 请检查用户权限")
|
||
print(f"📄 响应内容: {response.text}")
|
||
return False
|
||
|
||
elif response.status_code == 404:
|
||
print("❌ 接口未找到: 请检查接口路径是否正确")
|
||
print(f"📄 响应内容: {response.text}")
|
||
return False
|
||
|
||
else:
|
||
print(f"❌ 请求失败: HTTP {response.status_code}")
|
||
print(f"📄 响应内容: {response.text}")
|
||
return False
|
||
|
||
except requests.exceptions.ConnectionError:
|
||
print("❌ 连接失败: 请检查后端服务是否启动")
|
||
return False
|
||
except requests.exceptions.Timeout:
|
||
print("❌ 请求超时: 请检查网络连接")
|
||
return False
|
||
except Exception as e:
|
||
print(f"❌ 未知错误: {e}")
|
||
return False
|
||
|
||
def test_without_token():
|
||
"""测试不带Token的请求"""
|
||
print("\n" + "=" * 60)
|
||
print("🧪 测试无Token访问")
|
||
print("=" * 60)
|
||
|
||
try:
|
||
response = requests.get(DISCIPLINE_RECORD_OPTIONS_URL, timeout=10)
|
||
print(f"📊 响应状态码: {response.status_code}")
|
||
|
||
if response.status_code == 401:
|
||
print("✅ 正确: 无Token访问被拒绝")
|
||
return True
|
||
else:
|
||
print(f"❌ 异常: 应该返回401,但返回了{response.status_code}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ 请求异常: {e}")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
print("🚀 处分记录选项API测试开始")
|
||
|
||
# 检查Token配置
|
||
if TOKEN == "your_actual_token_here":
|
||
print("⚠️ 警告: 未配置真实Token,将进行部分测试")
|
||
|
||
# 执行测试
|
||
test1_result = test_without_token() # 先测试无Token访问
|
||
|
||
if TOKEN != "your_actual_token_here":
|
||
test2_result = test_discipline_record_options() # 再测试有Token访问
|
||
else:
|
||
print("\n⚠️ 跳过Token测试(未配置Token)")
|
||
test2_result = None
|
||
|
||
# 总结
|
||
print("\n" + "=" * 60)
|
||
print("📋 测试总结")
|
||
print("=" * 60)
|
||
print(f"无Token测试: {'✅ 通过' if test1_result else '❌ 失败'}")
|
||
if test2_result is not None:
|
||
print(f"有Token测试: {'✅ 通过' if test2_result else '❌ 失败'}")
|
||
else:
|
||
print("有Token测试: ⚠️ 未执行(需要配置Token)")
|
||
|
||
print("\n💡 使用说明:")
|
||
print("1. 确保后端服务正在运行 (http://localhost:8080)")
|
||
print("2. 将脚本中的 TOKEN 替换为真实的认证Token")
|
||
print("3. 重新运行脚本进行完整测试")
|
||
|
||
# 退出码
|
||
if test1_result and (test2_result is None or test2_result):
|
||
sys.exit(0)
|
||
else:
|
||
sys.exit(1) |