test_all_salary_calculation_protection.py
7.38 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试所有薪酬计算服务的保护逻辑
验证已锁定和已确认的记录不会被覆盖
"""
import requests
import time
# API配置
BASE_URL = "http://localhost:2011"
LOGIN_URL = f"{BASE_URL}/api/oauth/Login"
# 所有薪酬计算接口
SALARY_SERVICES = [
{
"name": "健康师工资",
"endpoint": "/api/Extend/LqSalary/calculate/health-coach",
"description": "健康师薪酬服务"
},
{
"name": "店长工资",
"endpoint": "/api/Extend/LqStoreManagerSalary/calculate",
"description": "店长薪酬服务"
},
{
"name": "主任工资",
"endpoint": "/api/Extend/LqDirectorSalary/calculate",
"description": "主任薪酬服务"
},
{
"name": "店助工资",
"endpoint": "/api/Extend/LqAssistantSalary/calculate",
"description": "店助薪酬服务"
},
{
"name": "科技部老师工资",
"endpoint": "/api/Extend/LqTechTeacherSalary/calculate",
"description": "科技部老师薪酬服务"
},
{
"name": "大项目部老师工资",
"endpoint": "/api/Extend/LqMajorProjectTeacherSalary/calculate",
"description": "大项目部老师薪酬服务"
},
{
"name": "大项目主管工资",
"endpoint": "/api/Extend/LqMajorProjectDirectorSalary/calculate",
"description": "大项目主管薪酬服务"
},
{
"name": "科技部总经理工资",
"endpoint": "/api/Extend/LqTechGeneralManagerSalary/calculate",
"description": "科技部总经理薪酬服务"
},
{
"name": "事业部总经理工资",
"endpoint": "/api/Extend/LqBusinessUnitManagerSalary/calculate",
"description": "事业部总经理薪酬服务"
}
]
def get_token():
"""获取登录token"""
data = {
"account": "admin",
"password": "e10adc3949ba59abbe56e057f20f883e" # 123456的MD5
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
try:
response = requests.post(LOGIN_URL, data=data, headers=headers, timeout=10)
if response.status_code == 200:
result = response.json()
if result.get("code") == 200 and result.get("data"):
token = result["data"].get("token")
return token
print(f"❌ 获取token失败: {response.status_code}")
print(f"响应: {response.text[:200]}")
except Exception as e:
print(f"❌ 请求异常: {str(e)}")
return None
def test_salary_calculation(service_info, year, month, token):
"""测试单个薪酬计算接口"""
name = service_info["name"]
endpoint = service_info["endpoint"]
url = f"{BASE_URL}{endpoint}"
headers = {
"Authorization": token,
"Content-Type": "application/json"
}
params = {
"year": year,
"month": month
}
print(f"\n{'='*70}")
print(f"测试: {name}")
print(f"{'='*70}")
print(f"接口: {endpoint}")
print(f"参数: year={year}, month={month}")
try:
start_time = time.time()
response = requests.post(url, json=params, headers=headers, timeout=60)
elapsed_time = time.time() - start_time
print(f"响应时间: {elapsed_time:.2f}秒")
print(f"状态码: {response.status_code}")
if response.status_code == 200:
try:
result = response.json()
if result.get("code") == 200:
print(f"✅ {name} - 计算成功")
msg = result.get("msg", "")
if msg:
print(f" 消息: {msg}")
return True
else:
print(f"❌ {name} - 计算失败")
print(f" 错误代码: {result.get('code')}")
print(f" 错误信息: {result.get('msg', '未知错误')}")
return False
except:
# 可能是字符串响应
text = response.text[:200]
if "操作成功" in text or "成功" in text:
print(f"✅ {name} - 计算成功")
print(f" 响应: {text}")
return True
else:
print(f"⚠️ {name} - 响应格式异常")
print(f" 响应: {text}")
return False
else:
print(f"❌ {name} - HTTP错误: {response.status_code}")
print(f" 响应: {response.text[:200]}")
return False
except requests.exceptions.Timeout:
print(f"❌ {name} - 请求超时(超过60秒)")
return False
except Exception as e:
print(f"❌ {name} - 请求异常: {str(e)}")
return False
def main():
"""主测试函数"""
print("="*70)
print("薪酬计算保护逻辑测试")
print("="*70)
print("\n测试目标:")
print("1. 验证所有9个薪酬计算接口是否正常工作")
print("2. 确认已锁定或已确认的记录不会被覆盖")
print("3. 检查日志输出是否正确")
print("\n" + "="*70)
# 获取token
print("\n1. 获取认证Token...")
token = get_token()
if not token:
print("❌ 无法获取Token,请检查:")
print(" - 后端服务是否运行")
print(" - 服务地址是否正确(默认:http://localhost:2011)")
print(" - 登录账号密码是否正确")
return
print("✅ Token获取成功")
# 测试参数
year = 2025
month = 12
print(f"\n2. 开始测试所有薪酬计算接口...")
print(f" 测试月份: {year}年{month}月")
print(f"\n{'='*70}")
# 测试结果统计
success_count = 0
fail_count = 0
results = []
# 测试每个服务
for i, service in enumerate(SALARY_SERVICES, 1):
print(f"\n[{i}/{len(SALARY_SERVICES)}] 测试 {service['name']}...")
success = test_salary_calculation(service, year, month, token)
results.append({
"name": service["name"],
"endpoint": service["endpoint"],
"success": success
})
if success:
success_count += 1
else:
fail_count += 1
# 避免请求过快
if i < len(SALARY_SERVICES):
time.sleep(1)
# 输出测试总结
print(f"\n{'='*70}")
print("测试总结")
print(f"{'='*70}")
print(f"总测试数: {len(SALARY_SERVICES)}")
print(f"✅ 成功: {success_count}")
print(f"❌ 失败: {fail_count}")
print(f"\n详细结果:")
for result in results:
status = "✅ 通过" if result["success"] else "❌ 失败"
print(f" {status} - {result['name']}")
print(f"\n{'='*70}")
print("测试说明:")
print("1. 接口调用成功后,请检查后端日志:")
print(" - 是否显示'跳过了 N 条已锁定或已确认的工资记录'")
print(" - 确认已锁定或已确认的记录数量是否正确")
print("2. 检查数据库中已锁定或已确认的记录:")
print(" - 扣款项目是否被保留")
print(" - 补贴项目是否被保留")
print(" - 其他导入的数据是否被保留")
print(f"{'='*70}")
if __name__ == '__main__':
main()