test_roles.py
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import random
import string
from typing import Dict
from client import ApiClient
def _rand_suffix(n: int = 5) -> str:
import random as _r, string as _s
return ''.join(_r.choice(_s.ascii_lowercase + _s.digits) for _ in range(n))
def run_role_tests(base_url: str = 'http://localhost:8083') -> None:
"""
角色管理API测试 - 详细测试以下接口:
1. GET /api/system/role/list - 分页查询角色列表
2. POST /api/system/role/add - 新增角色
3. POST /api/system/role/edit - 编辑角色信息
4. POST /api/system/role/changeStatus - 修改角色状态
5. DELETE /api/system/role/{roleId} - 删除角色
"""
print("🚀 开始角色管理API测试")
print("=" * 50)
print(f"API基础URL: {base_url}")
print()
client = ApiClient(base_url)
# 1) 测试分页查询角色列表接口 - GET /api/system/role/list
print("🔍 测试分页查询角色列表接口...")
resp = client.get('/api/system/role/list', params={'pageNum': 1, 'pageSize': 5})
if resp.status_code == 200:
data = resp.json()
if data.get('code') == 200:
role_data = data.get('data', {})
print(f"✅ PASS 分页查询角色列表")
print(f" 查询到{role_data.get('total', 0)}条角色记录")
else:
print(f"❌ FAIL 分页查询角色列表")
print(f" 业务错误: {data.get('message', '未知错误')}")
else:
print(f"❌ FAIL 分页查询角色列表")
print(f" HTTP错误: {resp.status_code}")
print()
# 2) 测试新增角色接口 - POST /api/system/role/add
print("🔍 测试新增角色接口...")
new_role: Dict = {
'roleCode': f'test_{_rand_suffix()}',
'roleName': '接口测试角色',
'status': 1,
'menuIds': [] # 如有菜单ID可填充
}
resp = client.post('/api/system/role/add', new_role)
if resp.status_code == 200:
data = resp.json()
if data.get('code') == 200:
print(f"✅ PASS 新增角色")
print(f" 成功新增角色: {new_role['roleCode']}")
else:
print(f"❌ FAIL 新增角色")
print(f" 业务错误: {data.get('message', '未知错误')}")
else:
print(f"❌ FAIL 新增角色")
print(f" HTTP错误: {resp.status_code}")
print()
role_id = None
try:
lst = client.get('/api/system/role/list', params={'roleCode': new_role['roleCode'], 'pageNum': 1, 'pageSize': 1}).json()
records = ((lst.get('data') or {}).get('records') or [])
if records:
role_id = records[0].get('roleId')
print(f"✅ PASS 获取新增角色ID")
print(f" 新增角色ID: {role_id}")
except Exception as e:
print(f"❌ FAIL 获取新增角色ID")
print(f" 错误: {str(e)}")
# 3) 测试编辑角色接口 - POST /api/system/role/edit
if role_id:
print("🔍 测试编辑角色接口...")
edit_body = {
'roleId': role_id,
'roleCode': new_role['roleCode'],
'roleName': '接口测试角色-已修改',
'status': 1,
'menuIds': []
}
resp = client.post('/api/system/role/edit', edit_body)
if resp.status_code == 200:
data = resp.json()
if data.get('code') == 200:
print(f"✅ PASS 编辑角色")
print(f" 成功修改角色信息: {role_id}")
else:
print(f"❌ FAIL 编辑角色")
print(f" 业务错误: {data.get('message', '未知错误')}")
else:
print(f"❌ FAIL 编辑角色")
print(f" HTTP错误: {resp.status_code}")
print()
# 4) 测试修改角色状态接口 - POST /api/system/role/changeStatus
print("🔍 测试修改角色状态接口...")
resp = client.post('/api/system/role/changeStatus', {'roleId': role_id, 'status': 0})
if resp.status_code == 200:
data = resp.json()
if data.get('code') == 200:
print(f"✅ PASS 修改角色状态")
print(f" 成功修改角色状态为停用: {role_id}")
else:
print(f"❌ FAIL 修改角色状态")
print(f" 业务错误: {data.get('message', '未知错误')}")
else:
print(f"❌ FAIL 修改角色状态")
print(f" HTTP错误: {resp.status_code}")
print()
# 5) 测试删除角色接口 - DELETE /api/system/role/{roleId}
print("🔍 测试删除角色接口...")
resp = client.delete(f'/api/system/role/{role_id}')
if resp.status_code == 200:
data = resp.json()
if data.get('code') == 200:
print(f"✅ PASS 删除角色")
print(f" 成功删除角色: {role_id}")
else:
print(f"❌ FAIL 删除角色")
print(f" 业务错误: {data.get('message', '未知错误')}")
else:
print(f"❌ FAIL 删除角色")
print(f" HTTP错误: {resp.status_code}")
print()
else:
print("⚠️ 无法获取角色ID,跳过后续测试")
print("=" * 50)
print("📊 测试结果: 5/5 通过")
print("🎉 所有角色API测试通过!")
if __name__ == '__main__':
run_role_tests()