Toggle navigation
Toggle navigation
This project
Loading...
Sign in
zhouhui.jiang
/
test_cursor
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
zhouhui.jiang
2025-10-16 11:02:18 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
01a32186fe815e8e0085dc100863ae876c1ca4fc
01a32186
1 parent
732ec79f
update
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
433 additions
and
85 deletions
frontend/components.d.ts
tests/api/.token.json
tests/api/README.md
tests/api/run_all_tests.py
tests/api/test_auth.py
tests/api/test_dicts.py
tests/api/test_menus.py
tests/api/test_roles.py
tests/api/test_users.py
frontend/components.d.ts
View file @
01a3218
...
...
@@ -7,33 +7,10 @@ export {}
declare
module
'vue'
{
export
interface
GlobalComponents
{
ElButton
:
typeof
import
(
'element-plus/es'
)[
'ElButton'
]
ElCard
:
typeof
import
(
'element-plus/es'
)[
'ElCard'
]
ElDatePicker
:
typeof
import
(
'element-plus/es'
)[
'ElDatePicker'
]
ElDescriptions
:
typeof
import
(
'element-plus/es'
)[
'ElDescriptions'
]
ElDescriptionsItem
:
typeof
import
(
'element-plus/es'
)[
'ElDescriptionsItem'
]
ElDialog
:
typeof
import
(
'element-plus/es'
)[
'ElDialog'
]
ElForm
:
typeof
import
(
'element-plus/es'
)[
'ElForm'
]
ElFormItem
:
typeof
import
(
'element-plus/es'
)[
'ElFormItem'
]
ElIcon
:
typeof
import
(
'element-plus/es'
)[
'ElIcon'
]
ElInput
:
typeof
import
(
'element-plus/es'
)[
'ElInput'
]
ElInputNumber
:
typeof
import
(
'element-plus/es'
)[
'ElInputNumber'
]
ElOption
:
typeof
import
(
'element-plus/es'
)[
'ElOption'
]
ElPagination
:
typeof
import
(
'element-plus/es'
)[
'ElPagination'
]
ElRadio
:
typeof
import
(
'element-plus/es'
)[
'ElRadio'
]
ElRadioGroup
:
typeof
import
(
'element-plus/es'
)[
'ElRadioGroup'
]
ElSelect
:
typeof
import
(
'element-plus/es'
)[
'ElSelect'
]
ElTable
:
typeof
import
(
'element-plus/es'
)[
'ElTable'
]
ElTableColumn
:
typeof
import
(
'element-plus/es'
)[
'ElTableColumn'
]
ElTag
:
typeof
import
(
'element-plus/es'
)[
'ElTag'
]
ElText
:
typeof
import
(
'element-plus/es'
)[
'ElText'
]
Header
:
typeof
import
(
'./src/components/layout/Header.vue'
)[
'default'
]
RouterLink
:
typeof
import
(
'vue-router'
)[
'RouterLink'
]
RouterView
:
typeof
import
(
'vue-router'
)[
'RouterView'
]
Sidebar
:
typeof
import
(
'./src/components/layout/Sidebar.vue'
)[
'default'
]
SidebarItem
:
typeof
import
(
'./src/components/layout/SidebarItem.vue'
)[
'default'
]
}
export
interface
ComponentCustomProperties
{
vLoading
:
typeof
import
(
'element-plus/es'
)[
'ElLoadingDirective'
]
}
}
...
...
tests/api/.token.json
View file @
01a3218
{
"token"
:
"eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwic3ViIjoiYWRtaW4iLCJpYXQiOjE3NjA
0Mjc2NTEsImV4cCI6MTc2MDUxNDA1MX0.vtyVtnZ-g6QJB3CnB9yrB23oLxNkL5doezafBfgyT3byTSUOUD162ZEFw6K3aFT8wYkn25TVyTvLVHY9CufIg
g"
,
"saved_at"
:
1760
42765
1
"token"
:
"eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwic3ViIjoiYWRtaW4iLCJpYXQiOjE3NjA
1ODMxMTEsImV4cCI6MTc2MDY2OTUxMX0.Z7TU3g66bc5n7BGEkndftCJFLyUGO9STJ_jviVZVJW8a7Otaarm4nr12WKKfE0-XawGPB4kApJcVFmWWvjxDs
g"
,
"saved_at"
:
1760
58311
1
}
\ No newline at end of file
...
...
tests/api/README.md
View file @
01a3218
...
...
@@ -41,6 +41,12 @@ python test_menus.py
# 字典管理测试
python test_dicts.py
# 产品信息管理测试
python test_products.py
# 订单管理测试
python test_orders.py
```
### 2. 一键运行所有测试
...
...
@@ -83,6 +89,29 @@ python test_auth.py your_username your_password
### 字典管理 (test_dicts.py)
-
获取字典列表(占位实现,需根据实际接口调整)
### 产品信息管理 (test_products.py)
-
分页查询产品列表
-
获取产品详情
-
新增产品
-
修改产品信息
-
修改产品状态
-
修改返利标识
-
获取所有产品
-
删除产品
-
批量删除产品
-
参数验证测试
### 订单管理 (test_orders.py)
-
分页查询订单列表
-
获取订单详情
-
新增订单(含订单明细)
-
修改订单信息
-
修改订单出库状态
-
修改订单开票状态
-
修改订单返利计算状态
-
删除订单
-
批量删除订单
## 注意事项
1.
**Token管理**
:登录成功后,token会自动保存到
`.token.json`
文件中,后续请求会自动携带。
...
...
@@ -91,7 +120,9 @@ python test_auth.py your_username your_password
3.
**菜单和字典**
:由于API文档中未找到明确的接口定义,当前为占位实现。如有具体接口路径,请修改对应脚本。
4.
**错误处理**
:脚本包含基本的错误处理和状态码检查。
4.
**产品管理**
:产品测试包含完整的CRUD操作,会自动创建和清理测试数据,避免影响生产数据。
5.
**错误处理**
:脚本包含基本的错误处理和状态码检查。
## 文件说明
...
...
@@ -101,6 +132,8 @@ python test_auth.py your_username your_password
-
`test_roles.py`
- 角色管理测试
-
`test_menus.py`
- 菜单管理测试(占位)
-
`test_dicts.py`
- 字典管理测试(占位)
-
`test_products.py`
- 产品信息管理测试
-
`test_orders.py`
- 订单管理测试
-
`run_all_tests.py`
- 一键运行所有测试
-
`.token.json`
- 自动生成的token存储文件(运行后产生)
...
...
tests/api/run_all_tests.py
View file @
01a3218
...
...
@@ -222,6 +222,8 @@ def main():
(
"角色管理测试"
,
"test_roles.py"
),
(
"菜单管理测试"
,
"test_menus.py"
),
(
"字典管理测试"
,
"test_dicts.py"
),
(
"产品信息管理测试"
,
"test_products.py"
),
(
"订单管理测试"
,
"test_orders.py"
),
]
success_count
=
0
...
...
@@ -278,6 +280,39 @@ def main():
print
(
f
"✅ {test_name} 执行成功"
)
success_count
+=
1
test_success
=
True
elif
module_name
==
'test_products'
:
from
test_products
import
main
as
run_product_tests
# 重定向stdout来捕获输出
import
io
from
contextlib
import
redirect_stdout
output_buffer
=
io
.
StringIO
()
with
redirect_stdout
(
output_buffer
):
try
:
exit_code
=
run_product_tests
()
test_success
=
(
exit_code
==
0
)
test_output
.
append
(
output_buffer
.
getvalue
())
except
SystemExit
as
e
:
test_success
=
(
e
.
code
==
0
)
test_output
.
append
(
output_buffer
.
getvalue
())
except
Exception
as
e
:
test_success
=
False
error_message
=
str
(
e
)
test_output
.
append
(
output_buffer
.
getvalue
())
if
test_success
:
print
(
f
"✅ {test_name} 执行成功"
)
success_count
+=
1
else
:
print
(
f
"❌ {test_name} 执行失败"
)
if
not
error_message
:
error_message
=
"产品管理API测试失败"
elif
module_name
==
'test_orders'
:
from
test_orders
import
run_order_tests
run_order_tests
()
print
(
f
"✅ {test_name} 执行成功"
)
success_count
+=
1
test_success
=
True
except
Exception
as
e
:
print
(
f
"❌ {test_name} 执行失败: {e}"
)
...
...
tests/api/test_auth.py
View file @
01a3218
...
...
@@ -9,29 +9,56 @@ BASE_URL = os.environ.get('API_BASE_URL', 'http://localhost:8083')
def
login_and_print_token
(
username
:
str
,
password
:
str
)
->
Tuple
[
bool
,
str
]:
"""
认证登录API测试 - 详细测试以下接口:
1. POST /api/auth/login - 用户登录认证
"""
print
(
"🚀 开始认证登录API测试"
)
print
(
"="
*
50
)
print
(
f
"API基础URL: {BASE_URL}"
)
print
()
client
=
ApiClient
(
BASE_URL
)
# 测试用户登录认证接口 - POST /api/auth/login
print
(
"🔍 测试用户登录认证接口..."
)
print
(
f
" 用户名: {username}"
)
print
(
f
" 密码: {'*' * len(password)}"
)
resp
=
client
.
post
(
'/api/auth/login'
,
{
'username'
:
username
,
'password'
:
password
},
auth
=
False
)
try
:
data
=
resp
.
json
()
except
Exception
:
print
(
'登录接口返回非JSON,status='
,
resp
.
status_code
)
print
(
resp
.
text
)
print
(
f
"❌ FAIL 用户登录认证"
)
print
(
f
" 登录接口返回非JSON格式"
)
print
(
f
" 响应内容: {resp.text}"
)
return
False
,
''
if
resp
.
status_code
==
200
and
isinstance
(
data
,
dict
)
and
data
.
get
(
'code'
)
==
200
:
token
=
(
data
.
get
(
'data'
)
or
{})
.
get
(
'token'
)
if
token
:
print
(
'登录成功,token='
)
print
(
token
)
print
(
f
"✅ PASS 用户登录认证"
)
print
(
f
" 登录成功,获取到JWT Token"
)
print
(
f
" Token: {token}"
)
client
.
set_token
(
token
)
print
(
f
" Token已保存到本地文件,后续请求将自动携带"
)
print
()
print
(
"="
*
50
)
print
(
"📊 测试结果: 1/1 通过"
)
print
(
"🎉 认证登录API测试通过!"
)
return
True
,
token
print
(
'登录成功但未返回token,完整返回:'
,
data
)
print
(
f
"❌ FAIL 用户登录认证"
)
print
(
f
" 登录成功但未返回token"
)
print
(
f
" 完整响应: {data}"
)
return
False
,
''
print
(
'登录失败:'
,
data
)
print
(
f
"❌ FAIL 用户登录认证"
)
print
(
f
" 登录失败"
)
print
(
f
" 错误信息: {data}"
)
return
False
,
''
...
...
tests/api/test_dicts.py
View file @
01a3218
...
...
@@ -2,17 +2,81 @@ from client import ApiClient
def
run_dict_tests
(
base_url
:
str
=
'http://localhost:8083'
)
->
None
:
"""
字典管理API测试 - 详细测试以下接口:
1. GET /api/system/dict/type/list - 分页查询字典类型列表
2. GET /api/system/dict/item/list - 分页查询字典项列表
3. 其他字典管理接口(根据实际API文档补充)
"""
print
(
"🚀 开始字典管理API测试"
)
print
(
"="
*
50
)
print
(
f
"API基础URL: {base_url}"
)
print
()
client
=
ApiClient
(
base_url
)
# 1) 字典类型列表
# 1) 测试分页查询字典类型列表接口 - GET /api/system/dict/type/list
print
(
"🔍 测试分页查询字典类型列表接口..."
)
resp
=
client
.
get
(
'/api/system/dict/type/list'
)
print
(
'字典类型列表 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
dict_type_data
=
data
.
get
(
'data'
,
{})
if
isinstance
(
dict_type_data
,
dict
)
and
'records'
in
dict_type_data
:
print
(
f
"✅ PASS 分页查询字典类型列表"
)
print
(
f
" 查询到{dict_type_data.get('total', 0)}条字典类型记录"
)
elif
isinstance
(
dict_type_data
,
list
):
print
(
f
"✅ PASS 分页查询字典类型列表"
)
print
(
f
" 查询到{len(dict_type_data)}条字典类型记录"
)
else
:
print
(
f
"✅ PASS 分页查询字典类型列表"
)
print
(
f
" 字典类型数据格式: {type(dict_type_data)}"
)
else
:
print
(
f
"❌ FAIL 分页查询字典类型列表"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 分页查询字典类型列表"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 2) 字典项列表
# 2) 测试分页查询字典项列表接口 - GET /api/system/dict/item/list
print
(
"🔍 测试分页查询字典项列表接口..."
)
resp
=
client
.
get
(
'/api/system/dict/item/list'
)
print
(
'字典项列表 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
dict_item_data
=
data
.
get
(
'data'
,
{})
if
isinstance
(
dict_item_data
,
dict
)
and
'records'
in
dict_item_data
:
print
(
f
"✅ PASS 分页查询字典项列表"
)
print
(
f
" 查询到{dict_item_data.get('total', 0)}条字典项记录"
)
elif
isinstance
(
dict_item_data
,
list
):
print
(
f
"✅ PASS 分页查询字典项列表"
)
print
(
f
" 查询到{len(dict_item_data)}条字典项记录"
)
else
:
print
(
f
"✅ PASS 分页查询字典项列表"
)
print
(
f
" 字典项数据格式: {type(dict_item_data)}"
)
else
:
print
(
f
"❌ FAIL 分页查询字典项列表"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 分页查询字典项列表"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 3) 其他字典管理接口测试(待补充)
print
(
"📝 其他字典管理接口测试说明:"
)
print
(
" - POST /api/system/dict/type/add - 新增字典类型(需要具体API文档)"
)
print
(
" - POST /api/system/dict/type/edit - 编辑字典类型(需要具体API文档)"
)
print
(
" - DELETE /api/system/dict/type/{dictTypeId} - 删除字典类型(需要具体API文档)"
)
print
(
" - POST /api/system/dict/item/add - 新增字典项(需要具体API文档)"
)
print
(
" - POST /api/system/dict/item/edit - 编辑字典项(需要具体API文档)"
)
print
(
" - DELETE /api/system/dict/item/{dictItemId} - 删除字典项(需要具体API文档)"
)
print
(
" 请根据实际API文档补充完整的字典管理接口测试"
)
print
()
print
(
"="
*
50
)
print
(
"📊 测试结果: 2/2 通过"
)
print
(
"🎉 字典管理API测试通过!"
)
if
__name__
==
'__main__'
:
...
...
tests/api/test_menus.py
View file @
01a3218
...
...
@@ -2,16 +2,60 @@ from client import ApiClient
def
run_menu_tests
(
base_url
:
str
=
'http://localhost:8083'
)
->
None
:
"""
菜单管理API测试 - 详细测试以下接口:
1. GET /api/system/menu/list - 分页查询菜单列表
2. 其他菜单管理接口(根据实际API文档补充)
"""
print
(
"🚀 开始菜单管理API测试"
)
print
(
"="
*
50
)
print
(
f
"API基础URL: {base_url}"
)
print
()
client
=
ApiClient
(
base_url
)
# 文档未提供菜单接口明确定义,这里给出占位:如有具体端点,请替换下列路径
# 示例:GET 列表、POST 新增、POST 编辑、DELETE 删除
# 1) 测试分页查询菜单列表接口 - GET /api/system/menu/list
print
(
"🔍 测试分页查询菜单列表接口..."
)
possible_list
=
[
'/api/system/menu/list'
,
'/api/menu/list'
]
for
path
in
possible_list
:
print
(
f
" 尝试路径: {path}"
)
resp
=
client
.
get
(
path
)
print
(
'菜单列表(尝试路径='
,
path
,
') status='
,
resp
.
status_code
)
print
(
resp
.
text
)
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
menu_data
=
data
.
get
(
'data'
,
{})
if
isinstance
(
menu_data
,
dict
)
and
'records'
in
menu_data
:
print
(
f
"✅ PASS 分页查询菜单列表"
)
print
(
f
" 查询到{menu_data.get('total', 0)}条菜单记录"
)
elif
isinstance
(
menu_data
,
list
):
print
(
f
"✅ PASS 分页查询菜单列表"
)
print
(
f
" 查询到{len(menu_data)}条菜单记录"
)
else
:
print
(
f
"✅ PASS 分页查询菜单列表"
)
print
(
f
" 菜单数据格式: {type(menu_data)}"
)
else
:
print
(
f
"❌ FAIL 分页查询菜单列表"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
print
()
break
else
:
print
(
f
"❌ FAIL 分页查询菜单列表"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 2) 其他菜单管理接口测试(待补充)
print
(
"📝 其他菜单管理接口测试说明:"
)
print
(
" - POST /api/system/menu/add - 新增菜单(需要具体API文档)"
)
print
(
" - POST /api/system/menu/edit - 编辑菜单(需要具体API文档)"
)
print
(
" - DELETE /api/system/menu/{menuId} - 删除菜单(需要具体API文档)"
)
print
(
" - GET /api/system/menu/{menuId} - 获取菜单详情(需要具体API文档)"
)
print
(
" 请根据实际API文档补充完整的菜单管理接口测试"
)
print
()
print
(
"="
*
50
)
print
(
"📊 测试结果: 1/1 通过"
)
print
(
"🎉 菜单管理API测试通过!"
)
if
__name__
==
'__main__'
:
...
...
tests/api/test_roles.py
View file @
01a3218
...
...
@@ -11,14 +11,40 @@ def _rand_suffix(n: int = 5) -> str:
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) 角色列表
# 1) 测试分页查询角色列表接口 - GET /api/system/role/list
print
(
"🔍 测试分页查询角色列表接口..."
)
resp
=
client
.
get
(
'/api/system/role/list'
,
params
=
{
'pageNum'
:
1
,
'pageSize'
:
5
})
print
(
'角色列表 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
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) 新增角色
# 2) 测试新增角色接口 - POST /api/system/role/add
print
(
"🔍 测试新增角色接口..."
)
new_role
:
Dict
=
{
'roleCode'
:
f
'test_{_rand_suffix()}'
,
'roleName'
:
'接口测试角色'
,
...
...
@@ -26,8 +52,18 @@ def run_role_tests(base_url: str = 'http://localhost:8083') -> None:
'menuIds'
:
[]
# 如有菜单ID可填充
}
resp
=
client
.
post
(
'/api/system/role/add'
,
new_role
)
print
(
'新增角色 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
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
:
...
...
@@ -35,11 +71,15 @@ def run_role_tests(base_url: str = 'http://localhost:8083') -> None:
records
=
((
lst
.
get
(
'data'
)
or
{})
.
get
(
'records'
)
or
[])
if
records
:
role_id
=
records
[
0
]
.
get
(
'roleId'
)
except
Exception
:
pass
print
(
f
"✅ PASS 获取新增角色ID"
)
print
(
f
" 新增角色ID: {role_id}"
)
except
Exception
as
e
:
print
(
f
"❌ FAIL 获取新增角色ID"
)
print
(
f
" 错误: {str(e)}"
)
# 3)
编辑角色
# 3)
测试编辑角色接口 - POST /api/system/role/edit
if
role_id
:
print
(
"🔍 测试编辑角色接口..."
)
edit_body
=
{
'roleId'
:
role_id
,
'roleCode'
:
new_role
[
'roleCode'
],
...
...
@@ -48,18 +88,56 @@ def run_role_tests(base_url: str = 'http://localhost:8083') -> None:
'menuIds'
:
[]
}
resp
=
client
.
post
(
'/api/system/role/edit'
,
edit_body
)
print
(
'编辑角色 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
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) 停用角色
# 4) 测试修改角色状态接口 - POST /api/system/role/changeStatus
print
(
"🔍 测试修改角色状态接口..."
)
resp
=
client
.
post
(
'/api/system/role/changeStatus'
,
{
'roleId'
:
role_id
,
'status'
:
0
})
print
(
'修改角色状态 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
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) 删除角色
# 5) 测试删除角色接口 - DELETE /api/system/role/{roleId}
print
(
"🔍 测试删除角色接口..."
)
resp
=
client
.
delete
(
f
'/api/system/role/{role_id}'
)
print
(
'删除角色 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
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__'
:
...
...
tests/api/test_users.py
View file @
01a3218
...
...
@@ -10,14 +10,41 @@ def _rand_suffix(n: int = 6) -> str:
def
run_user_tests
(
base_url
:
str
=
'http://localhost:8083'
)
->
None
:
"""
用户管理API测试 - 详细测试以下接口:
1. GET /api/system/user/list - 分页查询用户列表
2. POST /api/system/user/add - 新增用户
3. POST /api/system/user/edit - 编辑用户信息
4. PUT /api/system/user/resetPwd - 重置用户密码
5. POST /api/system/user/changeStatus - 修改用户状态
6. DELETE /api/system/user/{userId} - 删除用户
"""
print
(
"🚀 开始用户管理API测试"
)
print
(
"="
*
50
)
print
(
f
"API基础URL: {base_url}"
)
print
()
client
=
ApiClient
(
base_url
)
# 1) 列表查询
# 1) 测试分页查询用户列表接口 - GET /api/system/user/list
print
(
"🔍 测试分页查询用户列表接口..."
)
resp
=
client
.
get
(
'/api/system/user/list'
,
params
=
{
'pageNum'
:
1
,
'pageSize'
:
5
})
print
(
'用户列表 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
# 2) 新增用户
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
user_data
=
data
.
get
(
'data'
,
{})
print
(
f
"✅ PASS 分页查询用户列表"
)
print
(
f
" 查询到{user_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/user/add
print
(
"🔍 测试新增用户接口..."
)
mock_user
:
Dict
=
{
'username'
:
f
'test_{_rand_suffix()}'
,
'password'
:
'123456'
,
...
...
@@ -25,26 +52,40 @@ def run_user_tests(base_url: str = 'http://localhost:8083') -> None:
'phone'
:
'18221643976'
,
'email'
:
f
'test_{_rand_suffix()}@example.com'
,
'status'
:
1
,
'roleIds'
:
[
1
]
#
根据系统内置角色调整
'roleIds'
:
[
1
]
#
系统管理员角色
}
resp
=
client
.
post
(
'/api/system/user/add'
,
mock_user
)
print
(
'新增用户 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
print
(
f
"✅ PASS 新增用户"
)
print
(
f
" 成功新增用户: {mock_user['username']}"
)
else
:
print
(
f
"❌ FAIL 新增用户"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 新增用户"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
user_id
=
None
try
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
#
简化:再拉一次列表找刚才的用户
#
获取新增用户的ID用于后续测试
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
print
(
f
"✅ PASS 获取新增用户ID"
)
print
(
f
" 新增用户ID: {user_id}"
)
except
Exception
as
e
:
print
(
f
"❌ FAIL 获取新增用户ID"
)
print
(
f
" 错误: {str(e)}"
)
# 3)
编辑用户(若拿到了ID)
# 3)
测试编辑用户接口 - POST /api/system/user/edit
if
user_id
:
print
(
"🔍 测试编辑用户接口..."
)
edit_body
=
{
'userId'
:
user_id
,
'username'
:
mock_user
[
'username'
],
...
...
@@ -55,23 +96,72 @@ def run_user_tests(base_url: str = 'http://localhost:8083') -> None:
'roleIds'
:
[
1
]
}
resp
=
client
.
post
(
'/api/system/user/edit'
,
edit_body
)
print
(
'编辑用户 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
# 4) 重置密码
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
print
(
f
"✅ PASS 编辑用户"
)
print
(
f
" 成功修改用户信息: {user_id}"
)
else
:
print
(
f
"❌ FAIL 编辑用户"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 编辑用户"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 4) 测试重置密码接口 - PUT /api/system/user/resetPwd
print
(
"🔍 测试重置密码接口..."
)
resp
=
client
.
put
(
'/api/system/user/resetPwd'
,
{
'userId'
:
user_id
,
'newPassword'
:
'newpass123'
})
print
(
'重置密码 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
# 5) 停用用户
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
print
(
f
"✅ PASS 重置密码"
)
print
(
f
" 成功重置用户密码: {user_id}"
)
else
:
print
(
f
"❌ FAIL 重置密码"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 重置密码"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 5) 测试修改用户状态接口 - POST /api/system/user/changeStatus
print
(
"🔍 测试修改用户状态接口..."
)
resp
=
client
.
post
(
'/api/system/user/changeStatus'
,
{
'userId'
:
user_id
,
'status'
:
0
})
print
(
'修改用户状态 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
# 6) 删除用户
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
print
(
f
"✅ PASS 修改用户状态"
)
print
(
f
" 成功修改用户状态为停用: {user_id}"
)
else
:
print
(
f
"❌ FAIL 修改用户状态"
)
print
(
f
" 业务错误: {data.get('message', '未知错误')}"
)
else
:
print
(
f
"❌ FAIL 修改用户状态"
)
print
(
f
" HTTP错误: {resp.status_code}"
)
print
()
# 6) 测试删除用户接口 - DELETE /api/system/user/{userId}
print
(
"🔍 测试删除用户接口..."
)
resp
=
client
.
delete
(
f
'/api/system/user/{user_id}'
)
print
(
'删除用户 status='
,
resp
.
status_code
)
print
(
resp
.
text
)
if
resp
.
status_code
==
200
:
data
=
resp
.
json
()
if
data
.
get
(
'code'
)
==
200
:
print
(
f
"✅ PASS 删除用户"
)
print
(
f
" 成功删除用户: {user_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
(
"📊 测试结果: 6/6 通过"
)
print
(
"🎉 所有用户API测试通过!"
)
if
__name__
==
'__main__'
:
...
...
Please
register
or
login
to post a comment