test_users.py
2.19 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
import random
import string
from typing import Dict
from client import ApiClient
def _rand_suffix(n: int = 6) -> str:
return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(n))
def run_user_tests(base_url: str = 'http://localhost:8083') -> None:
client = ApiClient(base_url)
# 1) 列表查询
resp = client.get('/api/system/user/list', params={'pageNum': 1, 'pageSize': 5})
print('用户列表 status=', resp.status_code)
print(resp.text)
# 2) 新增用户
mock_user: Dict = {
'username': f'test_{_rand_suffix()}',
'password': '123456',
'realName': '接口测试用户',
'phone': '18221643976',
'email': f'test_{_rand_suffix()}@example.com',
'status': 1,
'roleIds': [1] # 根据系统内置角色调整
}
resp = client.post('/api/system/user/add', mock_user)
print('新增用户 status=', resp.status_code)
print(resp.text)
user_id = None
try:
data = resp.json()
if data.get('code') == 200:
# 简化:再拉一次列表找刚才的用户
lst = client.get('/api/system/user/list', params={'username': mock_user['username'], 'pageNum': 1, 'pageSize': 1}).json()
records = ((lst.get('data') or {}).get('records') or [])
if records:
user_id = records[0].get('userId')
except Exception:
pass
# 3) 编辑用户(若拿到了ID)
if user_id:
edit_body = {
'userId': user_id,
'username': mock_user['username'],
'realName': '接口测试用户-已修改',
'phone': mock_user['phone'],
'email': mock_user['email'],
'status': 1,
'roleIds': [1]
}
resp = client.post('/api/system/user/edit', edit_body)
print('编辑用户 status=', resp.status_code)
print(resp.text)
# 4) 重置密码
resp = client.put('/api/system/user/resetPwd', {'userId': user_id, 'newPassword': 'newpass123'})
print('重置密码 status=', resp.status_code)
print(resp.text)
# 5) 停用用户
resp = client.post('/api/system/user/changeStatus', {'userId': user_id, 'status': 0})
print('修改用户状态 status=', resp.status_code)
print(resp.text)
# 6) 删除用户
resp = client.delete(f'/api/system/user/{user_id}')
print('删除用户 status=', resp.status_code)
print(resp.text)
if __name__ == '__main__':
run_user_tests()