Files
张翔 0d0b4decc3 test(e2e): update e2e tests and auth tokens
- Update E2E test files with latest authentication tokens
- Improve test stability and error handling
- Update pytest configuration
- Enhance gateway direct test with settings integration
2026-04-23 16:35:57 +08:00

147 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""
User Journey 测试脚本
测试完整的用户流程
"""
import requests
import json
import time
API_BASE_URL = "http://localhost:8084"
def print_step(step_num, description):
print(f"\n{'='*60}")
print(f"步骤 {step_num}: {description}")
print('='*60)
def test_user_journey():
"""测试完整的用户旅程"""
# 步骤 1: 登录
print_step(1, "用户登录")
login_data = {
"username": "admin",
"password": "admin123"
}
response = requests.post(f"{API_BASE_URL}/api/auth/login", json=login_data)
print(f"状态码: {response.status_code}")
if response.status_code != 200:
print(f"❌ 登录失败: {response.text}")
return False
token_data = response.json()
token = token_data.get("token")
print(f"✅ 登录成功")
print(f"用户: {token_data.get('username')}")
print(f"Token: {token[:50]}...")
headers = {"Authorization": f"Bearer {token}"}
# 步骤 2: 获取用户列表
print_step(2, "获取用户列表")
response = requests.get(f"{API_BASE_URL}/api/users", headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
users = response.json()
print(f"✅ 获取用户列表成功")
print(f"用户数量: {len(users) if isinstance(users, list) else 'N/A'}")
else:
print(f"⚠️ 获取用户列表失败: {response.text[:200]}")
# 步骤 3: 创建新用户
print_step(3, "创建新用户")
timestamp = int(time.time() * 1000)
new_user = {
"username": f"testuser_{timestamp}",
"password": "Test@123",
"email": f"test_{timestamp}@example.com",
"phone": "13800138000",
"nickname": f"测试用户_{timestamp}",
"status": 1,
"roleId": 2
}
response = requests.post(f"{API_BASE_URL}/api/users", json=new_user, headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code in [200, 201]:
created_user = response.json()
user_id = created_user.get("id") or created_user.get("data", {}).get("id")
print(f"✅ 创建用户成功")
print(f"用户ID: {user_id}")
print(f"用户名: {new_user['username']}")
else:
print(f"⚠️ 创建用户失败: {response.text[:200]}")
user_id = None
# 步骤 4: 更新用户信息
if user_id:
print_step(4, "更新用户信息")
update_data = {
"nickname": f"更新后的用户_{timestamp}",
"email": f"updated_{timestamp}@example.com"
}
response = requests.put(f"{API_BASE_URL}/api/users/{user_id}", json=update_data, headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
print(f"✅ 更新用户成功")
else:
print(f"⚠️ 更新用户失败: {response.text[:200]}")
# 步骤 5: 获取角色列表
print_step(5, "获取角色列表")
response = requests.get(f"{API_BASE_URL}/api/roles", headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
roles = response.json()
print(f"✅ 获取角色列表成功")
print(f"角色数量: {len(roles) if isinstance(roles, list) else 'N/A'}")
else:
print(f"⚠️ 获取角色列表失败: {response.text[:200]}")
# 步骤 6: 获取菜单列表
print_step(6, "获取菜单列表")
response = requests.get(f"{API_BASE_URL}/api/menus", headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
menus = response.json()
print(f"✅ 获取菜单列表成功")
print(f"菜单数量: {len(menus) if isinstance(menus, list) else 'N/A'}")
else:
print(f"⚠️ 获取菜单列表失败: {response.text[:200]}")
# 步骤 7: 删除测试用户
if user_id:
print_step(7, "删除测试用户")
response = requests.delete(f"{API_BASE_URL}/api/users/{user_id}", headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code in [200, 204]:
print(f"✅ 删除用户成功")
else:
print(f"⚠️ 删除用户失败: {response.text[:200]}")
# 步骤 8: 登出
print_step(8, "用户登出")
response = requests.post(f"{API_BASE_URL}/api/auth/logout", headers=headers)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
print(f"✅ 登出成功")
else:
print(f"⚠️ 登出失败: {response.text[:200]}")
print(f"\n{'='*60}")
print("User Journey 测试完成")
print('='*60)
return True
if __name__ == "__main__":
test_user_journey()