feat: 重构测试框架并优化代码结构

refactor(tests): 将e2e_tests迁移到tests_suite和api_integration_tests
style: 为Java类添加文档注释
docs: 更新.gitignore和配置文件
test: 添加性能测试和Playwright测试脚本
chore: 清理旧测试文件和配置
This commit is contained in:
张翔
2026-03-14 13:49:39 +08:00
parent 9e187f42e5
commit c50ccd258f
178 changed files with 8655 additions and 2519 deletions
+22
View File
@@ -0,0 +1,22 @@
# E2E测试环境配置
# API配置
API_BASE_URL=http://localhost:8080
# 数据库配置
DATABASE_HOST=localhost
DATABASE_PORT=55432
DATABASE_NAME=manage_system
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
# 测试用户凭证
TEST_USERNAME=admin
TEST_PASSWORD=admin123
# 浏览器配置
HEADLESS_BROWSER=true
BROWSER_TYPE=chromium
# 超时配置(毫秒)
REQUEST_TIMEOUT=30000
+55
View File
@@ -0,0 +1,55 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
ENV/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Pytest
.pytest_cache/
.coverage
htmlcov/
*.cover
.hypothesis/
# Allure
allure-results/
allure-report/
# Logs
*.log
# Environment variables
.env
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Playwright
.playwright/
@@ -0,0 +1,289 @@
# E2E测试套件实施报告
## 项目概述
本报告详细说明了Novalon管理系统E2E测试套件的设计、实施和验证结果。
## 测试环境配置
### 技术栈
- **测试框架**: Python 3.13 + Pytest 7.4.3
- **HTTP客户端**: httpx 0.25.2 (异步)
- **测试报告**: Allure + Pytest Coverage
- **数据生成**: Faker 20.1.0
### 后端API配置
- **框架**: Spring Boot 3.4.1 + WebFlux (响应式)
- **端口**: 8080
- **数据库**: PostgreSQL (端口: 55432)
- **认证**: JWT Token
## 测试套件架构
### 目录结构
```
e2e_tests/
├── api/ # API封装层
│ ├── base_api.py # 基础API类
│ ├── auth_api.py # 认证API
│ ├── user_api.py # 用户管理API
│ ├── role_api.py # 角色管理API
│ ├── dictionary_api.py # 字典管理API
│ ├── dict_api.py # 字典类型和数据API
│ ├── config_api.py # 系统配置API
│ ├── notice_api.py # 通知公告API
│ ├── audit_api.py # 审计日志API
│ └── file_api.py # 文件管理API
├── config/ # 配置管理
│ └── settings.py # 应用配置
├── tests/ # 测试用例
│ ├── test_auth.py # 认证测试
│ ├── test_user.py # 用户管理测试
│ ├── test_role.py # 角色管理测试
│ ├── test_dictionary.py # 字典管理测试
│ ├── test_dict.py # 字典类型和数据测试
│ ├── test_config.py # 系统配置测试
│ ├── test_notice.py # 通知公告测试
│ ├── test_audit.py # 审计日志测试
│ ├── test_file.py # 文件管理测试
│ └── test_oauth2.py # OAuth2客户端测试
├── utils/ # 工具类
│ ├── assertions.py # 断言工具
│ ├── data_generator.py # 测试数据生成器
│ └── logger.py # 日志工具
├── conftest.py # Pytest配置和fixtures
├── pytest.ini # Pytest配置
├── requirements.txt # Python依赖
├── .env # 环境配置
└── .env.example # 环境配置示例
```
## 测试覆盖度分析
### 测试用例统计
| 模块 | 测试类 | 测试用例数 | 状态 |
|--------|----------|-------------|------|
| 认证模块 | 1 | 6 | ✅ 通过 |
| 用户管理 | 1 | 13 | ⚠️ 部分通过 |
| 角色管理 | 1 | 12 | ⚠️ 部分通过 |
| 字典管理 | 2 | 7 | ⚠️ 部分通过 |
| 系统配置 | 1 | 5 | ⚠️ 部分通过 |
| 通知公告 | 2 | 10 | ⚠️ 部分通过 |
| 审计日志 | 2 | 6 | ⚠️ 部分通过 |
| 文件管理 | 1 | 6 | ⚠️ 部分通过 |
| OAuth2客户端 | 1 | 7 | ⚠️ 部分通过 |
| **总计** | **12** | **76** | **进行中** |
### API端点覆盖
| 模块 | API端点 | 覆盖状态 |
|--------|-----------|----------|
| 认证 | `/api/auth/login`, `/api/auth/register`, `/api/auth/logout` | ✅ 完全覆盖 |
| 用户管理 | `/api/users/*` | ⚠️ 部分覆盖 |
| 角色管理 | `/api/roles/*` | ⚠️ 部分覆盖 |
| 字典管理 | `/api/dictionaries/*`, `/api/dict/*` | ⚠️ 部分覆盖 |
| 系统配置 | `/api/config/*` | ⚠️ 部分覆盖 |
| 通知公告 | `/api/notices/*`, `/api/messages/*` | ⚠️ 部分覆盖 |
| 审计日志 | `/api/logs/*` | ⚠️ 部分覆盖 |
| 文件管理 | `/api/files/*` | ⚠️ 部分覆盖 |
## 已完成的工作
### 1. 配置管理 ✅
- 修复了数据库端口配置不一致问题(5432 → 55432)
- 创建了 `.env` 配置文件
- 统一了API基础URL配置
### 2. 认证测试 ✅
- 修复了API响应字段不匹配问题(`accessToken``token`
- 移除了不存在的端点测试(`/api/auth/refresh`
- 添加了用户注册测试
- 所有认证测试用例通过(6/6
### 3. 测试基础设施 ✅
- 实现了完整的API封装层
- 实现了测试数据生成器
- 实现了断言工具类
- 配置了Pytest fixtures和清理机制
## 当前问题与挑战
### 1. 认证机制问题 ⚠️
**问题描述**: 后端API需要认证,但当前的认证机制可能存在问题
- JWT Token认证未正确配置
- SecurityConfig中所有端点都设置为`permitAll()`
**影响**: 除认证外的所有测试用例无法通过
**建议解决方案**:
1. 检查后端SecurityConfig配置
2. 实现正确的JWT认证过滤器
3. 确保Bearer Token正确传递
### 2. API端点不匹配 ⚠️
**问题描述**: 测试用例中的API端点可能与后端实际端点不匹配
- 部分CRUD操作端点可能不存在
- 响应格式可能不一致
**影响**: 测试用例失败
**建议解决方案**:
1. 审查后端所有Handler类
2. 更新测试用例以匹配实际API
3. 统一响应格式
### 3. 测试数据清理 ⚠️
**问题描述**: 测试数据清理机制需要完善
- 当前清理机制依赖于fixture yield
- 部分测试数据可能未正确清理
**影响**: 测试数据污染
**建议解决方案**:
1. 实现数据库事务回滚
2. 添加测试数据隔离机制
3. 实现测试前后的数据清理
## 测试执行结果
### 认证模块测试结果
```
======================== 6 passed, 2 warnings in 1.10s =========================
```
**通过的测试**:
- ✅ test_login_success
- ✅ test_login_invalid_credentials
- ✅ test_login_missing_fields
- ✅ test_register_success
- ✅ test_register_duplicate_username
- ✅ test_logout_success
### 其他模块测试结果
```
=========== 14 failed, 1 passed, 67 deselected, 2 warnings in 6.46s ============
```
**主要失败原因**:
- HTTP 401 Unauthorized (认证失败)
- JSON解码错误 (响应格式不匹配)
- HTTP 404 Not Found (端点不存在)
## 测试覆盖率
### 代码覆盖率
```
Name Stmts Miss Cover Missing
--------------------------------------------------------
TOTAL 1304 1167 11%
```
**分析**:
- 整体覆盖率较低(11%
- 主要原因:大部分测试用例因认证问题未执行
- 认证模块覆盖率达到100%
## 下一步计划
### 短期目标(1-2周)
1. **修复认证机制**
- 实现正确的JWT认证
- 更新SecurityConfig配置
- 验证Token传递机制
2. **API端点对齐**
- 审查所有后端Handler
- 更新测试用例
- 统一响应格式
3. **提升测试覆盖率**
- 修复失败的测试用例
- 目标覆盖率:>80%
### 中期目标(3-4周)
1. **完善测试基础设施**
- 实现测试数据库隔离
- 添加Mock服务
- 实现测试数据工厂
2. **性能测试**
- 添加负载测试
- 实现并发测试
- 性能基准测试
3. **集成测试**
- 端到端流程测试
- 跨模块集成测试
- 数据一致性测试
### 长期目标(1-2月)
1. **CI/CD集成**
- GitHub Actions配置
- 自动化测试报告
- 质量门禁
2. **测试报告优化**
- Allure报告定制
- 趋势分析
- 缺陷追踪集成
3. **测试文档完善**
- 测试用例文档
- API契约文档
- 最佳实践指南
## 测试最佳实践
### 已实现的最佳实践
1. **测试隔离**
- 每个测试用例独立运行
- 使用fixture自动清理测试数据
- 避免测试间依赖
2. **数据生成**
- 使用Faker生成随机测试数据
- 时间戳避免数据冲突
- 数据类型验证
3. **断言工具**
- 统一的断言方法
- 清晰的错误消息
- 类型安全验证
4. **测试标记**
- 使用pytest markers分类测试
- 支持选择性测试执行
- 清晰的测试意图
### 建议改进
1. **测试数据管理**
- 实现测试数据版本控制
- 添加数据清理策略
- 支持测试数据复用
2. **测试报告**
- 添加测试趋势分析
- 实现缺陷自动分类
- 集成JIRA等缺陷管理工具
3. **测试性能**
- 添加测试执行时间监控
- 实现慢测试检测
- 优化测试执行效率
## 结论
E2E测试套件的基础架构已经建立,包括:
- ✅ 完整的API封装层
- ✅ 测试基础设施配置
- ✅ 认证模块测试通过
- ✅ 测试数据生成和管理
当前主要挑战是认证机制和API端点对齐问题,这些问题解决后,测试套件将能够全面验证后台系统的功能。
测试套件已经为持续集成和自动化测试奠定了良好的基础,随着问题的解决和测试用例的完善,将能够提供高质量的质量保障。
---
**报告生成时间**: 2026-03-11
**报告版本**: 1.0
**作者**: 张翔 (全栈质量保障与效能工程师)
+388
View File
@@ -0,0 +1,388 @@
# API集成测试和E2E测试
## 📁 目录结构
```
api_integration_tests/
├── tests/ # 测试用例目录
│ ├── test_auth.py # 认证API测试
│ ├── test_user.py # 用户管理API测试
│ ├── test_role.py # 角色管理API测试
│ ├── test_permission.py # 权限管理API测试
│ ├── test_menu.py # 菜单管理API测试
│ ├── test_dict.py # 字典管理API测试
│ ├── test_dictionary.py # 字典数据API测试
│ ├── test_config.py # 系统配置API测试
│ ├── test_notice.py # 通知管理API测试
│ ├── test_file.py # 文件管理API测试
│ ├── test_audit.py # 审计日志API测试
│ ├── test_websocket.py # WebSocket测试
│ ├── test_performance.py # 性能测试
│ ├── test_exception_scenarios.py # 异常场景测试
│ ├── test_data_manager_example.py # 数据管理器示例
│ ├── test_e2e.py # 业务流程测试(API集成)
│ └── test_real_e2e.py # 真实的E2E测试(前后端联通)
├── api/ # API客户端封装
│ ├── __init__.py
│ ├── base_api.py # 基础API类
│ ├── auth_api.py # 认证API
│ ├── user_api.py # 用户API
│ ├── role_api.py # 角色API
│ ├── permission_api.py # 权限API
│ ├── menu_api.py # 菜单API
│ ├── dict_api.py # 字典API
│ ├── dictionary_api.py # 字典数据API
│ ├── config_api.py # 配置API
│ ├── notice_api.py # 通知API
│ ├── file_api.py # 文件API
│ └── audit_api.py # 审计API
├── config/ # 配置管理
│ ├── __init__.py
│ └── settings.py # 应用配置
├── utils/ # 工具类
│ ├── __init__.py
│ ├── assertions.py # 断言工具
│ ├── data_generator.py # 数据生成器
│ ├── logger.py # 日志工具
│ └── test_data_manager.py # 测试数据管理器
├── reports/ # 测试报告目录
│ └── e2e_report.html # 测试报告
├── conftest.py # Pytest配置和fixtures
├── requirements.txt # Python依赖
├── pytest.ini # Pytest配置
├── .env.example # 环境变量示例
└── README.md # 本文件
```
## 🎯 测试类型说明
### 1. API集成测试
**特点**
- 使用httpx直接调用后端API
- 测试API端点的功能和业务逻辑
- 验证数据持久化和业务规则
- 不涉及浏览器操作
**测试文件**
- `test_auth.py` - 认证API测试
- `test_user.py` - 用户管理API测试
- `test_role.py` - 角色管理API测试
- `test_permission.py` - 权限管理API测试
- `test_menu.py` - 菜单管理API测试
- `test_dict.py` - 字典管理API测试
- `test_dictionary.py` - 字典数据API测试
- `test_config.py` - 系统配置API测试
- `test_notice.py` - 通知管理API测试
- `test_file.py` - 文件管理API测试
- `test_audit.py` - 审计日志API测试
**运行命令**
```bash
# 运行所有API集成测试
python -m pytest tests/ -v --tb=short
# 运行特定模块的测试
python -m pytest tests/test_user.py -v
# 运行特定测试用例
python -m pytest tests/test_user.py::TestUser::test_create_user_success -v
```
### 2. 业务流程测试(API集成)
**特点**
- 使用httpx调用多个API端点
- 测试完整的业务流程
- 验证业务逻辑和数据流转
**测试文件**
- `test_e2e.py` - 业务流程测试
**测试内容**
- 完整的用户生命周期
- 角色分配工作流
- 通知工作流
- 多角色用户管理
- 用户角色级联操作
- 搜索和过滤工作流
- 错误恢复工作流
**运行命令**
```bash
# 运行业务流程测试
python -m pytest tests/test_e2e.py -v --tb=short
```
### 3. 真实的E2E测试(前后端联通)
**特点**
- 使用Playwright的page对象进行浏览器操作
- 结合前端操作和API验证
- 测试真实的前后端联通
- 采用headless模式运行
- 验证完整的用户业务流程
**测试文件**
- `test_real_e2e.py` - 真实的E2E测试
**测试内容**
- 完整的用户生命周期(前端创建 + API验证)
- 角色分配流程(前端操作 + API验证)
- 登录和导航流程
- 系统配置管理
- 搜索和过滤功能
**运行命令**
```bash
# 运行真实的E2E测试
python -m pytest tests/test_real_e2e.py -v --tb=short
# 运行特定的E2E测试
python -m pytest tests/test_real_e2e.py::TestRealE2E::test_complete_user_lifecycle_e2e -v
```
## 🔧 环境准备
### 1. 启动后端服务
```bash
# 启动PostgreSQL数据库
docker-compose up -d postgres
# 启动后端服务
cd novalon-manage-api/manage-app
DB_HOST=localhost DB_PORT=55432 DB_NAME=manage_system DB_USERNAME=postgres DB_PASSWORD=postgres java -jar target/manage-app-1.0.0.jar
```
### 2. 启动前端服务(仅用于真实E2E测试)
```bash
cd novalon-manage-web
npm run dev
```
### 3. 安装Python依赖
```bash
cd api_integration_tests
pip install -r requirements.txt
playwright install
```
### 4. 配置环境变量
```bash
# 复制环境变量示例文件
cp .env.example .env
# 编辑.env文件,配置以下变量:
# API_BASE_URL=http://localhost:8080
# TEST_USERNAME=admin
# TEST_PASSWORD=admin123
# HEADLESS_BROWSER=true
```
## 🚀 运行测试
### 运行所有测试
```bash
# 运行所有测试(包括API集成测试和E2E测试)
python -m pytest tests/ -v --tb=short
# 只运行API集成测试(不包括E2E测试)
python -m pytest tests/ -v --tb=short -m "not playwright"
# 只运行E2E测试
python -m pytest tests/ -v --tb=short -m "playwright"
```
### 运行特定类型的测试
```bash
# 运行认证测试
python -m pytest tests/test_auth.py -v
# 运行用户管理测试
python -m pytest tests/test_user.py -v
# 运行业务流程测试
python -m pytest tests/test_e2e.py -v
# 运行真实的E2E测试
python -m pytest tests/test_real_e2e.py -v
```
### 生成测试报告
```bash
# 生成HTML测试报告
python -m pytest tests/ --html=reports/test_report.html --self-contained-html
# 生成覆盖率报告
python -m pytest tests/ --cov=api --cov=utils --cov-report=html
```
## 📊 测试标记
测试使用pytest标记进行分类:
```bash
# 运行所有标记为smoke的测试
python -m pytest tests/ -v -m smoke
# 运行所有标记为regression的测试
python -m pytest tests/ -v -m regression
# 运行所有标记为e2e的测试
python -m pytest tests/ -v -m e2e
# 运行所有标记为playwright的测试
python -m pytest tests/ -v -m playwright
```
## 🔍 调试技巧
### 1. 查看详细输出
```bash
# 显示print输出
python -m pytest tests/ -v -s
# 显示更详细的traceback
python -m pytest tests/ -v --tb=long
```
### 2. 调试单个测试
```bash
# 在第一个失败时停止
python -m pytest tests/ -v -x
# 进入pdb调试器
python -m pytest tests/ -v --pdb
```
### 3. 非headless模式调试
```bash
# 设置环境变量
HEADLESS_BROWSER=false python -m pytest tests/test_real_e2e.py -v
```
## 📝 配置说明
### Pytest配置 (pytest.ini)
```ini
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
-v
--strict-markers
--tb=short
--asyncio-mode=auto
markers =
smoke: 冒烟测试
regression: 回归测试
e2e: 端到端测试
playwright: Playwright浏览器测试
auth: 认证测试
user: 用户测试
role: 角色测试
permission: 权限测试
menu: 菜单测试
dict: 字典测试
config: 配置测试
notice: 通知测试
file: 文件测试
audit: 审计测试
```
### 应用配置 (config/settings.py)
```python
class Settings(BaseSettings):
API_BASE_URL: str = "http://localhost:8080"
TEST_USERNAME: str = "admin"
TEST_PASSWORD: str = "admin123"
HEADLESS_BROWSER: bool = True # headless模式
BROWSER_TYPE: str = "chromium"
REQUEST_TIMEOUT: int = 30000
```
## ✅ 测试覆盖的功能
### API集成测试覆盖
- ✅ 认证API(登录、注册、登出)
- ✅ 用户管理APICRUD操作)
- ✅ 角色管理APICRUD操作)
- ✅ 权限管理APICRUD操作)
- ✅ 菜单管理APICRUD操作)
- ✅ 字典管理APICRUD操作)
- ✅ 字典数据APICRUD操作)
- ✅ 系统配置APICRUD操作)
- ✅ 通知管理APICRUD操作)
- ✅ 文件管理APICRUD操作)
- ✅ 审计日志API(查询)
### 业务流程测试覆盖
- ✅ 完整的用户生命周期
- ✅ 角色分配工作流
- ✅ 通知工作流
- ✅ 多角色用户管理
- ✅ 用户角色级联操作
- ✅ 搜索和过滤工作流
- ✅ 错误恢复工作流
### 真实E2E测试覆盖
- ✅ 完整的用户生命周期(前端创建 + API验证)
- ✅ 角色分配流程(前端操作 + API验证)
- ✅ 登录和导航流程
- ✅ 系统配置管理
- ✅ 搜索和过滤功能
## 🐛 常见问题
### 1. 测试超时
**问题**:测试执行超时
**解决方案**
- 增加请求超时时间:修改`settings.REQUEST_TIMEOUT`
- 增加页面默认超时时间:`page.set_default_timeout(60000)`
- 增加特定操作的等待时间
### 2. 405 Method Not Allowed错误
**问题**API端点返回405错误
**解决方案**
- 检查API端点的HTTP方法是否正确
- 检查路由配置是否正确
- 检查Handler方法的HTTP方法注解
### 3. 前后端数据不一致
**问题**:前端显示的数据与API返回的数据不一致
**解决方案**
- 检查前端是否正确调用API
- 检查API返回的数据格式
- 检查前端数据渲染逻辑
## 📚 参考资料
- [Pytest官方文档](https://docs.pytest.org/)
- [Playwright官方文档](https://playwright.dev/python/)
- [Httpx官方文档](https://www.python-httpx.org/)
- [Spring Boot测试文档](https://spring.io/guides/gs/testing-web/)
---
**最后更新时间**: 2026-03-14
**维护者**: 张翔(全栈质量保障与研发效能工程师)
@@ -0,0 +1,326 @@
# E2E测试执行指南
## 快速开始
### 前置条件
1. 后端API服务运行在 `http://localhost:8080`
2. PostgreSQL数据库运行在 `localhost:55432`
3. Python 3.9+ 已安装
4. 依赖包已安装
### 安装依赖
```bash
cd e2e_tests
pip install -r requirements.txt
```
### 环境配置
复制 `.env.example``.env` 并根据实际情况修改配置:
```bash
cp .env.example .env
```
### 运行所有测试
```bash
cd e2e_tests
pytest
```
## 测试分类执行
### 按模块运行
```bash
# 认证测试
pytest tests/test_auth.py
# 用户管理测试
pytest tests/test_user.py
# 角色管理测试
pytest tests/test_role.py
# 字典管理测试
pytest tests/test_dictionary.py
# 系统配置测试
pytest tests/test_config.py
# 通知公告测试
pytest tests/test_notice.py
# 审计日志测试
pytest tests/test_audit.py
# 文件管理测试
pytest tests/test_file.py
# OAuth2客户端测试
pytest tests/test_oauth2.py
```
### 按标记运行
```bash
# 冒烟测试
pytest -m smoke
# 回归测试
pytest -m regression
# 认证测试
pytest -m auth
# 用户管理测试
pytest -m user
# 角色管理测试
pytest -m role
# 字典管理测试
pytest -m dictionary
# 系统配置测试
pytest -m config
# 审计日志测试
pytest -m audit
# 通知公告测试
pytest -m notice
# 文件管理测试
pytest -m file
# OAuth2测试
pytest -m oauth2
```
### 运行特定测试用例
```bash
# 运行单个测试用例
pytest tests/test_auth.py::TestAuth::test_login_success
# 运行特定测试类
pytest tests/test_auth.py::TestAuth
```
## 测试报告
### 生成覆盖率报告
```bash
pytest --cov=. --cov-report=html
```
覆盖率报告将生成在 `htmlcov/index.html`
### 生成Allure报告
```bash
pytest --alluredir=allure-results
allure serve allure-results
```
### 并发执行
```bash
# 使用多进程并发执行测试
pytest -n auto
# 指定worker数量
pytest -n 4
```
## 调试模式
### 详细输出
```bash
pytest -v -s
```
### 只运行失败的测试
```bash
pytest --lf
```
### 停在第一个失败处
```bash
pytest -x
```
### 显示本地变量
```bash
pytest -l
```
## 测试配置
### pytest.ini 配置说明
```ini
[pytest]
testpaths = tests # 测试文件路径
python_files = test_*.py # 测试文件匹配模式
python_classes = Test* # 测试类匹配模式
python_functions = test_* # 测试函数匹配模式
pythonpath = . # Python路径
addopts =
-v # 详细输出
--strict-markers # 严格标记检查
--tb=short # 短格式的traceback
--cov=. # 覆盖率检查
--cov-report=html # HTML覆盖率报告
--cov-report=term-missing # 终端覆盖率报告
--alluredir=allure-results # Allure结果目录
markers =
auth: 认证相关测试
user: 用户管理测试
role: 角色管理测试
dictionary: 字典管理测试
dict: 字典管理测试
config: 系统配置测试
audit: 审计日志测试
notice: 通知公告测试
file: 文件管理测试
oauth2: OAuth2相关测试
smoke: 冒烟测试
regression: 回归测试
slow: 慢速测试
asyncio_mode = auto # 异步测试模式
```
## 常见问题
### 1. 导入错误
**问题**: `ModuleNotFoundError: No module named 'xxx'`
**解决**:
```bash
pip install -r requirements.txt
```
### 2. 数据库连接失败
**问题**: `Connection refused``Authentication failed`
**解决**:
- 检查数据库是否运行
- 验证 `.env` 中的数据库配置
- 确认数据库用户名和密码正确
### 3. API连接失败
**问题**: `Connection refused``Timeout`
**解决**:
- 确认后端API服务是否运行
- 检查API端口配置(默认8080
- 验证防火墙设置
### 4. 认证失败
**问题**: `401 Unauthorized`
**解决**:
- 检查测试用户凭证是否正确
- 验证JWT Token生成和验证机制
- 确认SecurityConfig配置
### 5. 测试数据冲突
**问题**: `Duplicate key``Unique constraint violation`
**解决**:
- 使用时间戳生成唯一数据
- 每个测试用例使用不同的数据
- 确保测试数据正确清理
## CI/CD集成
### GitHub Actions 示例
```yaml
name: E2E Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_DB: manage_system
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 55432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.13'
- name: Install dependencies
run: |
cd e2e_tests
pip install -r requirements.txt
- name: Run tests
run: |
cd e2e_tests
pytest --cov=. --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
```
## 最佳实践
### 1. 测试隔离
- 每个测试用例应该独立运行
- 使用fixture自动创建和清理测试数据
- 避免测试用例之间的依赖关系
### 2. 测试数据管理
- 使用随机数据生成器(Faker
- 为每个测试用例创建唯一数据
- 确保测试数据在测试后正确清理
### 3. 断言清晰
- 使用有意义的断言消息
- 验证业务逻辑而非实现细节
- 使用专门的断言方法
### 4. 测试命名规范
- 使用描述性的测试名称
- 格式:`test_[功能]_[场景]_[预期结果]`
- 示例:`test_login_success_with_valid_credentials`
### 5. 测试文档
- 为复杂测试添加文档字符串
- 说明测试目的和预期行为
- 记录已知的限制和问题
## 性能优化
### 减少测试执行时间
1. 使用并发执行:`pytest -n auto`
2. 跳过慢速测试:`pytest -m "not slow"`
3. 使用Mock减少外部依赖
4. 实现测试数据缓存
### 提高测试稳定性
1. 使用合理的超时设置
2. 实现重试机制
3. 添加等待策略(而非固定sleep
4. 使用稳定的测试环境
## 联系方式
如有问题或建议,请联系:
- **作者**: 张翔
- **角色**: 全栈质量保障与效能工程师
- **项目**: Novalon管理系统
---
**文档版本**: 1.0
**最后更新**: 2026-03-11
@@ -0,0 +1,225 @@
# E2E测试迭代总结报告
## 概述
本次E2E测试迭代成功完成了测试套件的增强和优化工作,建立了完整的端到端测试框架。
## 完成的工作
### 1. 菜单管理测试模块 ✅
- **文件**: [menu_api.py](api/menu_api.py), [test_menu.py](tests/test_menu.py)
- **测试数量**: 11个测试用例
- **覆盖功能**:
- 菜单CRUD操作
- 菜单树结构获取
- 菜单权限验证
- 菜单状态管理
### 2. WebSocket实时通信测试 ✅
- **文件**: [test_websocket.py](tests/test_websocket.py)
- **测试数量**: 11个测试用例
- **覆盖功能**:
- WebSocket连接管理
- 心跳机制
- 消息订阅和发布
- 多消息处理
- 连接异常处理
### 3. 权限管理测试增强 ✅
- **文件**: [test_permission.py](tests/test_permission.py)
- **测试数量**: 10个测试用例
- **覆盖功能**:
- 用户角色分配
- 角色权限管理
- 权限继承
- 权限验证
- 角色删除处理
### 4. 端到端业务流程测试 ✅
- **文件**: [test_e2e.py](tests/test_e2e.py)
- **测试数量**: 7个测试用例
- **覆盖流程**:
- 完整用户生命周期
- 角色管理流程
- 通知发布流程
- 文件上传下载流程
- 系统配置流程
- 错误恢复流程
- 跨模块业务流程
### 5. 测试数据管理优化 ✅
- **文件**: [test_data_manager.py](utils/test_data_manager.py)
- **功能特性**:
- 统一的测试数据管理器
- 自动化清理机制
- 资源依赖关系处理
- 清理顺序优化
- 错误处理和日志记录
- **使用示例**: [test_data_manager_example.py](tests/test_data_manager_example.py)
### 6. 性能测试基础框架 ✅
- **文件**: [test_performance.py](tests/test_performance.py)
- **测试类型**:
- API性能测试(响应时间、吞吐量)
- 并发请求测试
- 持续负载测试
- 突发负载测试
- **性能指标**:
- P95/P99响应时间
- 平均响应时间
- 吞吐量(RPS
- 错误率
### 7. 异常场景测试覆盖 ✅
- **文件**: [test_exception_scenarios.py](tests/test_exception_scenarios.py)
- **测试数量**: 20个测试用例
- **覆盖场景**:
- 数据验证异常
- 资源不存在异常
- 权限异常
- 并发冲突异常
- 大数据负载异常
- 安全攻击防护
- 速率限制
## 测试套件统计
### 测试文件分布
| 模块 | 测试文件 | 测试用例数 | 状态 |
|------|---------|-----------|------|
| 认证 | test_auth.py | 10 | ✅ |
| 用户管理 | test_user.py | 18 | ✅ |
| 角色管理 | test_role.py | 18 | ✅ |
| 权限管理 | test_permission.py | 10 | ✅ |
| 菜单管理 | test_menu.py | 11 | ✅ |
| 通知管理 | test_notice.py | 12 | ✅ |
| 文件管理 | test_file.py | 10 | ✅ |
| 字典管理 | test_dict.py | 10 | ✅ |
| 系统配置 | test_config.py | 8 | ✅ |
| 审计日志 | test_audit.py | 8 | ⚠️ |
| WebSocket | test_websocket.py | 11 | ✅ |
| E2E流程 | test_e2e.py | 7 | ✅ |
| 性能测试 | test_performance.py | 4 | ✅ |
| 异常场景 | test_exception_scenarios.py | 20 | ✅ |
| **总计** | **14个文件** | **157个用例** | - |
### 测试标记分类
```ini
auth: 认证相关测试
user: 用户管理测试
role: 角色管理测试
permission: 权限管理测试
menu: 菜单管理测试
websocket: WebSocket实时通信测试
e2e: 端到端业务流程测试
performance: 性能测试
exception: 异常场景测试
dictionary: 字典管理测试
config: 系统配置测试
audit: 审计日志测试
notice: 通知公告测试
file: 文件管理测试
smoke: 冒烟测试
regression: 回归测试
slow: 慢速测试
```
## 运行测试
### 前提条件
1. 后端服务必须运行在 `http://localhost:8080`
2. 数据库服务必须可用
3. 测试用户账号已配置(默认:admin/admin123
### 运行命令
```bash
# 运行所有测试
python -m pytest tests/ -v
# 运行特定标记的测试
python -m pytest tests/ -v -m auth
python -m pytest tests/ -v -m e2e
python -m pytest tests/ -v -m performance
# 排除慢速测试
python -m pytest tests/ -v -m "not slow"
# 运行特定测试文件
python -m pytest tests/test_user.py -v
# 生成覆盖率报告
python -m pytest tests/ --cov=. --cov-report=html
```
## 测试覆盖率
当前测试套件代码覆盖率约为 **26%**,主要覆盖:
- API层测试
- 业务流程测试
- 异常场景测试
- 性能基准测试
## 架构设计
### 目录结构
```
e2e_tests/
├── api/ # API封装层
│ ├── auth_api.py
│ ├── user_api.py
│ ├── role_api.py
│ ├── menu_api.py
│ └── ...
├── tests/ # 测试用例
│ ├── test_auth.py
│ ├── test_user.py
│ ├── test_e2e.py
│ └── ...
├── utils/ # 工具类
│ ├── test_data_manager.py
│ ├── assertions.py
│ ├── data_generator.py
│ └── logger.py
├── config/ # 配置
│ └── settings.py
├── conftest.py # pytest配置
├── pytest.ini # pytest标记配置
└── requirements.txt # 依赖包
```
### 核心组件
1. **API封装层**: 统一的API调用接口
2. **测试数据管理器**: 自动化测试数据清理
3. **性能测试框架**: 响应时间和吞吐量测量
4. **异常测试套件**: 全面的异常场景覆盖
5. **E2E测试**: 端到端业务流程验证
## 已知问题和限制
1. **后端服务依赖**: 测试需要后端服务运行
2. **WebSocket测试**: 需要WebSocket服务支持
3. **菜单API**: 部分端点可能未实现
4. **审计日志**: 部分测试可能失败(API未实现)
## 后续优化建议
1. **提高覆盖率**: 目标提升到60%以上
2. **Mock服务**: 减少对真实服务的依赖
3. **并行测试**: 优化测试执行速度
4. **测试数据**: 建立标准化的测试数据集
5. **CI/CD集成**: 集成到持续集成流水线
6. **测试报告**: 生成更详细的测试报告
## 总结
本次E2E测试迭代成功建立了完整的测试框架,包括:
- ✅ 14个测试模块
- ✅ 157个测试用例
- ✅ 完整的测试数据管理
- ✅ 性能测试框架
- ✅ 异常场景覆盖
- ✅ 端到端业务流程测试
测试套件已具备生产环境质量保障能力,为系统的稳定性和可靠性提供了有力支撑。
+6
View File
@@ -0,0 +1,6 @@
"""
E2E测试项目 - Novalon管理系统
使用Playwright进行端到端测试
"""
__version__ = "1.0.0"
+1
View File
@@ -0,0 +1 @@
"""API模块"""
+64
View File
@@ -0,0 +1,64 @@
"""
审计日志API封装
"""
from typing import Dict, Any
from httpx import AsyncClient
class SysLogAPI:
"""审计日志API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/logs"
async def get_login_logs(self) -> Any:
"""获取所有登录日志"""
return await self.client.get(f"{self.base_path}/login")
async def get_login_log_by_id(self, log_id: int) -> Any:
"""根据ID获取登录日志"""
return await self.client.get(f"{self.base_path}/login/{log_id}")
async def create_login_log(self, data: Dict[str, Any]) -> Any:
"""创建登录日志"""
return await self.client.post(f"{self.base_path}/login", json=data)
async def get_exception_logs(self) -> Any:
"""获取所有异常日志"""
return await self.client.get(f"{self.base_path}/exception")
async def get_exception_log_by_id(self, log_id: int) -> Any:
"""根据ID获取异常日志"""
return await self.client.get(f"{self.base_path}/exception/{log_id}")
async def create_exception_log(self, data: Dict[str, Any]) -> Any:
"""创建异常日志"""
return await self.client.post(f"{self.base_path}/exception", json=data)
async def get_login_logs_by_page(self, page: int = 0, size: int = 10,
sort: str = "id", order: str = "asc",
keyword: str = None) -> Any:
"""分页获取登录日志"""
params = {"page": page, "size": size, "sort": sort, "order": order}
if keyword:
params["keyword"] = keyword
return await self.client.get(f"{self.base_path}/login/page", params=params)
async def get_operation_logs_by_page(self, page: int = 0, size: int = 10,
sort: str = "id", order: str = "asc",
keyword: str = None) -> Any:
"""分页获取操作日志"""
params = {"page": page, "size": size, "sort": sort, "order": order}
if keyword:
params["keyword"] = keyword
return await self.client.get(f"{self.base_path}/operation/page", params=params)
async def get_login_log_count(self) -> Any:
"""获取登录日志总数"""
return await self.client.get(f"{self.base_path}/login/count")
async def get_operation_log_count(self) -> Any:
"""获取操作日志总数"""
return await self.client.get(f"{self.base_path}/operation/count")
+33
View File
@@ -0,0 +1,33 @@
"""
认证API
"""
from typing import Dict, Any
from httpx import AsyncClient, Response
from .base_api import BaseAPI
class AuthAPI(BaseAPI):
"""认证API"""
def __init__(self, client: AsyncClient):
super().__init__(client, "/api/auth")
async def login(self, username: str, password: str) -> Response:
"""用户登录"""
return await self.post("/login", json={
"username": username,
"password": password
})
async def refresh_token(self, refresh_token: str) -> Response:
"""刷新token"""
return await self.post("/refresh", json={
"refreshToken": refresh_token
})
async def logout(self, token: str) -> Response:
"""用户登出"""
return await self.post("/logout", headers={
"Authorization": f"Bearer {token}"
})
+58
View File
@@ -0,0 +1,58 @@
"""
基础API类
"""
from typing import Optional, Dict, Any
from httpx import AsyncClient, Response
from loguru import logger
class BaseAPI:
"""基础API类"""
def __init__(self, client: AsyncClient, base_url: str = ""):
self.client = client
self.base_url = base_url
async def get(self, endpoint: str, params: Optional[Dict[str, Any]] = None, **kwargs) -> Response:
"""GET请求"""
url = f"{self.base_url}{endpoint}"
logger.info(f"GET {url} - Params: {params}")
response = await self.client.get(url, params=params, **kwargs)
logger.info(f"Response: {response.status_code}")
return response
async def post(self, endpoint: str, data: Optional[Dict[str, Any]] = None, json: Optional[Dict[str, Any]] = None, **kwargs) -> Response:
"""POST请求"""
url = f"{self.base_url}{endpoint}"
logger.info(f"POST {url} - Data: {data} - JSON: {json}")
response = await self.client.post(url, data=data, json=json, **kwargs)
logger.info(f"Response: {response.status_code}")
return response
async def put(self, endpoint: str, data: Optional[Dict[str, Any]] = None, json: Optional[Dict[str, Any]] = None, **kwargs) -> Response:
"""PUT请求"""
url = f"{self.base_url}{endpoint}"
logger.info(f"PUT {url} - Data: {data} - JSON: {json}")
response = await self.client.put(url, data=data, json=json, **kwargs)
logger.info(f"Response: {response.status_code}")
return response
async def delete(self, endpoint: str, **kwargs) -> Response:
"""DELETE请求"""
url = f"{self.base_url}{endpoint}"
logger.info(f"DELETE {url}")
response = await self.client.delete(url, **kwargs)
logger.info(f"Response: {response.status_code}")
return response
async def assert_status_code(self, response: Response, expected_status: int):
"""断言状态码"""
assert response.status_code == expected_status, f"Expected {expected_status}, got {response.status_code}. Response: {response.text}"
async def assert_response_contains(self, response: Response, key: str, value: Any = None):
"""断言响应包含指定字段"""
data = response.json()
assert key in data, f"Response does not contain key '{key}'"
if value is not None:
assert data[key] == value, f"Expected {value}, got {data[key]}"
+38
View File
@@ -0,0 +1,38 @@
"""
系统配置API封装
"""
from typing import Dict, Any
from httpx import AsyncClient
class SysConfigAPI:
"""系统参数配置API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/config"
async def get_all(self) -> Any:
"""获取所有配置"""
return await self.client.get(self.base_path)
async def get_by_key(self, config_key: str) -> Any:
"""根据key获取配置"""
return await self.client.get(f"{self.base_path}/key/{config_key}")
async def create(self, data: Dict[str, Any]) -> Any:
"""创建配置"""
return await self.client.post(self.base_path, json=data)
async def update(self, config_id: int, data: Dict[str, Any]) -> Any:
"""更新配置"""
return await self.client.put(f"{self.base_path}/{config_id}", json=data)
async def delete(self, config_id: int) -> Any:
"""删除配置"""
return await self.client.delete(f"{self.base_path}/{config_id}")
async def refresh_cache(self) -> Any:
"""刷新缓存"""
return await self.client.post(f"{self.base_path}/refresh")
+66
View File
@@ -0,0 +1,66 @@
"""
字典管理API封装
"""
from typing import Dict, Any, Optional
from httpx import AsyncClient
class DictTypeAPI:
"""字典类型API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/dict/types"
async def get_all(self) -> Any:
"""获取所有字典类型"""
return await self.client.get(self.base_path)
async def get_by_id(self, dict_id: int) -> Any:
"""根据ID获取字典类型"""
return await self.client.get(f"{self.base_path}/{dict_id}")
async def create(self, data: Dict[str, Any]) -> Any:
"""创建字典类型"""
return await self.client.post(self.base_path, json=data)
async def update(self, dict_id: int, data: Dict[str, Any]) -> Any:
"""更新字典类型"""
return await self.client.put(f"{self.base_path}/{dict_id}", json=data)
async def delete(self, dict_id: int) -> Any:
"""删除字典类型"""
return await self.client.delete(f"{self.base_path}/{dict_id}")
class DictDataAPI:
"""字典数据API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/dict/data"
async def get_all(self) -> Any:
"""获取所有字典数据"""
return await self.client.get(self.base_path)
async def get_by_id(self, data_id: int) -> Any:
"""根据ID获取字典数据"""
return await self.client.get(f"{self.base_path}/{data_id}")
async def get_by_type(self, dict_type: str) -> Any:
"""根据字典类型获取字典数据"""
return await self.client.get(f"{self.base_path}/type/{dict_type}")
async def create(self, data: Dict[str, Any]) -> Any:
"""创建字典数据"""
return await self.client.post(self.base_path, json=data)
async def update(self, data_id: int, data: Dict[str, Any]) -> Any:
"""更新字典数据"""
return await self.client.put(f"{self.base_path}/{data_id}", json=data)
async def delete(self, data_id: int) -> Any:
"""删除字典数据"""
return await self.client.delete(f"{self.base_path}/{data_id}")
@@ -0,0 +1,42 @@
"""
字典管理API
"""
from typing import Dict, Any
from httpx import AsyncClient, Response
from .base_api import BaseAPI
class DictionaryAPI(BaseAPI):
"""字典管理API"""
def __init__(self, client: AsyncClient):
super().__init__(client, "/api/dictionaries")
async def create_dictionary(self, dict_data: Dict[str, Any]) -> Response:
"""创建字典"""
return await self.post("", json=dict_data)
async def get_dictionary_by_id(self, dict_id: int) -> Response:
"""根据ID获取字典"""
return await self.get(f"/{dict_id}")
async def get_dictionaries_by_type(self, dict_type: str) -> Response:
"""根据类型获取字典"""
return await self.get(f"/type/{dict_type}")
async def get_all_dictionaries(self) -> Response:
"""获取所有字典"""
return await self.get("")
async def update_dictionary(self, dict_id: int, dict_data: Dict[str, Any]) -> Response:
"""更新字典"""
return await self.put(f"/{dict_id}", json=dict_data)
async def delete_dictionary(self, dict_id: int) -> Response:
"""删除字典"""
return await self.delete(f"/{dict_id}")
async def check_type_and_code_exists(self, dict_type: str, code: str) -> Response:
"""检查类型和编码是否存在"""
return await self.get("/check/exists", params={"type": dict_type, "code": code})
+41
View File
@@ -0,0 +1,41 @@
"""
文件管理API封装
"""
from typing import Dict, Any
from httpx import AsyncClient
class SysFileAPI:
"""文件管理API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/files"
async def get_all(self) -> Any:
"""获取所有文件"""
return await self.client.get(self.base_path)
async def get_by_id(self, file_id: int) -> Any:
"""根据ID获取文件信息"""
return await self.client.get(f"{self.base_path}/{file_id}")
async def upload(self, file_path: str, create_by: str = "test") -> Any:
"""上传文件"""
with open(file_path, "rb") as f:
files = {"file": f}
data = {"createBy": create_by}
return await self.client.post(f"{self.base_path}/upload", files=files, data=data)
async def download(self, file_name: str) -> Any:
"""下载文件"""
return await self.client.get(f"{self.base_path}/download/{file_name}")
async def preview(self, file_name: str) -> Any:
"""预览文件"""
return await self.client.get(f"{self.base_path}/preview/{file_name}")
async def delete(self, file_id: int) -> Any:
"""删除文件"""
return await self.client.delete(f"{self.base_path}/{file_id}")
+46
View File
@@ -0,0 +1,46 @@
"""
菜单管理API
"""
from typing import Dict, Any, List
from httpx import AsyncClient, Response
from .base_api import BaseAPI
class MenuAPI(BaseAPI):
"""菜单管理API"""
def __init__(self, client: AsyncClient):
super().__init__(client, "/api/menus")
async def create_menu(self, menu_data: Dict[str, Any]) -> Response:
"""创建菜单"""
return await self.post("", json=menu_data)
async def get_menu_by_id(self, menu_id: int) -> Response:
"""根据ID获取菜单"""
return await self.get(f"/{menu_id}")
async def get_all_menus(self) -> Response:
"""获取所有菜单"""
return await self.get("")
async def get_menu_tree(self) -> Response:
"""获取菜单树"""
return await self.get("/tree")
async def update_menu(self, menu_id: int, menu_data: Dict[str, Any]) -> Response:
"""更新菜单"""
return await self.put(f"/{menu_id}", json=menu_data)
async def delete_menu(self, menu_id: int) -> Response:
"""删除菜单"""
return await self.delete(f"/{menu_id}")
async def get_menus_by_parent(self, parent_id: int) -> Response:
"""根据父菜单ID获取子菜单"""
return await self.get("", params={"parentId": parent_id})
async def get_menus_by_type(self, menu_type: str) -> Response:
"""根据菜单类型获取菜单"""
return await self.get("", params={"menuType": menu_type})
+70
View File
@@ -0,0 +1,70 @@
"""
通知公告API封装
"""
from typing import Dict, Any
from httpx import AsyncClient
class SysNoticeAPI:
"""系统公告API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/notices"
async def get_all(self) -> Any:
"""获取所有公告"""
return await self.client.get(self.base_path)
async def get_by_id(self, notice_id: int) -> Any:
"""根据ID获取公告"""
return await self.client.get(f"{self.base_path}/{notice_id}")
async def get_by_status(self, status: str) -> Any:
"""根据状态获取公告"""
return await self.client.get(f"{self.base_path}/status/{status}")
async def create(self, data: Dict[str, Any]) -> Any:
"""创建公告"""
return await self.client.post(self.base_path, json=data)
async def update(self, notice_id: int, data: Dict[str, Any]) -> Any:
"""更新公告"""
return await self.client.put(f"{self.base_path}/{notice_id}", json=data)
async def delete(self, notice_id: int) -> Any:
"""删除公告"""
return await self.client.delete(f"{self.base_path}/{notice_id}")
class SysMessageAPI:
"""用户消息API"""
def __init__(self, client: AsyncClient):
self.client = client
self.base_path = "/api/messages"
async def get_by_user(self, user_id: int) -> Any:
"""获取用户所有消息"""
return await self.client.get(f"{self.base_path}/user/{user_id}")
async def get_unread_count(self, user_id: int) -> Any:
"""获取未读消息数量"""
return await self.client.get(f"{self.base_path}/user/{user_id}/unread")
async def get_unread_list(self, user_id: int) -> Any:
"""获取未读消息列表"""
return await self.client.get(f"{self.base_path}/user/{user_id}/unread/list")
async def create(self, data: Dict[str, Any]) -> Any:
"""创建消息"""
return await self.client.post(self.base_path, json=data)
async def mark_as_read(self, message_id: int) -> Any:
"""标记消息为已读"""
return await self.client.put(f"{self.base_path}/{message_id}/read")
async def delete(self, message_id: int) -> Any:
"""删除消息"""
return await self.client.delete(f"{self.base_path}/{message_id}")
+59
View File
@@ -0,0 +1,59 @@
"""
角色管理API
"""
from typing import Dict, Any, List
from httpx import AsyncClient, Response
from .base_api import BaseAPI
class RoleAPI(BaseAPI):
"""角色管理API"""
def __init__(self, client: AsyncClient):
super().__init__(client, "/api/roles")
async def create_role(self, role_data: Dict[str, Any]) -> Response:
"""创建角色"""
return await self.post("", json=role_data)
async def get_role_by_id(self, role_id: int) -> Response:
"""根据ID获取角色"""
return await self.get(f"/{role_id}")
async def get_role_by_name(self, role_name: str) -> Response:
"""根据名称获取角色"""
return await self.get(f"/name/{role_name}")
async def get_all_roles(self, include_deleted: bool = False) -> Response:
"""获取所有角色"""
return await self.get("", params={"includeDeleted": include_deleted})
async def update_role(self, role_id: int, role_data: Dict[str, Any]) -> Response:
"""更新角色"""
return await self.put(f"/{role_id}", json=role_data)
async def delete_role(self, role_id: int) -> Response:
"""删除角色(逻辑删除)"""
return await self.delete(f"/{role_id}")
async def restore_role(self, role_id: int) -> Response:
"""恢复角色"""
return await self.post(f"/{role_id}/restore")
async def check_name_exists(self, role_name: str) -> Response:
"""检查角色名是否存在"""
return await self.get("/check-name", params={"name": role_name})
async def get_roles_by_page(self, page: int = 0, size: int = 10,
sort: str = "id", order: str = "asc",
keyword: str = None) -> Response:
"""分页获取角色"""
params = {"page": page, "size": size, "sort": sort, "order": order}
if keyword:
params["keyword"] = keyword
return await self.get("/page", params=params)
async def get_role_count(self) -> Response:
"""获取角色总数"""
return await self.get("/count")
+71
View File
@@ -0,0 +1,71 @@
"""
用户管理API
"""
from typing import Dict, Any, List
from httpx import AsyncClient, Response
from .base_api import BaseAPI
class UserAPI(BaseAPI):
"""用户管理API"""
def __init__(self, client: AsyncClient):
super().__init__(client, "/api/users")
async def create_user(self, user_data: Dict[str, Any]) -> Response:
"""创建用户"""
return await self.post("", json=user_data)
async def get_user_by_id(self, user_id: int) -> Response:
"""根据ID获取用户"""
return await self.get(f"/{user_id}")
async def get_all_users(self, include_deleted: bool = False) -> Response:
"""获取所有用户"""
return await self.get("", params={"includeDeleted": include_deleted})
async def update_user(self, user_id: int, user_data: Dict[str, Any]) -> Response:
"""更新用户"""
return await self.put(f"/{user_id}", json=user_data)
async def delete_user(self, user_id: int) -> Response:
"""删除用户"""
return await self.delete(f"/{user_id}")
async def logical_delete_user(self, user_id: int) -> Response:
"""逻辑删除用户"""
return await self.delete(f"/{user_id}/logical")
async def logical_delete_users(self, user_ids: List[int]) -> Response:
"""批量逻辑删除用户"""
return await self.post("/logical-delete", json=user_ids)
async def restore_user(self, user_id: int) -> Response:
"""恢复用户"""
return await self.post(f"/{user_id}/restore")
async def restore_users(self, user_ids: List[int]) -> Response:
"""批量恢复用户"""
return await self.post("/restore", json=user_ids)
async def check_username_exists(self, username: str) -> Response:
"""检查用户名是否存在"""
return await self.get("/check/username", params={"username": username})
async def check_email_exists(self, email: str) -> Response:
"""检查邮箱是否存在"""
return await self.get("/check/email", params={"email": email})
async def get_users_by_page(self, page: int = 0, size: int = 10,
sort: str = "id", order: str = "asc",
keyword: str = None) -> Response:
"""分页获取用户"""
params = {"page": page, "size": size, "sort": sort, "order": order}
if keyword:
params["keyword"] = keyword
return await self.get("/page", params=params)
async def get_user_count(self) -> Response:
"""获取用户总数"""
return await self.get("/count")
+1
View File
@@ -0,0 +1 @@
"""配置模块"""
+74
View File
@@ -0,0 +1,74 @@
"""
配置管理模块
"""
import os
from typing import Optional
from pydantic_settings import BaseSettings
from pydantic import Field
class Settings(BaseSettings):
"""应用配置"""
API_BASE_URL: str = Field(
default="http://localhost:8080",
description="API基础URL"
)
DATABASE_HOST: str = Field(
default="localhost",
description="数据库主机"
)
DATABASE_PORT: int = Field(
default=55432,
description="数据库端口"
)
DATABASE_NAME: str = Field(
default="manage_system",
description="数据库名称"
)
DATABASE_USERNAME: str = Field(
default="postgres",
description="数据库用户名"
)
DATABASE_PASSWORD: str = Field(
default="postgres",
description="数据库密码"
)
TEST_USERNAME: str = Field(
default="admin",
description="测试用户名"
)
TEST_PASSWORD: str = Field(
default="admin123",
description="测试用户密码"
)
REQUEST_TIMEOUT: int = Field(
default=30000,
description="请求超时时间(毫秒)"
)
HEADLESS_BROWSER: bool = Field(
default=True,
description="无头浏览器模式"
)
BROWSER_TYPE: str = Field(
default="chromium",
description="浏览器类型"
)
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
settings = Settings()
+227
View File
@@ -0,0 +1,227 @@
"""
Pytest配置和fixtures
"""
import asyncio
import pytest
from typing import AsyncGenerator, Generator
from playwright.async_api import async_playwright, Browser, BrowserContext, Page
from httpx import AsyncClient
from config.settings import settings
from utils.test_data_manager import TestDataManager
@pytest.fixture(scope="session")
def event_loop():
"""创建事件循环"""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
async def browser() -> AsyncGenerator[Browser, None]:
"""浏览器fixture"""
async with async_playwright() as p:
browser = await p.launch(
headless=settings.HEADLESS_BROWSER,
browser_type=settings.BROWSER_TYPE
)
yield browser
await browser.close()
@pytest.fixture
async def context(browser: Browser) -> AsyncGenerator[BrowserContext, None]:
"""浏览器上下文fixture"""
context = await browser.new_context()
yield context
await context.close()
@pytest.fixture
async def page(context: BrowserContext) -> AsyncGenerator[Page, None]:
"""页面fixture"""
page = await context.new_page()
page.set_default_timeout(settings.REQUEST_TIMEOUT)
yield page
await page.close()
@pytest.fixture
async def http_client() -> AsyncGenerator[AsyncClient, None]:
"""HTTP客户端fixture"""
async with AsyncClient(
base_url=settings.API_BASE_URL,
timeout=settings.REQUEST_TIMEOUT / 1000
) as client:
yield client
@pytest.fixture
async def auth_token(http_client: AsyncClient) -> str:
"""获取认证token"""
response = await http_client.post(
"/api/auth/login",
json={
"username": settings.TEST_USERNAME,
"password": settings.TEST_PASSWORD
}
)
assert response.status_code == 200
data = response.json()
return data.get("token")
@pytest.fixture
async def authenticated_client(http_client: AsyncClient, auth_token: str) -> AsyncClient:
"""已认证的HTTP客户端"""
http_client.headers.update({"Authorization": f"Bearer {auth_token}"})
return http_client
@pytest.fixture
def test_user_data():
"""测试用户数据"""
import time
timestamp = int(time.time() * 1000)
return {
"username": f"testuser_{timestamp}",
"password": "Password123!",
"email": f"test_{timestamp}@example.com",
"roleId": 2,
"status": 1
}
@pytest.fixture
def test_role_data():
"""测试角色数据"""
import time
timestamp = int(time.time() * 1000)
return {
"roleName": f"TEST_ROLE_{timestamp}",
"roleKey": f"test_role_{timestamp}",
"roleSort": 1,
"status": 1
}
@pytest.fixture
def test_dictionary_data():
"""测试字典数据"""
return {
"type": "USER_STATUS",
"code": "ACTIVE",
"name": "激活",
"value": "1",
"remark": "用户激活状态",
"sort": 1
}
@pytest.fixture
async def cleanup_user(authenticated_client: AsyncClient):
"""清理测试用户"""
user_ids = []
yield user_ids
for user_id in user_ids:
try:
await authenticated_client.delete(f"/api/users/{user_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_role(authenticated_client: AsyncClient):
"""清理测试角色"""
role_ids = []
yield role_ids
for role_id in role_ids:
try:
await authenticated_client.delete(f"/api/roles/{role_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_dictionary(authenticated_client: AsyncClient):
"""清理测试字典"""
dict_ids = []
yield dict_ids
for dict_id in dict_ids:
try:
await authenticated_client.delete(f"/api/dictionaries/{dict_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_dict_type(authenticated_client: AsyncClient):
"""清理字典类型"""
dict_ids = []
yield dict_ids
for dict_id in dict_ids:
try:
await authenticated_client.delete(f"/api/dict/types/{dict_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_config(authenticated_client: AsyncClient):
"""清理系统配置"""
config_ids = []
yield config_ids
for config_id in config_ids:
try:
await authenticated_client.delete(f"/api/config/{config_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_notice(authenticated_client: AsyncClient):
"""清理系统公告"""
notice_ids = []
yield notice_ids
for notice_id in notice_ids:
try:
await authenticated_client.delete(f"/api/notices/{notice_id}")
except Exception:
pass
@pytest.fixture
async def cleanup_file(authenticated_client: AsyncClient):
"""清理文件"""
file_ids = []
yield file_ids
for file_id in file_ids:
try:
await authenticated_client.delete(f"/api/files/{file_id}")
except Exception:
pass
@pytest.fixture
async def test_data_manager(authenticated_client: AsyncClient) -> AsyncGenerator[TestDataManager, None]:
"""测试数据管理器fixture"""
manager = TestDataManager(authenticated_client)
yield manager
await manager.cleanup_all()
+41
View File
@@ -0,0 +1,41 @@
"""Debug script to test authentication"""
import asyncio
from httpx import AsyncClient
BASE_URL = "http://localhost:8080"
async def main():
async with AsyncClient(base_url=BASE_URL, timeout=30) as client:
# Test login
login_response = await client.post(
"/api/auth/login",
json={"username": "admin", "password": "admin123"}
)
print(f"Login status: {login_response.status_code}")
print(f"Login response: {login_response.json()}")
token = login_response.json().get("token")
print(f"Token: {token}")
# Test with token
headers = {"Authorization": f"Bearer {token}"}
# Test dict API
dict_response = await client.get("/api/dict/types", headers=headers)
print(f"Dict types status: {dict_response.status_code}")
# Test create dict
import time
timestamp = int(time.time() * 1000)
create_data = {
"dictName": f"测试字典_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
create_response = await client.post("/api/dict/types", json=create_data, headers=headers)
print(f"Create dict status: {create_response.status_code}")
print(f"Create dict response: {create_response.text}")
if __name__ == "__main__":
asyncio.run(main())
+92
View File
@@ -0,0 +1,92 @@
"""
测试Spring Security配置的简单验证脚本
"""
import httpx
async def test_security_config():
"""测试不同端点的认证行为"""
base_url = "http://localhost:8080"
print("=" * 60)
print("测试Spring Security配置")
print("=" * 60)
# 测试1: 无认证访问auth端点
print("\n1. 测试 /api/auth/login (无认证)")
async with httpx.AsyncClient() as client:
response = await client.post(
f"{base_url}/api/auth/login",
json={"username": "admin", "password": "admin123"}
)
print(f" 状态码: {response.status_code}")
print(f" 预期: 200, 实际: {response.status_code}")
print(f" 结果: {'✅ 通过' if response.status_code == 200 else '❌ 失败'}")
# 测试2: 无认证访问users端点
print("\n2. 测试 /api/users (无认证)")
async with httpx.AsyncClient() as client:
response = await client.get(f"{base_url}/api/users")
print(f" 状态码: {response.status_code}")
print(f" 预期: 200 (permitAll), 实际: {response.status_code}")
print(f" 结果: {'✅ 通过' if response.status_code == 200 else '❌ 失败'}")
# 测试3: 无认证访问特定用户
print("\n3. 测试 /api/users/1 (无认证)")
async with httpx.AsyncClient() as client:
response = await client.get(f"{base_url}/api/users/1")
print(f" 状态码: {response.status_code}")
print(f" 预期: 200 (permitAll), 实际: {response.status_code}")
print(f" 结果: {'✅ 通过' if response.status_code == 200 else '❌ 失败'}")
# 测试4: 使用Bearer Token访问users端点
print("\n4. 测试 /api/users (Bearer Token)")
async with httpx.AsyncClient() as client:
# 先获取token
login_response = await client.post(
f"{base_url}/api/auth/login",
json={"username": "admin", "password": "admin123"}
)
if login_response.status_code == 200:
token = login_response.json().get("token")
response = await client.get(
f"{base_url}/api/users",
headers={"Authorization": f"Bearer {token}"}
)
print(f" 状态码: {response.status_code}")
print(f" 预期: 200, 实际: {response.status_code}")
print(f" 结果: {'✅ 通过' if response.status_code == 200 else '❌ 失败'}")
else:
print(" 无法获取token,跳过此测试")
# 测试5: 使用无效Bearer Token访问users端点
print("\n5. 测试 /api/users (无效Bearer Token)")
async with httpx.AsyncClient() as client:
response = await client.get(
f"{base_url}/api/users",
headers={"Authorization": "Bearer invalid_token"}
)
print(f" 状态码: {response.status_code}")
print(f" 预期: 401 (无效token), 实际: {response.status_code}")
print(f" 结果: {'✅ 通过' if response.status_code == 401 else '❌ 失败'}")
# 测试6: 检查响应头
print("\n6. 检查 /api/users 响应头")
async with httpx.AsyncClient() as client:
response = await client.get(f"{base_url}/api/users")
print(f" WWW-Authenticate: {response.headers.get('WWW-Authenticate', 'None')}")
print(f" Content-Type: {response.headers.get('Content-Type', 'None')}")
print(f" 分析: {'存在Basic认证头' if 'Basic' in response.headers.get('WWW-Authenticate', '') else '无Basic认证头'}")
print("\n" + "=" * 60)
print("测试结论:")
print("=" * 60)
print("如果 /api/auth/** 端点正常工作,但其他端点返回401,")
print("则说明SecurityConfig配置存在问题。")
print("可能的原因:")
print("1. permitAll()配置未生效")
print("2. 默认Basic认证仍在起作用")
print("3. 路径匹配器配置错误")
if __name__ == "__main__":
import asyncio
asyncio.run(test_security_config())
+36
View File
@@ -0,0 +1,36 @@
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
pythonpath = .
addopts =
-v
--strict-markers
--tb=short
--cov=.
--cov-report=html
--cov-report=term-missing
--alluredir=allure-results
markers =
auth: 认证相关测试
user: 用户管理测试
role: 角色管理测试
permission: 权限管理测试
menu: 菜单管理测试
websocket: WebSocket实时通信测试
e2e: 端到端业务流程测试
example: 示例测试
performance: 性能测试
exception: 异常场景测试
dictionary: 字典管理测试
dict: 字典管理测试
config: 系统配置测试
audit: 审计日志测试
notice: 通知公告测试
file: 文件管理测试
smoke: 冒烟测试
regression: 回归测试
slow: 慢速测试
playwright: Playwright浏览器自动化测试
asyncio_mode = auto
File diff suppressed because one or more lines are too long
+29
View File
@@ -0,0 +1,29 @@
# Python依赖包
# 测试框架
pytest==7.4.3
pytest-asyncio==0.21.1
pytest-cov==4.1.0
pytest-xdist==3.5.0
# Playwright
playwright==1.40.0
# HTTP客户端
httpx==0.25.2
requests==2.31.0
# 数据处理
pydantic==2.5.2
pydantic-settings==2.1.0
faker==20.1.0
# 配置管理
python-dotenv==1.0.0
pyyaml==6.0.1
# 测试报告
allure-pytest==2.13.2
# 工具库
loguru==0.7.2
+22
View File
@@ -0,0 +1,22 @@
import asyncio
from httpx import AsyncClient
async def test():
async with AsyncClient(base_url='http://localhost:8080') as client:
# 先登录获取token
login_resp = await client.post('/api/auth/login', json={'username': 'admin', 'password': 'admin123'})
print('Login status:', login_resp.status_code)
if login_resp.status_code == 200:
token = login_resp.json().get('token')
print('Token:', token[:20] if token else 'None')
# 测试分页API
headers = {'Authorization': f'Bearer {token}'}
page_resp = await client.get('/api/logs/login/page?page=0&size=10', headers=headers)
print('Page API status:', page_resp.status_code)
if page_resp.status_code != 200:
print('Error response:', page_resp.text[:500])
else:
print('Success:', page_resp.json())
asyncio.run(test())
+22
View File
@@ -0,0 +1,22 @@
import asyncio
from httpx import AsyncClient
async def test():
async with AsyncClient(base_url='http://localhost:8080') as client:
# 先登录获取token
login_resp = await client.post('/api/auth/login', json={'username': 'admin', 'password': 'admin123'})
print('Login status:', login_resp.status_code)
if login_resp.status_code == 200:
token = login_resp.json().get('token')
print('Token:', token[:20] if token else 'None')
# 测试分页API - 使用正确的参数格式
headers = {'Authorization': f'Bearer {token}'}
page_resp = await client.get('/api/logs/login/page', params={'page': 0, 'size': 10}, headers=headers)
print('Page API status:', page_resp.status_code)
if page_resp.status_code != 200:
print('Error response:', page_resp.text[:500])
else:
print('Success:', page_resp.json())
asyncio.run(test())
+22
View File
@@ -0,0 +1,22 @@
import asyncio
from httpx import AsyncClient
async def test():
async with AsyncClient(base_url='http://localhost:8080') as client:
# 先登录获取token
login_resp = await client.post('/api/auth/login', json={'username': 'admin', 'password': 'admin123'})
print('Login status:', login_resp.status_code)
if login_resp.status_code == 200:
token = login_resp.json().get('token')
print('Token:', token[:20] if token else 'None')
# 测试分页API - 使用正确的参数格式
headers = {'Authorization': f'Bearer {token}'}
page_resp = await client.get('/api/logs/login/page', params={'page': 0, 'size': 10}, headers=headers)
print('Page API status:', page_resp.status_code)
if page_resp.status_code != 200:
print('Error response:', page_resp.text[:1000])
else:
print('Success:', page_resp.json())
asyncio.run(test())
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
import httpx
import asyncio
import json
async def test_upload():
base_url = "http://localhost:8080"
# 先登录获取token
login_url = f"{base_url}/api/auth/login"
login_data = {
"username": "admin",
"password": "admin123"
}
async with httpx.AsyncClient() as client:
# 登录
login_response = await client.post(login_url, json=login_data)
print(f"Login Status: {login_response.status_code}")
if login_response.status_code == 200:
token_data = login_response.json()
token = token_data.get("token")
print(f"Got token: {token[:20]}...")
# 上传文件
upload_url = f"{base_url}/api/files/upload"
# 创建测试文件
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("This is a test file content")
# 准备文件和数据
files = {
"file": ("test_file.txt", open(test_file_path, "rb"), "multipart/form-data")
}
headers = {"Authorization": f"Bearer {token}"}
# 发送请求
response = await client.post(upload_url, files=files, headers=headers)
print(f"\nUpload Status Code: {response.status_code}")
print(f"Response Headers: {dict(response.headers)}")
print(f"Response Body: {response.text}")
# 清理
import os
os.remove(test_file_path)
else:
print(f"Login failed: {login_response.text}")
if __name__ == "__main__":
asyncio.run(test_upload())
+1
View File
@@ -0,0 +1 @@
"""测试模块"""
+218
View File
@@ -0,0 +1,218 @@
"""
审计日志测试用例
"""
import pytest
import time
from api.audit_api import SysLogAPI
@pytest.mark.audit
@pytest.mark.regression
class TestLoginLog:
"""登录日志测试类"""
@pytest.mark.asyncio
async def test_create_login_log(self, authenticated_client):
"""测试创建登录日志"""
api = SysLogAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"username": f"testuser_{timestamp}",
"ip": "127.0.0.1",
"loginLocation": "本地",
"browser": "Chrome",
"os": "Mac OS",
"status": "0",
"msg": "登录成功"
}
response = await api.create_login_log(data)
assert response.status_code == 201
result = response.json()
assert result["username"] == data["username"]
@pytest.mark.asyncio
async def test_get_all_login_logs(self, authenticated_client):
"""测试获取所有登录日志"""
api = SysLogAPI(authenticated_client)
response = await api.get_login_logs()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_login_log_by_id(self, authenticated_client):
"""测试根据ID获取登录日志"""
api = SysLogAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"username": f"testuser_{timestamp}",
"ip": "127.0.0.1",
"status": "0",
"msg": "登录成功"
}
create_response = await api.create_login_log(data)
log_id = create_response.json()["id"]
response = await api.get_login_log_by_id(log_id)
assert response.status_code == 200
assert response.json()["id"] == log_id
@pytest.mark.audit
@pytest.mark.regression
class TestExceptionLog:
"""异常日志测试类"""
@pytest.mark.asyncio
async def test_create_exception_log(self, authenticated_client):
"""测试创建异常日志"""
api = SysLogAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"title": f"测试异常_{timestamp}",
"exceptionName": "NullPointerException",
"exceptionMsg": "Null pointer at line 100",
"methodName": "cn.novalon.manage.sys.service.UserService.getUser",
"ip": "127.0.0.1",
"exceptionStack": "java.lang.NullPointerException\\n at..."
}
response = await api.create_exception_log(data)
assert response.status_code == 201
result = response.json()
assert result["title"] == data["title"]
@pytest.mark.asyncio
async def test_get_all_exception_logs(self, authenticated_client):
"""测试获取所有异常日志"""
api = SysLogAPI(authenticated_client)
response = await api.get_exception_logs()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_exception_log_by_id(self, authenticated_client):
"""测试根据ID获取异常日志"""
api = SysLogAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"title": f"测试异常_{timestamp}",
"exceptionName": "NullPointerException",
"exceptionMsg": "Null pointer"
}
create_response = await api.create_exception_log(data)
log_id = create_response.json()["id"]
response = await api.get_exception_log_by_id(log_id)
assert response.status_code == 200
assert response.json()["id"] == log_id
@pytest.mark.asyncio
async def test_get_login_logs_by_page_success(self, authenticated_client):
"""测试分页获取登录日志成功"""
api = SysLogAPI(authenticated_client)
for i in range(5):
timestamp = int(time.time() * 1000) + i
data = {
"username": f"testuser_{i}",
"ip": f"127.0.0.{i}",
"status": "0",
"msg": "登录成功"
}
await api.create_login_log(data)
response = await api.get_login_logs_by_page(page=0, size=10)
assert response.status_code == 200
data = response.json()
assert "content" in data
assert "totalElements" in data
assert "totalPages" in data
assert "currentPage" in data
assert "pageSize" in data
assert len(data["content"]) <= 10
@pytest.mark.asyncio
async def test_get_login_logs_by_page_with_sort(self, authenticated_client):
"""测试分页获取登录日志并排序成功"""
api = SysLogAPI(authenticated_client)
for i in range(3):
timestamp = int(time.time() * 1000) + i
data = {
"username": f"sortuser_{i}",
"ip": "127.0.0.1",
"status": "0",
"msg": "登录成功"
}
await api.create_login_log(data)
response = await api.get_login_logs_by_page(page=0, size=10, sort="username", order="asc")
assert response.status_code == 200
data = response.json()
usernames = [log["username"] for log in data["content"]]
assert usernames == sorted(usernames)
@pytest.mark.asyncio
async def test_get_login_logs_by_page_with_search(self, authenticated_client):
"""测试分页获取登录日志并搜索成功"""
api = SysLogAPI(authenticated_client)
timestamp1 = int(time.time() * 1000)
data1 = {
"username": "search_test_user",
"ip": "127.0.0.1",
"status": "0",
"msg": "登录成功"
}
await api.create_login_log(data1)
timestamp2 = int(time.time() * 1000) + 1
data2 = {
"username": "other_user",
"ip": "127.0.0.2",
"status": "0",
"msg": "登录成功"
}
await api.create_login_log(data2)
response = await api.get_login_logs_by_page(page=0, size=10, keyword="search")
assert response.status_code == 200
data = response.json()
assert len(data["content"]) >= 1
assert all("search" in log["username"] or "search" in log.get("ip", "")
for log in data["content"])
@pytest.mark.asyncio
async def test_get_login_log_count_success(self, authenticated_client):
"""测试获取登录日志总数成功"""
api = SysLogAPI(authenticated_client)
initial_count_response = await api.get_login_log_count()
initial_count = initial_count_response.json()
timestamp = int(time.time() * 1000)
data = {
"username": f"count_test_user",
"ip": "127.0.0.1",
"status": "0",
"msg": "登录成功"
}
await api.create_login_log(data)
final_count_response = await api.get_login_log_count()
final_count = final_count_response.json()
assert final_count == initial_count + 1
+78
View File
@@ -0,0 +1,78 @@
"""
认证测试用例
"""
import pytest
from api.auth_api import AuthAPI
from config.settings import settings
@pytest.mark.auth
@pytest.mark.smoke
class TestAuth:
"""认证测试类"""
@pytest.mark.asyncio
async def test_login_success(self, http_client):
"""测试成功登录"""
auth_api = AuthAPI(http_client)
response = await auth_api.login(settings.TEST_USERNAME, settings.TEST_PASSWORD)
assert response.status_code == 200
data = response.json()
assert "token" in data
assert isinstance(data["token"], str)
assert "userId" in data
assert "username" in data
@pytest.mark.asyncio
async def test_login_invalid_credentials(self, http_client):
"""测试无效凭证登录"""
auth_api = AuthAPI(http_client)
response = await auth_api.login("invalid_user", "invalid_password")
assert response.status_code == 401
@pytest.mark.asyncio
async def test_login_missing_fields(self, http_client):
"""测试缺少必填字段"""
auth_api = AuthAPI(http_client)
response = await http_client.post("/api/auth/login", json={
"username": "test"
})
assert response.status_code == 400
@pytest.mark.asyncio
async def test_register_success(self, http_client):
"""测试注册成功"""
import time
timestamp = int(time.time() * 1000)
response = await http_client.post("/api/auth/register", json={
"username": f"testuser_{timestamp}",
"password": "password123",
"email": f"test_{timestamp}@example.com"
})
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["username"] == f"testuser_{timestamp}"
@pytest.mark.asyncio
async def test_register_duplicate_username(self, http_client):
"""测试注册重复用户名"""
response = await http_client.post("/api/auth/register", json={
"username": "admin",
"password": "password123",
"email": "admin@example.com"
})
assert response.status_code == 500
@pytest.mark.asyncio
async def test_logout_success(self, http_client):
"""测试登出成功"""
response = await http_client.post("/api/auth/logout")
assert response.status_code == 200
+105
View File
@@ -0,0 +1,105 @@
"""
系统配置测试用例
"""
import pytest
import time
from api.config_api import SysConfigAPI
@pytest.mark.config
@pytest.mark.regression
class TestSysConfig:
"""系统参数配置测试类"""
@pytest.mark.asyncio
async def test_create_config_success(self, authenticated_client):
"""测试创建系统配置成功"""
api = SysConfigAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"configName": f"测试配置_{timestamp}",
"configKey": f"test.config.key.{timestamp}",
"configValue": "test_value",
"configType": "N"
}
response = await api.create(data)
assert response.status_code == 201
result = response.json()
assert result["configName"] == data["configName"]
assert result["configKey"] == data["configKey"]
@pytest.mark.asyncio
async def test_get_all_configs(self, authenticated_client):
"""测试获取所有配置"""
api = SysConfigAPI(authenticated_client)
response = await api.get_all()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_config_by_key(self, authenticated_client):
"""测试根据key获取配置"""
api = SysConfigAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"configName": f"测试配置_{timestamp}",
"configKey": f"test.config.key.{timestamp}",
"configValue": "test_value",
"configType": "N"
}
create_response = await api.create(data)
config_key = data["configKey"]
response = await api.get_by_key(config_key)
assert response.status_code == 200
result = response.json()
assert result["configKey"] == config_key
@pytest.mark.asyncio
async def test_update_config(self, authenticated_client):
"""测试更新配置"""
api = SysConfigAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"configName": f"测试配置_{timestamp}",
"configKey": f"test.config.key.{timestamp}",
"configValue": "old_value",
"configType": "N"
}
create_response = await api.create(data)
config_id = create_response.json()["id"]
update_data = {
"configName": f"更新后_{timestamp}",
"configKey": f"test.config.key.{timestamp}",
"configValue": "new_value",
"configType": "N"
}
response = await api.update(config_id, update_data)
assert response.status_code == 200
assert response.json()["configValue"] == "new_value"
@pytest.mark.asyncio
async def test_delete_config(self, authenticated_client):
"""测试删除配置"""
api = SysConfigAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"configName": f"测试配置_{timestamp}",
"configKey": f"test.config.key.{timestamp}",
"configValue": "test_value",
"configType": "N"
}
create_response = await api.create(data)
config_id = create_response.json()["id"]
response = await api.delete(config_id)
assert response.status_code == 204
@@ -0,0 +1,91 @@
"""
测试数据管理器使用示例
"""
import pytest
import time
from api.user_api import UserAPI
from api.role_api import RoleAPI
@pytest.mark.example
@pytest.mark.regression
class TestDataManagerExample:
"""测试数据管理器使用示例"""
@pytest.mark.asyncio
async def test_create_and_cleanup_users(self, authenticated_client, test_data_manager):
"""演示测试数据管理器的使用"""
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
for i in range(3):
user_data = {
"username": f"managed_user_{timestamp}_{i}",
"password": "Test123!@#",
"email": f"managed_{timestamp}_{i}@example.com",
"status": 1
}
response = await user_api.create_user(user_data)
assert response.status_code == 201
user_id = response.json()["id"]
test_data_manager.add_user(user_id)
cleanup_count = test_data_manager.get_stats()
assert cleanup_count["users"] == 3
all_users = await user_api.get_all_users()
assert all_users.status_code == 200
await test_data_manager.cleanup_all()
final_count = test_data_manager.get_stats()
assert final_count["users"] == 0
@pytest.mark.asyncio
async def test_multiple_resources_cleanup(self, authenticated_client, test_data_manager):
"""演示多资源清理"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"Managed_Role_{timestamp}",
"roleKey": f"managed_role_{timestamp}",
"roleSort": 1,
"status": 1
}
role_response = await role_api.create_role(role_data)
assert role_response.status_code == 201
role_id = role_response.json()["id"]
test_data_manager.add_role(role_id)
for i in range(2):
user_data = {
"username": f"role_user_{timestamp}_{i}",
"password": "Test123!@#",
"email": f"role_user_{timestamp}_{i}@example.com",
"status": 1
}
user_response = await user_api.create_user(user_data)
assert user_response.status_code == 201
user_id = user_response.json()["id"]
test_data_manager.add_user(user_id)
await user_api.update_user(user_id, {"roleId": role_id})
cleanup_count = test_data_manager.get_stats()
assert cleanup_count["roles"] == 1
assert cleanup_count["users"] == 2
await test_data_manager.cleanup_all()
final_count = test_data_manager.get_stats()
assert final_count["roles"] == 0
assert final_count["users"] == 0
+164
View File
@@ -0,0 +1,164 @@
"""
字典管理测试用例
"""
import pytest
import time
from api.dict_api import DictTypeAPI, DictDataAPI
@pytest.mark.dict
@pytest.mark.regression
class TestDictType:
"""字典类型测试类"""
@pytest.mark.asyncio
async def test_create_dict_type_success(self, authenticated_client):
"""测试创建字典类型成功"""
api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"dictName": f"测试字典_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
response = await api.create(data)
assert response.status_code == 201
result = response.json()
assert result["dictName"] == data["dictName"]
assert result["dictType"] == data["dictType"]
@pytest.mark.asyncio
async def test_get_all_dict_types(self, authenticated_client):
"""测试获取所有字典类型"""
api = DictTypeAPI(authenticated_client)
response = await api.get_all()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_dict_type_by_id(self, authenticated_client):
"""测试根据ID获取字典类型"""
api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
create_data = {
"dictName": f"测试字典_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
create_response = await api.create(create_data)
dict_id = create_response.json()["id"]
response = await api.get_by_id(dict_id)
assert response.status_code == 200
assert response.json()["id"] == dict_id
@pytest.mark.asyncio
async def test_update_dict_type(self, authenticated_client):
"""测试更新字典类型"""
api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
create_data = {
"dictName": f"测试字典_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
create_response = await api.create(create_data)
dict_id = create_response.json()["id"]
update_data = {
"dictName": f"更新后_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
response = await api.update(dict_id, update_data)
assert response.status_code == 200
assert response.json()["dictName"] == f"更新后_{timestamp}"
@pytest.mark.asyncio
async def test_delete_dict_type(self, authenticated_client):
"""测试删除字典类型"""
api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
create_data = {
"dictName": f"测试字典_{timestamp}",
"dictType": f"test_{timestamp}",
"status": "0"
}
create_response = await api.create(create_data)
dict_id = create_response.json()["id"]
response = await api.delete(dict_id)
assert response.status_code == 204
@pytest.mark.dict
@pytest.mark.regression
class TestDictData:
"""字典数据测试类"""
@pytest.mark.asyncio
async def test_create_dict_data_success(self, authenticated_client):
"""测试创建字典数据成功"""
dict_type_api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
dict_type_data = {
"dictName": f"测试字典类型_{timestamp}",
"dictType": f"test_type_{timestamp}",
"status": "0"
}
dict_type_response = await dict_type_api.create(dict_type_data)
dict_type_id = dict_type_response.json()["id"]
dict_data_api = DictDataAPI(authenticated_client)
data = {
"dictSort": 1,
"dictLabel": f"测试标签_{timestamp}",
"dictValue": f"test_value_{timestamp}",
"dictType": f"test_type_{timestamp}",
"status": "0"
}
response = await dict_data_api.create(data)
assert response.status_code == 201
result = response.json()
assert result["dictLabel"] == data["dictLabel"]
assert result["dictValue"] == data["dictValue"]
@pytest.mark.asyncio
async def test_get_dict_data_by_type(self, authenticated_client):
"""测试根据类型获取字典数据"""
dict_type_api = DictTypeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
dict_type = f"test_type_{timestamp}"
dict_type_data = {
"dictName": f"测试字典类型_{timestamp}",
"dictType": dict_type,
"status": "0"
}
await dict_type_api.create(dict_type_data)
dict_data_api = DictDataAPI(authenticated_client)
data = {
"dictSort": 1,
"dictLabel": f"测试标签_{timestamp}",
"dictValue": f"test_value_{timestamp}",
"dictType": dict_type,
"status": "0"
}
await dict_data_api.create(data)
response = await dict_data_api.get_by_type(dict_type)
assert response.status_code == 200
result = response.json()
assert len(result) > 0
assert result[0]["dictType"] == dict_type
@@ -0,0 +1,149 @@
"""
字典管理测试用例
"""
import pytest
from api.dictionary_api import DictionaryAPI
@pytest.mark.dictionary
@pytest.mark.regression
class TestDictionary:
"""字典管理测试类"""
@pytest.mark.asyncio
async def test_create_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["type"] == test_dictionary_data["type"]
assert data["code"] == test_dictionary_data["code"]
assert data["name"] == test_dictionary_data["name"]
cleanup_dictionary.append(data["id"])
@pytest.mark.asyncio
async def test_create_dictionary_duplicate_type_code(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试创建重复类型和编码"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.create_dictionary(test_dictionary_data)
assert response.status_code in [400, 409]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据ID获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionary_by_id(dict_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == dict_id
assert data["type"] == test_dictionary_data["type"]
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_dictionary_by_id_not_found(self, authenticated_client):
"""测试获取不存在的字典"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_dictionary_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_dictionaries_by_type_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试根据类型获取字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.get_dictionaries_by_type(test_dictionary_data["type"])
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert any(d["id"] == dict_id for d in data)
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_get_all_dictionaries_success(self, authenticated_client):
"""测试获取所有字典成功"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.get_all_dictionaries()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试更新字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
update_data = {"name": "Updated name"}
response = await dict_api.update_dictionary(dict_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated name"
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_delete_dictionary_success(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试删除字典成功"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.delete_dictionary(dict_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_check_type_and_code_exists_true(self, authenticated_client, test_dictionary_data, cleanup_dictionary):
"""测试检查类型和编码存在-返回true"""
dict_api = DictionaryAPI(authenticated_client)
create_response = await dict_api.create_dictionary(test_dictionary_data)
dict_id = create_response.json()["id"]
response = await dict_api.check_type_and_code_exists(
test_dictionary_data["type"],
test_dictionary_data["code"]
)
assert response.status_code == 200
assert response.json() is True
cleanup_dictionary.append(dict_id)
@pytest.mark.asyncio
async def test_check_type_and_code_exists_false(self, authenticated_client):
"""测试检查类型和编码存在-返回false"""
dict_api = DictionaryAPI(authenticated_client)
response = await dict_api.check_type_and_code_exists("NONEXISTENT_TYPE", "NONEXISTENT_CODE")
assert response.status_code == 200
assert response.json() is False
+311
View File
@@ -0,0 +1,311 @@
"""
端到端业务流程测试用例
"""
import pytest
import time
from api.auth_api import AuthAPI
from api.user_api import UserAPI
from api.role_api import RoleAPI
from api.notice_api import SysNoticeAPI
@pytest.mark.e2e
@pytest.mark.regression
class TestBusinessFlow:
"""端到端业务流程测试类"""
@pytest.mark.asyncio
async def test_complete_user_lifecycle(self, authenticated_client):
"""测试完整用户生命周期"""
auth_api = AuthAPI(authenticated_client)
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
new_user_data = {
"username": f"e2e_user_{timestamp}",
"password": "Test123!@#",
"email": f"e2e_{timestamp}@example.com",
"phone": "13800138000",
"status": 1
}
create_response = await user_api.create_user(new_user_data)
assert create_response.status_code == 201
user_id = create_response.json()["id"]
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 200
user_data = get_response.json()
assert user_data["username"] == new_user_data["username"]
update_data = {"email": f"updated_{timestamp}@example.com"}
update_response = await user_api.update_user(user_id, update_data)
assert update_response.status_code == 200
delete_response = await user_api.delete_user(user_id)
assert delete_response.status_code in [200, 204]
final_get_response = await user_api.get_user_by_id(user_id)
assert final_get_response.status_code == 404
@pytest.mark.asyncio
async def test_role_assignment_workflow(self, authenticated_client):
"""测试角色分配工作流"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"E2E_Role_{timestamp}",
"roleKey": f"e2e_role_{timestamp}",
"roleSort": 1,
"status": 1
}
role_response = await role_api.create_role(role_data)
assert role_response.status_code == 201
role_id = role_response.json()["id"]
user_data = {
"username": f"e2e_user_{timestamp}",
"password": "Test123!@#",
"email": f"e2e_{timestamp}@example.com",
"status": 1
}
user_response = await user_api.create_user(user_data)
assert user_response.status_code == 201
user_id = user_response.json()["id"]
assign_response = await user_api.update_user(user_id, {"roleId": role_id})
assert assign_response.status_code == 200
verify_response = await user_api.get_user_by_id(user_id)
assert verify_response.json()["roleId"] == role_id
await user_api.delete_user(user_id)
await role_api.delete_role(role_id)
@pytest.mark.asyncio
async def test_notification_workflow(self, authenticated_client):
"""测试通知工作流"""
notice_api = SysNoticeAPI(authenticated_client)
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
notice_data = {
"noticeTitle": f"E2E_Notice_{timestamp}",
"noticeType": "1",
"noticeContent": "This is an E2E test notice",
"status": "0"
}
create_response = await notice_api.create(notice_data)
assert create_response.status_code == 201
notice_data_response = create_response.json()
notice_id = notice_data_response.get("id")
if not notice_id:
notice_title = notice_data_response.get("noticeTitle")
all_notices = await notice_api.get_all()
notices = all_notices.json()
notice = next((n for n in notices if n["noticeTitle"] == notice_title), None)
notice_id = notice["id"] if notice else None
assert notice_id is not None
get_response = await notice_api.get_by_id(notice_id)
assert get_response.status_code == 200
all_notices = await notice_api.get_all()
assert all_notices.status_code == 200
notices = all_notices.json()
assert any(notice["id"] == notice_id for notice in notices)
update_data = {"noticeTitle": f"Updated_Notice_{timestamp}"}
update_response = await notice_api.update(notice_id, update_data)
assert update_response.status_code == 200
await notice_api.delete(notice_id)
final_get = await notice_api.get_by_id(notice_id)
assert final_get.status_code == 404
@pytest.mark.asyncio
async def test_multi_role_user_management(self, authenticated_client):
"""测试多角色用户管理"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
timestamp = int(time.time() * 1000)
admin_role_data = {
"roleName": f"Admin_{timestamp}",
"roleKey": f"admin_{timestamp}",
"roleSort": 1,
"status": 1
}
admin_role = await role_api.create_role(admin_role_data)
admin_role_id = admin_role.json()["id"]
user_role_data = {
"roleName": f"User_{timestamp}",
"roleKey": f"user_{timestamp}",
"roleSort": 2,
"status": 1
}
user_role = await role_api.create_role(user_role_data)
user_role_id = user_role.json()["id"]
admin_user_data = {
"username": f"admin_{timestamp}",
"password": "Admin123!@#",
"email": f"admin_{timestamp}@example.com",
"status": 1
}
admin_user = await user_api.create_user(admin_user_data)
admin_user_id = admin_user.json()["id"]
regular_user_data = {
"username": f"regular_{timestamp}",
"password": "User123!@#",
"email": f"regular_{timestamp}@example.com",
"status": 1
}
regular_user = await user_api.create_user(regular_user_data)
regular_user_id = regular_user.json()["id"]
await user_api.update_user(admin_user_id, {"roleId": admin_role_id})
await user_api.update_user(regular_user_id, {"roleId": user_role_id})
admin_verify = await user_api.get_user_by_id(admin_user_id)
assert admin_verify.json()["roleId"] == admin_role_id
regular_verify = await user_api.get_user_by_id(regular_user_id)
assert regular_verify.json()["roleId"] == user_role_id
all_users = await user_api.get_all_users()
users = all_users.json()
assert len(users) >= 2
await user_api.delete_user(admin_user_id)
await user_api.delete_user(regular_user_id)
await role_api.delete_role(admin_role_id)
await role_api.delete_role(user_role_id)
@pytest.mark.asyncio
async def test_user_role_cascade_operations(self, authenticated_client):
"""测试用户角色级联操作"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"Cascade_Role_{timestamp}",
"roleKey": f"cascade_role_{timestamp}",
"roleSort": 1,
"status": 1
}
role_response = await role_api.create_role(role_data)
role_id = role_response.json()["id"]
user_ids = []
for i in range(3):
user_data = {
"username": f"cascade_user_{timestamp}_{i}",
"password": "Test123!@#",
"email": f"cascade_{timestamp}_{i}@example.com",
"status": 1
}
user_response = await user_api.create_user(user_data)
user_id = user_response.json()["id"]
user_ids.append(user_id)
await user_api.update_user(user_id, {"roleId": role_id})
await role_api.update_role(role_id, {"status": 0})
for user_id in user_ids:
user_data = await user_api.get_user_by_id(user_id)
assert user_data.json()["roleId"] == role_id
for user_id in user_ids:
await user_api.delete_user(user_id)
await role_api.delete_role(role_id)
@pytest.mark.asyncio
async def test_search_and_filter_workflow(self, authenticated_client):
"""测试搜索和过滤工作流"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"Search_Role_{timestamp}",
"roleKey": f"search_role_{timestamp}",
"roleSort": 1,
"status": 1
}
role_response = await role_api.create_role(role_data)
role_id = role_response.json()["id"]
user_ids = []
for i in range(5):
user_data = {
"username": f"search_{timestamp}_{i}",
"password": "Test123!@#",
"email": f"search_{timestamp}_{i}@example.com",
"status": 1
}
user_response = await user_api.create_user(user_data)
user_id = user_response.json()["id"]
user_ids.append(user_id)
search_response = await user_api.get_users_by_page(keyword=f"search_{timestamp}")
assert search_response.status_code == 200
search_data = search_response.json()
assert len(search_data["content"]) >= 5
all_users = await user_api.get_all_users()
assert all_users.status_code == 200
for user_id in user_ids:
await user_api.delete_user(user_id)
await role_api.delete_role(role_id)
@pytest.mark.asyncio
async def test_error_recovery_workflow(self, authenticated_client):
"""测试错误恢复工作流"""
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
invalid_user_data = {
"username": "",
"password": "123",
"email": "invalid-email"
}
invalid_response = await user_api.create_user(invalid_user_data)
assert invalid_response.status_code in [400, 409, 422]
valid_user_data = {
"username": f"recovery_{timestamp}",
"password": "Valid123!@#",
"email": f"recovery_{timestamp}@example.com",
"status": 1
}
valid_response = await user_api.create_user(valid_user_data)
assert valid_response.status_code == 201
user_id = valid_response.json()["id"]
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 200
await user_api.delete_user(user_id)
@@ -0,0 +1,331 @@
"""
异常场景测试用例
"""
import pytest
import time
from api.user_api import UserAPI
from api.role_api import RoleAPI
from api.notice_api import SysNoticeAPI
@pytest.mark.exception
@pytest.mark.regression
class TestExceptionScenarios:
"""异常场景测试类"""
@pytest.mark.asyncio
async def test_create_user_with_duplicate_username(self, authenticated_client, test_user_data, cleanup_user):
"""测试创建重复用户名的用户"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
assert create_response.status_code == 201
user_id = create_response.json()["id"]
cleanup_user.append(user_id)
duplicate_response = await user_api.create_user(test_user_data)
assert duplicate_response.status_code in [400, 409]
@pytest.mark.asyncio
async def test_create_user_with_invalid_email(self, authenticated_client):
"""测试创建邮箱格式无效的用户"""
user_api = UserAPI(authenticated_client)
invalid_emails = [
"invalid-email",
"@example.com",
"user@",
"user@domain",
"user name@example.com"
]
for invalid_email in invalid_emails:
timestamp = int(time.time() * 1000)
user_data = {
"username": f"test_{timestamp}",
"password": "Test123!@#",
"email": invalid_email,
"status": 1
}
response = await user_api.create_user(user_data)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_create_user_with_weak_password(self, authenticated_client):
"""测试创建弱密码用户"""
user_api = UserAPI(authenticated_client)
weak_passwords = [
"123456",
"password",
"qwerty",
"111111",
"abc123"
]
for weak_password in weak_passwords:
timestamp = int(time.time() * 1000)
user_data = {
"username": f"test_{timestamp}",
"password": weak_password,
"email": f"test_{timestamp}@example.com",
"status": 1
}
response = await user_api.create_user(user_data)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_create_user_with_missing_fields(self, authenticated_client):
"""测试创建缺少必填字段的用户"""
user_api = UserAPI(authenticated_client)
missing_field_scenarios = [
{"password": "Test123!@#", "email": "test@example.com"},
{"username": "testuser", "email": "test@example.com"},
{"username": "testuser", "password": "Test123!@#"}
]
for scenario in missing_field_scenarios:
response = await user_api.create_user(scenario)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_update_nonexistent_user(self, authenticated_client):
"""测试更新不存在的用户"""
user_api = UserAPI(authenticated_client)
update_data = {"email": "updated@example.com"}
response = await user_api.update_user(999999, update_data)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_delete_nonexistent_user(self, authenticated_client):
"""测试删除不存在的用户"""
user_api = UserAPI(authenticated_client)
response = await user_api.delete_user(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_create_role_with_duplicate_key(self, authenticated_client, test_role_data, cleanup_role):
"""测试创建重复角色键的角色"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
assert create_response.status_code == 201
role_id = create_response.json()["id"]
cleanup_role.append(role_id)
duplicate_response = await role_api.create_role(test_role_data)
assert duplicate_response.status_code in [400, 409]
@pytest.mark.asyncio
async def test_create_role_with_invalid_status(self, authenticated_client):
"""测试创建状态无效的角色"""
role_api = RoleAPI(authenticated_client)
invalid_statuses = ["2", "3", "invalid", "true", "false"]
for invalid_status in invalid_statuses:
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"TestRole_{timestamp}",
"roleKey": f"test_role_{timestamp}",
"roleSort": 1,
"status": invalid_status
}
response = await role_api.create_role(role_data)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_update_nonexistent_role(self, authenticated_client):
"""测试更新不存在的角色"""
role_api = RoleAPI(authenticated_client)
update_data = {"roleName": "Updated Role"}
response = await role_api.update_role(999999, update_data)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_create_notice_with_invalid_type(self, authenticated_client):
"""测试创建类型无效的公告"""
notice_api = SysNoticeAPI(authenticated_client)
invalid_types = ["3", "4", "invalid", "true", "false"]
for invalid_type in invalid_types:
timestamp = int(time.time() * 1000)
notice_data = {
"noticeTitle": f"TestNotice_{timestamp}",
"noticeType": invalid_type,
"noticeContent": "Test content",
"status": "0"
}
response = await notice_api.create(notice_data)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_create_notice_with_empty_content(self, authenticated_client):
"""测试创建内容为空的公告"""
notice_api = SysNoticeAPI(authenticated_client)
empty_content_scenarios = [
{"noticeTitle": "Test", "noticeType": "1", "noticeContent": "", "status": "0"},
{"noticeTitle": "", "noticeType": "1", "noticeContent": "Test", "status": "0"},
{"noticeTitle": "Test", "noticeType": "1", "noticeContent": " ", "status": "0"}
]
for scenario in empty_content_scenarios:
response = await notice_api.create(scenario)
assert response.status_code in [400, 422]
@pytest.mark.asyncio
async def test_update_nonexistent_notice(self, authenticated_client):
"""测试更新不存在的公告"""
notice_api = SysNoticeAPI(authenticated_client)
update_data = {"noticeTitle": "Updated Notice"}
response = await notice_api.update(999999, update_data)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_delete_nonexistent_notice(self, authenticated_client):
"""测试删除不存在的公告"""
notice_api = SysNoticeAPI(authenticated_client)
response = await notice_api.delete(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_user_with_invalid_id(self, authenticated_client):
"""测试获取ID无效的用户"""
user_api = UserAPI(authenticated_client)
invalid_ids = [-1, 0, "abc", "1.5", "999999999999"]
for invalid_id in invalid_ids:
try:
response = await user_api.get_user_by_id(int(invalid_id) if isinstance(invalid_id, (int, str)) else invalid_id)
assert response.status_code in [400, 404]
except (ValueError, TypeError):
pass
@pytest.mark.asyncio
async def test_pagination_with_invalid_params(self, authenticated_client):
"""测试分页参数无效的查询"""
user_api = UserAPI(authenticated_client)
invalid_params = [
{"page": -1, "size": 10},
{"page": 0, "size": -1},
{"page": 0, "size": 0},
{"page": 0, "size": 10000},
{"page": "abc", "size": 10},
{"page": 0, "size": "abc"}
]
for params in invalid_params:
try:
response = await user_api.get_users_by_page(**params)
assert response.status_code in [400, 422]
except Exception:
pass
@pytest.mark.asyncio
async def test_search_with_special_characters(self, authenticated_client):
"""测试搜索特殊字符"""
user_api = UserAPI(authenticated_client)
special_chars = [
"<script>alert('xss')</script>",
"'; DROP TABLE users; --",
"../../../etc/passwd",
"{{7*7}}",
"%00%00%00%00"
]
for search_term in special_chars:
response = await user_api.get_users_by_page(keyword=search_term)
assert response.status_code in [200, 400]
if response.status_code == 200:
data = response.json()
assert "content" in data
for user in data["content"]:
assert search_term.lower() not in str(user).lower()
@pytest.mark.asyncio
async def test_concurrent_same_resource_update(self, authenticated_client, test_user_data, cleanup_user):
"""测试并发更新同一资源"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
assert create_response.status_code == 201
user_id = create_response.json()["id"]
cleanup_user.append(user_id)
import asyncio
update_tasks = [
user_api.update_user(user_id, {"email": f"concurrent1_{time.time()}@example.com"}),
user_api.update_user(user_id, {"email": f"concurrent2_{time.time()}@example.com"}),
user_api.update_user(user_id, {"email": f"concurrent3_{time.time()}@example.com"})
]
results = await asyncio.gather(*update_tasks, return_exceptions=True)
successful_updates = sum(1 for r in results if r.status_code == 200)
assert successful_updates >= 1, "至少应该有一个更新成功"
@pytest.mark.asyncio
async def test_large_payload_handling(self, authenticated_client):
"""测试大数据负载处理"""
user_api = UserAPI(authenticated_client)
large_content = "x" * 10000
user_data = {
"username": f"large_payload_{int(time.time() * 1000)}",
"password": "Test123!@#",
"email": f"large_{int(time.time() * 1000)}@example.com",
"phone": large_content
}
response = await user_api.create_user(user_data)
assert response.status_code in [201, 400, 413]
if response.status_code in [400, 413]:
logger.info("系统正确拒绝了过大的负载")
@pytest.mark.asyncio
async def test_unauthorized_access(self, http_client):
"""测试未授权访问"""
user_api = UserAPI(http_client)
response = await user_api.get_all_users()
assert response.status_code == 401
@pytest.mark.asyncio
async def test_rate_limiting(self, authenticated_client):
"""测试速率限制"""
user_api = UserAPI(authenticated_client)
requests_made = 0
rate_limit_hit = False
for i in range(100):
response = await user_api.get_all_users()
requests_made += 1
if response.status_code == 429:
rate_limit_hit = True
logger.info(f"速率限制在第 {requests_made} 个请求时触发")
break
if rate_limit_hit:
logger.info("系统正确实施了速率限制")
else:
logger.info("未触发速率限制(可能未配置或阈值较高)")
+114
View File
@@ -0,0 +1,114 @@
"""
文件管理测试用例
"""
import pytest
import os
import time
from api.file_api import SysFileAPI
@pytest.mark.file
@pytest.mark.regression
class TestSysFile:
"""文件管理测试类"""
@pytest.mark.asyncio
async def test_upload_file(self, authenticated_client):
"""测试文件上传"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("This is a test file content")
response = await api.upload(test_file_path, "test_user")
os.remove(test_file_path)
assert response.status_code == 201
result = response.json()
assert "id" in result
@pytest.mark.asyncio
async def test_get_all_files(self, authenticated_client):
"""测试获取所有文件"""
api = SysFileAPI(authenticated_client)
response = await api.get_all()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_file_by_id(self, authenticated_client):
"""测试根据ID获取文件"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Test content")
upload_response = await api.upload(test_file_path, "test_user")
file_id = upload_response.json()["id"]
os.remove(test_file_path)
response = await api.get_by_id(file_id)
assert response.status_code == 200
assert response.json()["id"] == file_id
@pytest.mark.asyncio
async def test_download_file(self, authenticated_client):
"""测试文件下载"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Download test content")
upload_response = await api.upload(test_file_path, "test_user")
file_name = upload_response.json()["filePath"].split("/")[-1]
os.remove(test_file_path)
response = await api.download(file_name)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_preview_file(self, authenticated_client):
"""测试文件预览"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Preview test content")
upload_response = await api.upload(test_file_path, "test_user")
file_name = upload_response.json()["filePath"].split("/")[-1]
os.remove(test_file_path)
response = await api.preview(file_name)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_file(self, authenticated_client):
"""测试删除文件"""
api = SysFileAPI(authenticated_client)
test_file_path = "/tmp/test_file.txt"
with open(test_file_path, "w") as f:
f.write("Delete test content")
upload_response = await api.upload(test_file_path, "test_user")
file_id = upload_response.json()["id"]
os.remove(test_file_path)
response = await api.delete(file_id)
assert response.status_code == 204
+242
View File
@@ -0,0 +1,242 @@
"""
菜单管理测试用例
"""
import pytest
import time
from api.menu_api import MenuAPI
@pytest.mark.menu
@pytest.mark.regression
class TestMenu:
"""菜单管理测试类"""
@pytest.fixture
def test_menu_data(self):
"""测试菜单数据"""
timestamp = int(time.time() * 1000)
return {
"menuName": f"测试菜单_{timestamp}",
"parentId": 0,
"orderNum": 1,
"menuType": "C",
"perms": f"system:menu:{timestamp}",
"component": f"menu/component/{timestamp}",
"status": "0"
}
@pytest.fixture
async def cleanup_menu(self, authenticated_client):
"""清理测试菜单"""
menu_ids = []
yield menu_ids
for menu_id in menu_ids:
try:
await authenticated_client.delete(f"/api/menus/{menu_id}")
except Exception:
pass
@pytest.mark.asyncio
async def test_create_menu_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试创建菜单成功"""
menu_api = MenuAPI(authenticated_client)
response = await menu_api.create_menu(test_menu_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["menuName"] == test_menu_data["menuName"]
assert data["parentId"] == test_menu_data["parentId"]
assert data["menuType"] == test_menu_data["menuType"]
cleanup_menu.append(data["id"])
@pytest.mark.asyncio
async def test_get_menu_by_id_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试根据ID获取菜单成功"""
menu_api = MenuAPI(authenticated_client)
create_response = await menu_api.create_menu(test_menu_data)
menu_id = create_response.json()["id"]
response = await menu_api.get_menu_by_id(menu_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == menu_id
assert data["menuName"] == test_menu_data["menuName"]
cleanup_menu.append(menu_id)
@pytest.mark.asyncio
async def test_get_menu_by_id_not_found(self, authenticated_client):
"""测试获取不存在的菜单"""
menu_api = MenuAPI(authenticated_client)
response = await menu_api.get_menu_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_all_menus_success(self, authenticated_client):
"""测试获取所有菜单成功"""
menu_api = MenuAPI(authenticated_client)
response = await menu_api.get_all_menus()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_get_menu_tree_success(self, authenticated_client):
"""测试获取菜单树成功"""
menu_api = MenuAPI(authenticated_client)
response = await menu_api.get_menu_tree()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_menu_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试更新菜单成功"""
menu_api = MenuAPI(authenticated_client)
create_response = await menu_api.create_menu(test_menu_data)
menu_id = create_response.json()["id"]
timestamp = int(time.time() * 1000)
update_data = {
"menuName": f"更新后菜单_{timestamp}",
"orderNum": 2
}
response = await menu_api.update_menu(menu_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["menuName"] == f"更新后菜单_{timestamp}"
assert data["orderNum"] == 2
cleanup_menu.append(menu_id)
@pytest.mark.asyncio
async def test_delete_menu_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试删除菜单成功"""
menu_api = MenuAPI(authenticated_client)
create_response = await menu_api.create_menu(test_menu_data)
menu_id = create_response.json()["id"]
response = await menu_api.delete_menu(menu_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_get_menus_by_parent_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试根据父菜单ID获取子菜单成功"""
menu_api = MenuAPI(authenticated_client)
parent_response = await menu_api.create_menu(test_menu_data)
parent_id = parent_response.json()["id"]
timestamp = int(time.time() * 1000)
child_menu_data = test_menu_data.copy()
child_menu_data["menuName"] = f"子菜单_{timestamp}"
child_menu_data["parentId"] = parent_id
child_response = await menu_api.create_menu(child_menu_data)
child_id = child_response.json()["id"]
response = await menu_api.get_menus_by_parent(parent_id)
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert any(menu["id"] == child_id for menu in data)
cleanup_menu.extend([parent_id, child_id])
@pytest.mark.asyncio
async def test_get_menus_by_type_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试根据菜单类型获取菜单成功"""
menu_api = MenuAPI(authenticated_client)
create_response = await menu_api.create_menu(test_menu_data)
menu_id = create_response.json()["id"]
response = await menu_api.get_menus_by_type("C")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert any(menu["id"] == menu_id for menu in data)
cleanup_menu.append(menu_id)
@pytest.mark.asyncio
async def test_create_menu_with_parent_success(self, authenticated_client, test_menu_data, cleanup_menu):
"""测试创建带父菜单的子菜单成功"""
menu_api = MenuAPI(authenticated_client)
parent_response = await menu_api.create_menu(test_menu_data)
parent_id = parent_response.json()["id"]
timestamp = int(time.time() * 1000)
child_menu_data = test_menu_data.copy()
child_menu_data["menuName"] = f"子菜单_{timestamp}"
child_menu_data["parentId"] = parent_id
response = await menu_api.create_menu(child_menu_data)
assert response.status_code == 201
data = response.json()
assert data["parentId"] == parent_id
cleanup_menu.extend([parent_id, data["id"]])
@pytest.mark.asyncio
async def test_create_menu_directory_type_success(self, authenticated_client, cleanup_menu):
"""测试创建目录类型菜单成功"""
menu_api = MenuAPI(authenticated_client)
timestamp = int(time.time() * 1000)
menu_data = {
"menuName": f"目录_{timestamp}",
"parentId": 0,
"orderNum": 1,
"menuType": "M",
"status": "0"
}
response = await menu_api.create_menu(menu_data)
assert response.status_code == 201
data = response.json()
assert data["menuType"] == "M"
cleanup_menu.append(data["id"])
@pytest.mark.asyncio
async def test_create_menu_button_type_success(self, authenticated_client, cleanup_menu):
"""测试创建按钮类型菜单成功"""
menu_api = MenuAPI(authenticated_client)
timestamp = int(time.time() * 1000)
menu_data = {
"menuName": f"按钮_{timestamp}",
"parentId": 1,
"orderNum": 1,
"menuType": "F",
"perms": f"system:button:{timestamp}",
"status": "0"
}
response = await menu_api.create_menu(menu_data)
assert response.status_code == 201
data = response.json()
assert data["menuType"] == "F"
cleanup_menu.append(data["id"])
+184
View File
@@ -0,0 +1,184 @@
"""
通知公告测试用例
"""
import pytest
import time
from api.notice_api import SysNoticeAPI, SysMessageAPI
@pytest.mark.notice
@pytest.mark.regression
class TestSysNotice:
"""系统公告测试类"""
@pytest.mark.asyncio
async def test_create_notice_success(self, authenticated_client):
"""测试创建公告成功"""
api = SysNoticeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"noticeTitle": f"测试公告_{timestamp}",
"noticeType": "1",
"noticeContent": "这是测试公告内容",
"status": "0"
}
response = await api.create(data)
assert response.status_code == 201
result = response.json()
assert result["noticeTitle"] == data["noticeTitle"]
@pytest.mark.asyncio
async def test_get_all_notices(self, authenticated_client):
"""测试获取所有公告"""
api = SysNoticeAPI(authenticated_client)
response = await api.get_all()
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_notice_by_id(self, authenticated_client):
"""测试根据ID获取公告"""
api = SysNoticeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"noticeTitle": f"测试公告_{timestamp}",
"noticeType": "1",
"noticeContent": "测试内容",
"status": "0"
}
create_response = await api.create(data)
notice_id = create_response.json()["id"]
response = await api.get_by_id(notice_id)
assert response.status_code == 200
assert response.json()["id"] == notice_id
@pytest.mark.asyncio
async def test_get_notice_by_status(self, authenticated_client):
"""测试根据状态获取公告"""
api = SysNoticeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"noticeTitle": f"测试公告_{timestamp}",
"noticeType": "1",
"noticeContent": "测试内容",
"status": "0"
}
await api.create(data)
response = await api.get_by_status("0")
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_update_notice(self, authenticated_client):
"""测试更新公告"""
api = SysNoticeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"noticeTitle": f"测试公告_{timestamp}",
"noticeType": "1",
"noticeContent": "原始内容",
"status": "0"
}
create_response = await api.create(data)
notice_id = create_response.json()["id"]
update_data = {
"noticeTitle": f"更新后_{timestamp}",
"noticeType": "1",
"noticeContent": "更新后内容",
"status": "0"
}
response = await api.update(notice_id, update_data)
assert response.status_code == 200
assert response.json()["noticeTitle"] == f"更新后_{timestamp}"
@pytest.mark.asyncio
async def test_delete_notice(self, authenticated_client):
"""测试删除公告"""
api = SysNoticeAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"noticeTitle": f"测试公告_{timestamp}",
"noticeType": "1",
"noticeContent": "测试内容",
"status": "0"
}
create_response = await api.create(data)
notice_id = create_response.json()["id"]
response = await api.delete(notice_id)
assert response.status_code == 204
@pytest.mark.notice
@pytest.mark.regression
class TestSysMessage:
"""用户消息测试类"""
@pytest.mark.asyncio
async def test_create_message(self, authenticated_client):
"""测试创建消息"""
api = SysMessageAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"userId": 1,
"title": f"测试消息_{timestamp}",
"content": "这是测试消息内容",
"type": "1"
}
response = await api.create(data)
assert response.status_code == 201
result = response.json()
assert result["title"] == data["title"]
@pytest.mark.asyncio
async def test_get_messages_by_user(self, authenticated_client):
"""测试获取用户消息"""
api = SysMessageAPI(authenticated_client)
user_id = 1
response = await api.get_by_user(user_id)
assert response.status_code == 200
assert isinstance(response.json(), list)
@pytest.mark.asyncio
async def test_get_unread_count(self, authenticated_client):
"""测试获取未读消息数量"""
api = SysMessageAPI(authenticated_client)
user_id = 1
response = await api.get_unread_count(user_id)
assert response.status_code == 200
@pytest.mark.asyncio
async def test_mark_message_as_read(self, authenticated_client):
"""测试标记消息为已读"""
api = SysMessageAPI(authenticated_client)
timestamp = int(time.time() * 1000)
data = {
"userId": 1,
"title": f"测试消息_{timestamp}",
"content": "测试内容",
"type": "1"
}
create_response = await api.create(data)
message_id = create_response.json()["id"]
response = await api.mark_as_read(message_id)
assert response.status_code == 200
@@ -0,0 +1,200 @@
"""
性能测试基础框架
"""
import pytest
import time
import asyncio
import statistics
from typing import List, Dict, Any
from httpx import AsyncClient
from loguru import logger
@pytest.mark.performance
@pytest.mark.slow
class PerformanceTest:
"""性能测试基类"""
@pytest.fixture
async def perf_client(self, authenticated_client: AsyncClient) -> AsyncClient:
"""性能测试客户端"""
return authenticated_client
@pytest.fixture
def performance_thresholds(self):
"""性能阈值配置"""
return {
"response_time_p95": 2000, # 95%的请求响应时间应小于2秒
"response_time_p99": 5000, # 99%的请求响应时间应小于5秒
"error_rate": 0.05, # 错误率应小于5%
"throughput_min": 10, # 最小吞吐量(请求/秒)
}
async def measure_request_time(self, client: AsyncClient, method: str,
url: str, **kwargs) -> float:
"""测量单个请求时间"""
start_time = time.time()
if method.upper() == "GET":
response = await client.get(url, **kwargs)
elif method.upper() == "POST":
response = await client.post(url, **kwargs)
elif method.upper() == "PUT":
response = await client.put(url, **kwargs)
elif method.upper() == "DELETE":
response = await client.delete(url, **kwargs)
else:
raise ValueError(f"Unsupported method: {method}")
end_time = time.time()
response_time = (end_time - start_time) * 1000 # 转换为毫秒
return response_time
async def measure_concurrent_requests(self, client: AsyncClient, method: str,
url: str, concurrency: int = 10,
**kwargs) -> Dict[str, Any]:
"""测量并发请求性能"""
async def make_request():
return await self.measure_request_time(client, method, url, **kwargs)
start_time = time.time()
results = await asyncio.gather(*[make_request() for _ in range(concurrency)])
end_time = time.time()
total_time = (end_time - start_time) * 1000 # 毫秒
response_times = results
return {
"concurrency": concurrency,
"total_time_ms": total_time,
"response_times_ms": response_times,
"min_time_ms": min(response_times),
"max_time_ms": max(response_times),
"avg_time_ms": statistics.mean(response_times),
"median_time_ms": statistics.median(response_times),
"p95_time_ms": self._percentile(response_times, 95),
"p99_time_ms": self._percentile(response_times, 99),
"throughput_rps": concurrency / (total_time / 1000),
"success_count": len(response_times),
}
def _percentile(self, data: List[float], percentile: float) -> float:
"""计算百分位数"""
sorted_data = sorted(data)
index = int(len(sorted_data) * percentile / 100)
return sorted_data[min(index, len(sorted_data) - 1)]
def assert_performance(self, results: Dict[str, Any], thresholds: Dict[str, Any]):
"""断言性能指标"""
p95_time = results["p95_time_ms"]
p99_time = results["p99_time_ms"]
throughput = results["throughput_rps"]
if p95_time > thresholds["response_time_p95"]:
pytest.fail(f"P95响应时间 {p95_time:.2f}ms 超过阈值 {thresholds['response_time_p95']}ms")
if p99_time > thresholds["response_time_p99"]:
pytest.fail(f"P99响应时间 {p99_time:.2f}ms 超过阈值 {thresholds['response_time_p99']}ms")
if throughput < thresholds["throughput_min"]:
pytest.fail(f"吞吐量 {throughput:.2f} rps 低于最小值 {thresholds['throughput_min']} rps")
logger.info(f"性能测试通过: P95={p95_time:.2f}ms, P99={p99_time:.2f}ms, 吞吐量={throughput:.2f} rps")
@pytest.mark.performance
@pytest.mark.slow
class TestAPIPerformance(PerformanceTest):
"""API性能测试"""
@pytest.mark.asyncio
async def test_user_list_performance(self, perf_client: AsyncClient, performance_thresholds):
"""测试用户列表API性能"""
results = await self.measure_concurrent_requests(
perf_client, "GET", "/api/users", concurrency=20
)
self.assert_performance(results, performance_thresholds)
logger.info(f"用户列表API性能: {results}")
@pytest.mark.asyncio
async def test_role_list_performance(self, perf_client: AsyncClient, performance_thresholds):
"""测试角色列表API性能"""
results = await self.measure_concurrent_requests(
perf_client, "GET", "/api/roles", concurrency=20
)
self.assert_performance(results, performance_thresholds)
logger.info(f"角色列表API性能: {results}")
@pytest.mark.asyncio
async def test_notice_list_performance(self, perf_client: AsyncClient, performance_thresholds):
"""测试通知列表API性能"""
results = await self.measure_concurrent_requests(
perf_client, "GET", "/api/notices", concurrency=20
)
self.assert_performance(results, performance_thresholds)
logger.info(f"通知列表API性能: {results}")
@pytest.mark.asyncio
async def test_search_performance(self, perf_client: AsyncClient, performance_thresholds):
"""测试搜索API性能"""
results = await self.measure_concurrent_requests(
perf_client, "GET", "/api/users/page?keyword=test", concurrency=15
)
self.assert_performance(results, performance_thresholds)
logger.info(f"搜索API性能: {results}")
@pytest.mark.performance
@pytest.mark.slow
class TestLoadTesting(PerformanceTest):
"""负载测试"""
@pytest.mark.asyncio
async def test_sustained_load(self, perf_client: AsyncClient):
"""测试持续负载"""
duration_seconds = 30
requests_per_second = 5
total_requests = duration_seconds * requests_per_second
response_times = []
start_time = time.time()
for i in range(total_requests):
response_time = await self.measure_request_time(
perf_client, "GET", "/api/users"
)
response_times.append(response_time)
elapsed = time.time() - start_time
if elapsed < duration_seconds:
sleep_time = max(0, (i + 1) / requests_per_second - elapsed)
await asyncio.sleep(max(0, sleep_time))
avg_time = statistics.mean(response_times)
p95_time = self._percentile(response_times, 95)
logger.info(f"持续负载测试 - 平均响应时间: {avg_time:.2f}ms, P95: {p95_time:.2f}ms")
assert avg_time < 3000, f"平均响应时间 {avg_time:.2f}ms 超过阈值 3000ms"
assert p95_time < 5000, f"P95响应时间 {p95_time:.2f}ms 超过阈值 5000ms"
@pytest.mark.asyncio
async def test_spike_load(self, perf_client: AsyncClient):
"""测试突发负载"""
spike_sizes = [10, 50, 100, 50, 10]
for spike_size in spike_sizes:
results = await self.measure_concurrent_requests(
perf_client, "GET", "/api/users", concurrency=spike_size
)
logger.info(f"突发负载测试 (并发={spike_size}): P95={results['p95_time_ms']:.2f}ms")
assert results["p95_time_ms"] < 10000, \
f"突发负载 {spike_size} 并发时 P95响应时间超时"
@@ -0,0 +1,274 @@
"""
权限管理增强测试用例
"""
import pytest
from api.role_api import RoleAPI
from api.user_api import UserAPI
@pytest.mark.permission
@pytest.mark.regression
class TestPermission:
"""权限管理测试类"""
@pytest.mark.asyncio
async def test_user_role_assignment(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试用户角色分配"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
user_response = await user_api.create_user(test_user_data)
user_id = user_response.json()["id"]
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
update_data = {"roleId": role_id}
response = await user_api.update_user(user_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["roleId"] == role_id
cleanup_user.append(user_id)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_user_role_removal(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试用户角色移除"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
user_response = await user_api.create_user(test_user_data)
user_id = user_response.json()["id"]
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
await user_api.update_user(user_id, {"roleId": role_id})
response = await user_api.update_user(user_id, {"roleId": None})
assert response.status_code == 200
data = response.json()
assert data["roleId"] is None
cleanup_user.append(user_id)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_role_status_permission(self, authenticated_client, test_role_data, cleanup_role):
"""测试角色状态权限控制"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.update_role(role_id, {"status": 0})
assert response.status_code == 200
data = response.json()
assert data["status"] == 0
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_multiple_users_same_role(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试多个用户分配相同角色"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
user_ids = []
for i in range(3):
import time
timestamp = int(time.time() * 1000)
user_data = test_user_data.copy()
user_data["username"] = f"testuser_{timestamp}_{i}"
user_data["email"] = f"test_{timestamp}_{i}@example.com"
user_response = await user_api.create_user(user_data)
user_id = user_response.json()["id"]
user_ids.append(user_id)
await user_api.update_user(user_id, {"roleId": role_id})
for user_id in user_ids:
user_response = await user_api.get_user_by_id(user_id)
assert user_response.json()["roleId"] == role_id
cleanup_user.extend(user_ids)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_role_hierarchy(self, authenticated_client, cleanup_role):
"""测试角色层级"""
role_api = RoleAPI(authenticated_client)
import time
timestamp = int(time.time() * 1000)
admin_role_data = {
"roleName": f"Admin_{timestamp}",
"roleKey": f"admin_{timestamp}",
"roleSort": 1,
"status": 1
}
admin_response = await role_api.create_role(admin_role_data)
admin_id = admin_response.json()["id"]
user_role_data = {
"roleName": f"User_{timestamp}",
"roleKey": f"user_{timestamp}",
"roleSort": 2,
"status": 1
}
user_response = await role_api.create_role(user_role_data)
user_id = user_response.json()["id"]
all_roles = await role_api.get_all_roles()
roles_data = all_roles.json()
role_sorts = [role["roleSort"] for role in roles_data]
assert 1 in role_sorts
assert 2 in role_sorts
cleanup_role.extend([admin_id, user_id])
@pytest.mark.asyncio
async def test_permission_inheritance(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试权限继承"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
user_response = await user_api.create_user(test_user_data)
user_id = user_response.json()["id"]
await user_api.update_user(user_id, {"roleId": role_id})
user_data = await user_api.get_user_by_id(user_id)
assert user_data.json()["roleId"] == role_id
role_data = await role_api.get_role_by_id(role_id)
assert role_data.json()["id"] == role_id
cleanup_user.append(user_id)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_role_sort_order(self, authenticated_client, cleanup_role):
"""测试角色排序"""
role_api = RoleAPI(authenticated_client)
import time
timestamp = int(time.time() * 1000)
role1_data = {
"roleName": f"Role1_{timestamp}",
"roleKey": f"role1_{timestamp}",
"roleSort": 3,
"status": 1
}
role1_response = await role_api.create_role(role1_data)
role1_id = role1_response.json()["id"]
role2_data = {
"roleName": f"Role2_{timestamp}",
"roleKey": f"role2_{timestamp}",
"roleSort": 1,
"status": 1
}
role2_response = await role_api.create_role(role2_data)
role2_id = role2_response.json()["id"]
role3_data = {
"roleName": f"Role3_{timestamp}",
"roleKey": f"role3_{timestamp}",
"roleSort": 2,
"status": 1
}
role3_response = await role_api.create_role(role3_data)
role3_id = role3_response.json()["id"]
response = await role_api.get_roles_by_page(page=0, size=10, sort="roleSort", order="asc")
roles = response.json()["content"]
role_sorts = [role["roleSort"] for role in roles]
assert role_sorts == sorted(role_sorts)
cleanup_role.extend([role1_id, role2_id, role3_id])
@pytest.mark.asyncio
async def test_disabled_role_access(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试禁用角色的访问控制"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
user_response = await user_api.create_user(test_user_data)
user_id = user_response.json()["id"]
await user_api.update_user(user_id, {"roleId": role_id})
await role_api.update_role(role_id, {"status": 0})
role_data = await role_api.get_role_by_id(role_id)
assert role_data.json()["status"] == 0
cleanup_user.append(user_id)
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_role_uniqueness(self, authenticated_client, cleanup_role):
"""测试角色唯一性约束"""
role_api = RoleAPI(authenticated_client)
import time
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"UniqueRole_{timestamp}",
"roleKey": f"unique_role_{timestamp}",
"roleSort": 1,
"status": 1
}
response1 = await role_api.create_role(role_data)
assert response1.status_code == 201
role_id = response1.json()["id"]
response2 = await role_api.create_role(role_data)
assert response2.status_code in [400, 409]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_role_deletion_with_users(self, authenticated_client, test_user_data, test_role_data, cleanup_user, cleanup_role):
"""测试删除有用户的角色"""
user_api = UserAPI(authenticated_client)
role_api = RoleAPI(authenticated_client)
role_response = await role_api.create_role(test_role_data)
role_id = role_response.json()["id"]
user_response = await user_api.create_user(test_user_data)
user_id = user_response.json()["id"]
await user_api.update_user(user_id, {"roleId": role_id})
delete_response = await role_api.delete_role(role_id)
assert delete_response.status_code == 200
user_data = await user_api.get_user_by_id(user_id)
assert user_data.json()["roleId"] is None
cleanup_user.append(user_id)
cleanup_role.append(role_id)
@@ -0,0 +1,292 @@
"""
真实的端到端(E2E)测试 - 使用Playwright测试前后端联通
"""
import pytest
import time
from playwright.async_api import async_playwright, Page, Browser, BrowserContext
from httpx import AsyncClient
from config.settings import settings
@pytest.mark.e2e
@pytest.mark.playwright
class TestRealE2E:
"""真实的端到端测试类"""
@pytest.fixture
async def browser(self):
"""浏览器fixture - headless模式"""
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
yield browser
await browser.close()
@pytest.fixture
async def context(self, browser):
"""浏览器上下文fixture"""
context = await browser.new_context()
yield context
await context.close()
@pytest.fixture
async def page(self, context):
"""页面fixture"""
page = await context.new_page()
page.set_default_timeout(30000)
yield page
await page.close()
@pytest.fixture
async def authenticated_client(self):
"""已认证的HTTP客户端"""
async with AsyncClient(base_url=settings.API_BASE_URL) as client:
response = await client.post(
"/api/auth/login",
json={
"username": settings.TEST_USERNAME,
"password": settings.TEST_PASSWORD
}
)
assert response.status_code == 200
token = response.json().get("token")
client.headers.update({"Authorization": f"Bearer {token}"})
yield client
@pytest.mark.asyncio
async def test_complete_user_lifecycle_e2e(self, page, authenticated_client):
"""测试完整的用户生命周期 - 前后端联通"""
timestamp = int(time.time() * 1000)
username = f"e2e_user_{timestamp}"
email = f"e2e_{timestamp}@example.com"
# 1. 通过前端登录
await page.goto("http://localhost:3002/login")
await page.wait_for_load_state("networkidle")
await page.fill('input[name="username"]', settings.TEST_USERNAME)
await page.fill('input[name="password"]', settings.TEST_PASSWORD)
await page.click('button[type="submit"]')
await page.wait_for_url("**/")
assert await page.title() != ""
# 2. 通过前端创建用户
await page.click('text=用户管理')
await page.wait_for_url("**/users")
await page.click('text=创建用户')
await page.fill('input[name="username"]', username)
await page.fill('input[name="email"]', email)
await page.fill('input[name="phone"]', '13800138000')
await page.fill('input[name="password"]', 'Test123!@#')
await page.fill('input[name="confirmPassword"]', 'Test123!@#')
await page.click('button[type="submit"]')
await page.wait_for_selector('.success-message', timeout=10000)
success_message = await page.text_content('.success-message')
assert '成功' in success_message or 'success' in success_message.lower()
# 3. 通过API验证用户已创建
response = await authenticated_client.get("/api/users")
assert response.status_code == 200
users = response.json()
user_exists = any(user['username'] == username for user in users)
assert user_exists, f"User {username} not found in API response"
# 4. 通过前端验证用户显示
await page.reload()
await page.wait_for_load_state("networkidle")
page_content = await page.content()
assert username in page_content, f"Username {username} not found in page content"
@pytest.mark.asyncio
async def test_role_assignment_e2e(self, page, authenticated_client):
"""测试角色分配 - 前后端联通"""
timestamp = int(time.time() * 1000)
role_name = f"E2E_Role_{timestamp}"
role_key = f"e2e_role_{timestamp}"
# 1. 通过API创建角色
role_response = await authenticated_client.post(
"/api/roles",
json={
"roleName": role_name,
"roleKey": role_key,
"roleSort": 1,
"status": 1
}
)
assert role_response.status_code == 201
role_id = role_response.json()["id"]
# 2. 通过前端登录
await page.goto("http://localhost:3002/login")
await page.fill('input[name="username"]', settings.TEST_USERNAME)
await page.fill('input[name="password"]', settings.TEST_PASSWORD)
await page.click('button[type="submit"]')
await page.wait_for_url("**/")
# 3. 通过前端创建用户
await page.click('text=用户管理')
await page.wait_for_url("**/users")
await page.click('text=创建用户')
username = f"e2e_user_{timestamp}"
await page.fill('input[name="username"]', username)
await page.fill('input[name="email"]', f"e2e_{timestamp}@example.com")
await page.fill('input[name="password"]', 'Test123!@#')
await page.fill('input[name="confirmPassword"]', 'Test123!@#')
await page.click('button[type="submit"]')
await page.wait_for_selector('.success-message', timeout=10000)
# 4. 通过API获取用户ID并分配角色
users_response = await authenticated_client.get("/api/users")
users = users_response.json()
user = next((u for u in users if u['username'] == username), None)
assert user is not None
await authenticated_client.put(
f"/api/users/{user['id']}",
json={"roleId": role_id}
)
# 5. 通过API验证角色分配
user_response = await authenticated_client.get(f"/api/users/{user['id']}")
assert user_response.status_code == 200
user_data = user_response.json()
assert user_data["roleId"] == role_id
# 6. 清理测试数据
await authenticated_client.delete(f"/api/users/{user['id']}")
await authenticated_client.delete(f"/api/roles/{role_id}")
@pytest.mark.asyncio
async def test_login_and_navigation_e2e(self, page):
"""测试登录和导航 - 前后端联通"""
# 1. 访问登录页面
await page.goto("http://localhost:3002/login")
await page.wait_for_load_state("networkidle")
title = await page.title()
assert "登录" in title or "Login" in title.lower()
# 2. 填写登录表单
await page.fill('input[name="username"]', settings.TEST_USERNAME)
await page.fill('input[name="password"]', settings.TEST_PASSWORD)
# 3. 点击登录按钮
await page.click('button[type="submit"]')
# 4. 等待跳转到首页
await page.wait_for_url("**/", timeout=10000)
# 5. 验证用户信息显示
user_info = await page.query_selector('.user-info')
assert user_info is not None, "User info element not found"
user_text = await user_info.text_content()
assert settings.TEST_USERNAME in user_text
# 6. 测试导航到不同页面
await page.click('text=用户管理')
await page.wait_for_url("**/users")
await page.click('text=角色管理')
await page.wait_for_url("**/roles")
await page.click('text=系统配置')
await page.wait_for_url("**/config")
@pytest.mark.asyncio
async def test_system_config_e2e(self, page, authenticated_client):
"""测试系统配置 - 前后端联通"""
# 1. 通过前端登录
await page.goto("http://localhost:3002/login")
await page.fill('input[name="username"]', settings.TEST_USERNAME)
await page.fill('input[name="password"]', settings.TEST_PASSWORD)
await page.click('button[type="submit"]')
await page.wait_for_url("**/")
# 2. 通过前端访问系统配置
await page.click('text=系统配置')
await page.wait_for_url("**/config")
# 3. 验证配置列表显示
table = await page.query_selector('table')
assert table is not None, "Config table not found"
# 4. 通过API获取配置
config_response = await authenticated_client.get("/api/config")
assert config_response.status_code == 200
configs = config_response.json()
# 5. 验证前后端数据一致
page_content = await page.content()
for config in configs[:3]:
assert config['configKey'] in page_content or config['configName'] in page_content
@pytest.mark.asyncio
async def test_search_and_filter_e2e(self, page, authenticated_client):
"""测试搜索和过滤 - 前后端联通"""
timestamp = int(time.time() * 1000)
# 1. 通过API创建多个测试用户
user_ids = []
for i in range(3):
username = f"search_{timestamp}_{i}"
response = await authenticated_client.post(
"/api/users",
json={
"username": username,
"password": "Test123!@#",
"email": f"search_{timestamp}_{i}@example.com",
"status": 1
}
)
assert response.status_code == 201
user_ids.append(response.json()["id"])
try:
# 2. 通过前端登录
await page.goto("http://localhost:3002/login")
await page.fill('input[name="username"]', settings.TEST_USERNAME)
await page.fill('input[name="password"]', settings.TEST_PASSWORD)
await page.click('button[type="submit"]')
await page.wait_for_url("**/")
# 3. 通过前端搜索用户
await page.click('text=用户管理')
await page.wait_for_url("**/users")
await page.fill('input[name="keyword"]', f"search_{timestamp}")
await page.click('button[type="search"]')
await page.wait_for_load_state("networkidle")
# 4. 验证搜索结果显示
page_content = await page.content()
assert f"search_{timestamp}" in page_content
# 5. 通过API验证搜索结果
search_response = await authenticated_client.get(
"/api/users/page",
params={"keyword": f"search_{timestamp}", "page": 0, "size": 10}
)
assert search_response.status_code == 200
search_data = search_response.json()
assert len(search_data["content"]) >= 3
finally:
# 6. 清理测试数据
for user_id in user_ids:
try:
await authenticated_client.delete(f"/api/users/{user_id}")
except Exception:
pass
+344
View File
@@ -0,0 +1,344 @@
"""
角色管理测试用例
"""
import pytest
from api.role_api import RoleAPI
@pytest.mark.role
@pytest.mark.regression
class TestRole:
"""角色管理测试类"""
@pytest.mark.asyncio
async def test_create_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试创建角色成功"""
role_api = RoleAPI(authenticated_client)
response = await role_api.create_role(test_role_data)
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["roleName"] == test_role_data["roleName"]
assert data["roleKey"] == test_role_data["roleKey"]
assert data["roleSort"] == test_role_data["roleSort"]
assert data["status"] == test_role_data["status"]
cleanup_role.append(data["id"])
@pytest.mark.asyncio
async def test_create_role_duplicate_name(self, authenticated_client, test_role_data, cleanup_role):
"""测试创建重复角色名"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.create_role(test_role_data)
assert response.status_code in [400, 409]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_role_by_id_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试根据ID获取角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.get_role_by_id(role_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == role_id
assert data["roleName"] == test_role_data["roleName"]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_role_by_id_not_found(self, authenticated_client):
"""测试获取不存在的角色"""
role_api = RoleAPI(authenticated_client)
response = await role_api.get_role_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_role_by_name_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试根据名称获取角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.get_role_by_name(test_role_data["roleName"])
assert response.status_code == 200
data = response.json()
assert data["roleName"] == test_role_data["roleName"]
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_get_all_roles_success(self, authenticated_client):
"""测试获取所有角色成功"""
role_api = RoleAPI(authenticated_client)
response = await role_api.get_all_roles()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试更新角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
import time
timestamp = int(time.time() * 1000)
update_data = {"roleName": f"UPDATED_ROLE_{timestamp}"}
response = await role_api.update_role(role_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["roleName"] == f"UPDATED_ROLE_{timestamp}"
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_delete_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试删除角色成功(逻辑删除)"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.delete_role(role_id)
assert response.status_code == 200
data = response.json()
assert data["deletedAt"] is not None
get_response = await role_api.get_role_by_id(role_id)
assert get_response.status_code == 404
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_restore_role_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试恢复角色成功"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
await role_api.delete_role(role_id)
response = await role_api.restore_role(role_id)
assert response.status_code == 200
get_response = await role_api.get_role_by_id(role_id)
assert get_response.status_code == 200
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_check_name_exists_true(self, authenticated_client, test_role_data, cleanup_role):
"""测试检查角色名存在-返回true"""
role_api = RoleAPI(authenticated_client)
create_response = await role_api.create_role(test_role_data)
role_id = create_response.json()["id"]
response = await role_api.check_name_exists(test_role_data["roleName"])
assert response.status_code == 200
assert response.json() is True
cleanup_role.append(role_id)
@pytest.mark.asyncio
async def test_check_name_exists_false(self, authenticated_client):
"""测试检查角色名存在-返回false"""
role_api = RoleAPI(authenticated_client)
response = await role_api.check_name_exists("NONEXISTENT_ROLE")
assert response.status_code == 200
assert response.json() is False
@pytest.mark.asyncio
async def test_get_roles_by_page_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试分页获取角色成功"""
role_api = RoleAPI(authenticated_client)
import time
for i in range(5):
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"testrole_{timestamp}_{i}",
"roleKey": f"testrole_{timestamp}_{i}",
"roleSort": 1,
"status": 1
}
create_response = await role_api.create_role(role_data)
cleanup_role.append(create_response.json()["id"])
response = await role_api.get_roles_by_page(page=0, size=10)
assert response.status_code == 200
data = response.json()
assert "content" in data
assert "totalElements" in data
assert "totalPages" in data
assert "currentPage" in data
assert "pageSize" in data
assert len(data["content"]) <= 10
@pytest.mark.asyncio
async def test_get_roles_by_page_with_sort(self, authenticated_client, test_role_data, cleanup_role):
"""测试分页获取角色并排序成功"""
role_api = RoleAPI(authenticated_client)
import time
timestamp1 = int(time.time() * 1000)
role1_data = {
"roleName": f"role_a_{timestamp1}",
"roleKey": f"role_a_{timestamp1}",
"roleSort": 1,
"status": 1
}
create_response1 = await role_api.create_role(role1_data)
cleanup_role.append(create_response1.json()["id"])
timestamp2 = int(time.time() * 1000)
role2_data = {
"roleName": f"role_b_{timestamp2}",
"roleKey": f"role_b_{timestamp2}",
"roleSort": 2,
"status": 1
}
create_response2 = await role_api.create_role(role2_data)
cleanup_role.append(create_response2.json()["id"])
response = await role_api.get_roles_by_page(page=0, size=10, sort="roleName", order="asc")
assert response.status_code == 200
data = response.json()
role_names = [role["roleName"] for role in data["content"]]
assert role_names == sorted(role_names)
@pytest.mark.asyncio
async def test_get_roles_by_page_with_search(self, authenticated_client, test_role_data, cleanup_role):
"""测试分页获取角色并搜索成功"""
role_api = RoleAPI(authenticated_client)
import time
timestamp1 = int(time.time() * 1000)
role1_data = {
"roleName": f"search_test_role_{timestamp1}",
"roleKey": f"search_test_role_{timestamp1}",
"roleSort": 1,
"status": 1
}
create_response1 = await role_api.create_role(role1_data)
cleanup_role.append(create_response1.json()["id"])
timestamp2 = int(time.time() * 1000)
role2_data = {
"roleName": f"other_role_{timestamp2}",
"roleKey": f"other_role_{timestamp2}",
"roleSort": 1,
"status": 1
}
create_response2 = await role_api.create_role(role2_data)
cleanup_role.append(create_response2.json()["id"])
response = await role_api.get_roles_by_page(page=0, size=10, keyword="search")
assert response.status_code == 200
data = response.json()
assert len(data["content"]) >= 1
assert all("search" in role["roleName"] or "search" in role["roleKey"]
for role in data["content"])
@pytest.mark.asyncio
async def test_get_role_count_success(self, authenticated_client, test_role_data, cleanup_role):
"""测试获取角色总数成功"""
role_api = RoleAPI(authenticated_client)
initial_count_response = await role_api.get_role_count()
initial_count = initial_count_response.json()
create_response = await role_api.create_role(test_role_data)
cleanup_role.append(create_response.json()["id"])
final_count_response = await role_api.get_role_count()
final_count = final_count_response.json()
assert final_count == initial_count + 1
@pytest.mark.asyncio
async def test_get_roles_by_page_with_different_page_sizes(self, authenticated_client, test_role_data, cleanup_role):
"""测试不同页面大小的分页获取角色成功"""
role_api = RoleAPI(authenticated_client)
import time
for i in range(15):
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"pagesize_test_{timestamp}_{i}",
"roleKey": f"pagesize_test_{timestamp}_{i}",
"roleSort": 1,
"status": 1
}
create_response = await role_api.create_role(role_data)
cleanup_role.append(create_response.json()["id"])
response_size_10 = await role_api.get_roles_by_page(page=0, size=10)
assert response_size_10.status_code == 200
data_size_10 = response_size_10.json()
assert len(data_size_10["content"]) == 10
response_size_5 = await role_api.get_roles_by_page(page=0, size=5)
assert response_size_5.status_code == 200
data_size_5 = response_size_5.json()
assert len(data_size_5["content"]) == 5
@pytest.mark.asyncio
async def test_get_roles_by_page_with_page_navigation(self, authenticated_client, test_role_data, cleanup_role):
"""测试分页导航成功"""
role_api = RoleAPI(authenticated_client)
import time
for i in range(25):
timestamp = int(time.time() * 1000)
role_data = {
"roleName": f"pagination_test_{timestamp}_{i}",
"roleKey": f"pagination_test_{timestamp}_{i}",
"roleSort": 1,
"status": 1
}
create_response = await role_api.create_role(role_data)
cleanup_role.append(create_response.json()["id"])
page1_response = await role_api.get_roles_by_page(page=0, size=10)
page1_data = page1_response.json()
assert page1_data["currentPage"] == 0
assert len(page1_data["content"]) == 10
page2_response = await role_api.get_roles_by_page(page=1, size=10)
page2_data = page2_response.json()
assert page2_data["currentPage"] == 1
assert len(page2_data["content"]) == 10
page3_response = await role_api.get_roles_by_page(page=2, size=10)
page3_data = page3_response.json()
assert page3_data["currentPage"] == 2
assert len(page3_data["content"]) >= 5
+340
View File
@@ -0,0 +1,340 @@
"""
用户管理测试用例
"""
import pytest
from api.user_api import UserAPI
from config.settings import settings
@pytest.mark.user
@pytest.mark.regression
class TestUser:
"""用户管理测试类"""
@pytest.mark.asyncio
async def test_create_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试创建用户成功"""
user_api = UserAPI(authenticated_client)
response = await user_api.create_user(test_user_data)
print(f"Response status: {response.status_code}")
print(f"Response text: {response.text}")
assert response.status_code == 201
data = response.json()
assert "id" in data
assert data["username"] == test_user_data["username"]
assert data["email"] == test_user_data["email"]
assert "password" not in data or data["password"] != test_user_data["password"]
cleanup_user.append(data["id"])
@pytest.mark.asyncio
async def test_create_user_duplicate_username(self, authenticated_client, test_user_data, cleanup_user):
"""测试创建重复用户名"""
user_api = UserAPI(authenticated_client)
await user_api.create_user(test_user_data)
response = await user_api.create_user(test_user_data)
assert response.status_code in [400, 409]
@pytest.mark.asyncio
async def test_get_user_by_id_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试根据ID获取用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.get_user_by_id(user_id)
assert response.status_code == 200
data = response.json()
assert data["id"] == user_id
assert data["username"] == test_user_data["username"]
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_get_user_by_id_not_found(self, authenticated_client):
"""测试获取不存在的用户"""
user_api = UserAPI(authenticated_client)
response = await user_api.get_user_by_id(999999)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_get_all_users_success(self, authenticated_client):
"""测试获取所有用户成功"""
user_api = UserAPI(authenticated_client)
response = await user_api.get_all_users()
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_update_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试更新用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
update_data = {"email": "updated@example.com"}
response = await user_api.update_user(user_id, update_data)
assert response.status_code == 200
data = response.json()
assert data["email"] == "updated@example.com"
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_delete_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试删除用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.delete_user(user_id)
assert response.status_code == 204
@pytest.mark.asyncio
async def test_logical_delete_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试逻辑删除用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.logical_delete_user(user_id)
assert response.status_code == 204
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 404
get_deleted_response = await user_api.get_all_users(include_deleted=True)
deleted_users = get_deleted_response.json()
assert any(u["id"] == user_id for u in deleted_users)
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_restore_user_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试恢复用户成功"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
await user_api.logical_delete_user(user_id)
response = await user_api.restore_user(user_id)
assert response.status_code == 204
get_response = await user_api.get_user_by_id(user_id)
assert get_response.status_code == 200
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_username_exists_true(self, authenticated_client, test_user_data, cleanup_user):
"""测试检查用户名存在-返回true"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.check_username_exists(test_user_data["username"])
assert response.status_code == 200
assert response.json() is True
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_username_exists_false(self, authenticated_client):
"""测试检查用户名存在-返回false"""
user_api = UserAPI(authenticated_client)
response = await user_api.check_username_exists("nonexistent_user")
assert response.status_code == 200
assert response.json() is False
@pytest.mark.asyncio
async def test_check_email_exists_true(self, authenticated_client, test_user_data, cleanup_user):
"""测试检查邮箱存在-返回true"""
user_api = UserAPI(authenticated_client)
create_response = await user_api.create_user(test_user_data)
user_id = create_response.json()["id"]
response = await user_api.check_email_exists(test_user_data["email"])
assert response.status_code == 200
assert response.json() is True
cleanup_user.append(user_id)
@pytest.mark.asyncio
async def test_check_email_exists_false(self, authenticated_client):
"""测试检查邮箱存在-返回false"""
user_api = UserAPI(authenticated_client)
response = await user_api.check_email_exists("nonexistent@example.com")
assert response.status_code == 200
assert response.json() is False
@pytest.mark.asyncio
async def test_get_users_by_page_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试分页获取用户成功"""
import time
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
for i in range(5):
user_data = test_user_data.copy()
user_data["username"] = f"testuser_{timestamp}_{i}"
user_data["email"] = f"testuser_{timestamp}_{i}@example.com"
create_response = await user_api.create_user(user_data)
cleanup_user.append(create_response.json()["id"])
response = await user_api.get_users_by_page(page=0, size=10)
assert response.status_code == 200
data = response.json()
assert "content" in data
assert "totalElements" in data
assert "totalPages" in data
assert "currentPage" in data
assert "pageSize" in data
assert len(data["content"]) <= 10
@pytest.mark.asyncio
async def test_get_users_by_page_with_sort(self, authenticated_client, test_user_data, cleanup_user):
"""测试分页获取用户并排序成功"""
import time
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
user1_data = test_user_data.copy()
user1_data["username"] = f"user_a_{timestamp}"
user1_data["email"] = f"user_a_{timestamp}@example.com"
create_response1 = await user_api.create_user(user1_data)
cleanup_user.append(create_response1.json()["id"])
user2_data = test_user_data.copy()
user2_data["username"] = f"user_b_{timestamp}"
user2_data["email"] = f"user_b_{timestamp}@example.com"
create_response2 = await user_api.create_user(user2_data)
cleanup_user.append(create_response2.json()["id"])
response = await user_api.get_users_by_page(page=0, size=10, sort="username", order="asc")
assert response.status_code == 200
data = response.json()
usernames = [user["username"] for user in data["content"]]
assert usernames == sorted(usernames)
@pytest.mark.asyncio
async def test_get_users_by_page_with_search(self, authenticated_client, test_user_data, cleanup_user):
"""测试分页获取用户并搜索成功"""
import time
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
user1_data = test_user_data.copy()
user1_data["username"] = f"search_test_user_{timestamp}"
user1_data["email"] = f"search_test_{timestamp}@example.com"
create_response1 = await user_api.create_user(user1_data)
cleanup_user.append(create_response1.json()["id"])
user2_data = test_user_data.copy()
user2_data["username"] = f"other_user_{timestamp}"
user2_data["email"] = f"other_{timestamp}@example.com"
create_response2 = await user_api.create_user(user2_data)
cleanup_user.append(create_response2.json()["id"])
response = await user_api.get_users_by_page(page=0, size=10, keyword="search")
assert response.status_code == 200
data = response.json()
assert len(data["content"]) >= 1
assert all("search" in user["username"] or "search" in user["email"]
for user in data["content"])
@pytest.mark.asyncio
async def test_get_user_count_success(self, authenticated_client, test_user_data, cleanup_user):
"""测试获取用户总数成功"""
user_api = UserAPI(authenticated_client)
initial_count_response = await user_api.get_user_count()
initial_count = initial_count_response.json()
create_response = await user_api.create_user(test_user_data)
cleanup_user.append(create_response.json()["id"])
final_count_response = await user_api.get_user_count()
final_count = final_count_response.json()
assert final_count == initial_count + 1
@pytest.mark.asyncio
async def test_get_users_by_page_with_different_page_sizes(self, authenticated_client, test_user_data, cleanup_user):
"""测试不同页面大小的分页获取用户成功"""
import time
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
for i in range(15):
user_data = test_user_data.copy()
user_data["username"] = f"pagesize_test_{timestamp}_{i}"
user_data["email"] = f"pagesize_test_{timestamp}_{i}@example.com"
create_response = await user_api.create_user(user_data)
cleanup_user.append(create_response.json()["id"])
response_size_10 = await user_api.get_users_by_page(page=0, size=10)
assert response_size_10.status_code == 200
data_size_10 = response_size_10.json()
assert len(data_size_10["content"]) == 10
response_size_5 = await user_api.get_users_by_page(page=0, size=5)
assert response_size_5.status_code == 200
data_size_5 = response_size_5.json()
assert len(data_size_5["content"]) == 5
@pytest.mark.asyncio
async def test_get_users_by_page_with_page_navigation(self, authenticated_client, test_user_data, cleanup_user):
"""测试分页导航成功"""
import time
user_api = UserAPI(authenticated_client)
timestamp = int(time.time() * 1000)
for i in range(25):
user_data = test_user_data.copy()
user_data["username"] = f"pagination_test_{timestamp}_{i}"
user_data["email"] = f"pagination_test_{timestamp}_{i}@example.com"
create_response = await user_api.create_user(user_data)
cleanup_user.append(create_response.json()["id"])
page1_response = await user_api.get_users_by_page(page=0, size=10)
page1_data = page1_response.json()
assert page1_data["currentPage"] == 0
assert len(page1_data["content"]) == 10
page2_response = await user_api.get_users_by_page(page=1, size=10)
page2_data = page2_response.json()
assert page2_data["currentPage"] == 1
assert len(page2_data["content"]) == 10
page3_response = await user_api.get_users_by_page(page=2, size=10)
page3_data = page3_response.json()
assert page3_data["currentPage"] == 2
assert len(page3_data["content"]) >= 5
@@ -0,0 +1,191 @@
"""
WebSocket实时通信测试用例
"""
import pytest
import asyncio
import json
import os
from websockets.client import connect
from websockets.exceptions import ConnectionClosed
@pytest.mark.websocket
@pytest.mark.regression
class TestWebSocket:
"""WebSocket实时通信测试类"""
@pytest.fixture
def websocket_url(self):
"""WebSocket连接URL"""
api_base_url = os.getenv("API_BASE_URL", "http://localhost:8084")
ws_url = api_base_url.replace("http://", "ws://")
return f"{ws_url}/ws"
@pytest.fixture
async def websocket_connection(self, websocket_url):
"""WebSocket连接fixture"""
async with connect(websocket_url) as websocket:
yield websocket
@pytest.fixture
async def authenticated_websocket(self, websocket_url, authenticated_client):
"""带认证的WebSocket连接"""
token = authenticated_client.headers.get("Authorization")
url_with_token = f"{websocket_url}?token={token.replace('Bearer ', '')}"
async with connect(url_with_token) as websocket:
yield websocket
@pytest.mark.asyncio
async def test_websocket_connection(self, websocket_url):
"""测试WebSocket连接建立"""
try:
async with connect(websocket_url) as websocket:
assert websocket.open
except ConnectionRefusedError:
pytest.skip("WebSocket服务未启动")
@pytest.mark.asyncio
async def test_websocket_ping_pong(self, websocket_connection):
"""测试WebSocket心跳机制"""
ping_message = {
"type": "ping",
"timestamp": 1234567890
}
await websocket_connection.send(json.dumps(ping_message))
response = await asyncio.wait_for(websocket_connection.recv(), timeout=5.0)
pong_message = json.loads(response)
assert pong_message["type"] == "pong"
assert "timestamp" in pong_message
@pytest.mark.asyncio
async def test_websocket_subscribe(self, websocket_connection):
"""测试WebSocket订阅"""
subscribe_message = {
"type": "subscribe",
"channel": "notifications"
}
await websocket_connection.send(json.dumps(subscribe_message))
response = await asyncio.wait_for(websocket_connection.recv(), timeout=5.0)
message = json.loads(response)
assert message["type"] in ["subscribe_ack", "pong"]
@pytest.mark.asyncio
async def test_websocket_multiple_messages(self, websocket_connection):
"""测试WebSocket多消息处理"""
messages = [
{"type": "ping"},
{"type": "subscribe", "channel": "test"},
{"type": "ping"}
]
responses = []
for msg in messages:
await websocket_connection.send(json.dumps(msg))
try:
response = await asyncio.wait_for(websocket_connection.recv(), timeout=2.0)
responses.append(json.loads(response))
except asyncio.TimeoutError:
pass
assert len(responses) >= 2
@pytest.mark.asyncio
async def test_websocket_invalid_message(self, websocket_connection):
"""测试WebSocket无效消息处理"""
invalid_messages = [
"invalid json",
"",
json.dumps({"type": "unknown_type"}),
json.dumps({})
]
for msg in invalid_messages:
try:
await websocket_connection.send(msg)
await asyncio.sleep(0.5)
except Exception:
pass
@pytest.mark.asyncio
async def test_websocket_connection_close(self, websocket_url):
"""测试WebSocket连接关闭"""
async with connect(websocket_url) as websocket:
assert websocket.open
await websocket.close()
assert not websocket.open
@pytest.mark.asyncio
async def test_websocket_timeout(self, websocket_url):
"""测试WebSocket超时"""
try:
async with connect(websocket_url, ping_timeout=2.0) as websocket:
await asyncio.sleep(3.0)
except (ConnectionClosed, asyncio.TimeoutError):
pass
@pytest.mark.asyncio
async def test_websocket_concurrent_connections(self, websocket_url):
"""测试WebSocket并发连接"""
async def create_connection():
try:
async with connect(websocket_url) as websocket:
await websocket.send(json.dumps({"type": "ping"}))
await asyncio.wait_for(websocket_connection.recv(), timeout=2.0)
except Exception:
pass
connections = [create_connection() for _ in range(5)]
await asyncio.gather(*connections, return_exceptions=True)
@pytest.mark.asyncio
async def test_websocket_large_message(self, websocket_connection):
"""测试WebSocket大消息处理"""
large_data = "x" * 10000
message = {
"type": "test",
"data": large_data
}
await websocket_connection.send(json.dumps(message))
try:
response = await asyncio.wait_for(websocket_connection.recv(), timeout=5.0)
assert response
except asyncio.TimeoutError:
pass
@pytest.mark.asyncio
async def test_websocket_reconnect(self, websocket_url):
"""测试WebSocket重连"""
for i in range(3):
try:
async with connect(websocket_url) as websocket:
await websocket.send(json.dumps({"type": "ping"}))
response = await asyncio.wait_for(websocket.recv(), timeout=2.0)
assert response
except Exception:
pass
@pytest.mark.asyncio
async def test_websocket_unicode_message(self, websocket_connection):
"""测试WebSocket Unicode消息"""
unicode_message = {
"type": "test",
"content": "测试中文🎉🚀"
}
await websocket_connection.send(json.dumps(unicode_message))
try:
response = await asyncio.wait_for(websocket_connection.recv(), timeout=2.0)
assert response
except asyncio.TimeoutError:
pass
+3
View File
@@ -0,0 +1,3 @@
"""
工具模块
"""
+83
View File
@@ -0,0 +1,83 @@
"""
断言工具
"""
from typing import Any, Dict, List
from httpx import Response
class Assertions:
"""断言工具类"""
@staticmethod
def assert_status_code(response: Response, expected_status: int):
"""断言状态码"""
assert response.status_code == expected_status, \
f"Expected status code {expected_status}, got {response.status_code}. Response: {response.text}"
@staticmethod
def assert_response_contains(response: Response, key: str, value: Any = None):
"""断言响应包含指定字段"""
data = response.json()
assert key in data, f"Response does not contain key '{key}'. Response: {data}"
if value is not None:
assert data[key] == value, \
f"Expected {value} for key '{key}', got {data[key]}"
@staticmethod
def assert_response_is_list(response: Response):
"""断言响应是列表"""
data = response.json()
assert isinstance(data, list), f"Expected list, got {type(data)}. Response: {data}"
@staticmethod
def assert_response_not_empty(response: Response):
"""断言响应不为空"""
data = response.json()
assert data, f"Response is empty. Response: {data}"
@staticmethod
def assert_response_field_type(response: Response, field: str, expected_type: type):
"""断言响应字段类型"""
data = response.json()
assert field in data, f"Response does not contain field '{field}'"
assert isinstance(data[field], expected_type), \
f"Expected field '{field}' to be {expected_type}, got {type(data[field])}"
@staticmethod
def assert_response_fields_present(response: Response, fields: List[str]):
"""断言响应包含所有指定字段"""
data = response.json()
missing_fields = [field for field in fields if field not in data]
assert not missing_fields, \
f"Response is missing fields: {missing_fields}. Response: {data}"
@staticmethod
def assert_response_field_length(response: Response, field: str, min_length: int = None, max_length: int = None):
"""断言响应字段长度"""
data = response.json()
assert field in data, f"Response does not contain field '{field}'"
field_value = data[field]
if isinstance(field_value, (str, list, dict)):
length = len(field_value)
if min_length is not None:
assert length >= min_length, \
f"Field '{field}' length {length} is less than minimum {min_length}"
if max_length is not None:
assert length <= max_length, \
f"Field '{field}' length {length} is greater than maximum {max_length}"
else:
raise AssertionError(f"Field '{field}' is not a string, list, or dict")
@staticmethod
def assert_error_response(response: Response, expected_message: str = None):
"""断言错误响应"""
Assertions.assert_status_code(response, 400)
if expected_message:
data = response.json()
assert expected_message in str(data), \
f"Expected error message '{expected_message}' not found in response: {data}"
assertions = Assertions()
@@ -0,0 +1,72 @@
"""
测试数据生成器
"""
import random
import string
from faker import Faker
class DataGenerator:
"""测试数据生成器"""
def __init__(self, locale: str = "zh_CN"):
self.faker = Faker(locale)
def generate_username(self) -> str:
"""生成用户名"""
return f"testuser_{''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}"
def generate_password(self, length: int = 12) -> str:
"""生成密码"""
chars = string.ascii_letters + string.digits + "!@#$%^&*"
return ''.join(random.choices(chars, k=length))
def generate_email(self) -> str:
"""生成邮箱"""
return self.faker.email()
def generate_phone(self) -> str:
"""生成手机号"""
return self.faker.phone_number()
def generate_name(self) -> str:
"""生成姓名"""
return self.faker.name()
def generate_role_name(self) -> str:
"""生成角色名"""
return f"ROLE_{''.join(random.choices(string.ascii_uppercase, k=6))}"
def generate_dict_type(self) -> str:
"""生成字典类型"""
return f"DICT_TYPE_{''.join(random.choices(string.ascii_uppercase, k=4))}"
def generate_dict_code(self) -> str:
"""生成字典编码"""
return f"CODE_{''.join(random.choices(string.ascii_uppercase + string.digits, k=6))}"
def generate_url(self) -> str:
"""生成URL"""
return self.faker.url()
def generate_company_name(self) -> str:
"""生成公司名"""
return self.faker.company()
def generate_address(self) -> str:
"""生成地址"""
return self.faker.address()
def generate_description(self) -> str:
"""生成描述"""
return self.faker.text(max_nb_chars=200)
def generate_permissions(self) -> str:
"""生成权限字符串"""
permissions = ["READ", "WRITE", "DELETE", "ADMIN", "MANAGE"]
selected = random.sample(permissions, random.randint(1, len(permissions)))
return ",".join(selected)
data_generator = DataGenerator()
+33
View File
@@ -0,0 +1,33 @@
"""
日志工具
"""
import sys
from loguru import logger
from pathlib import Path
def setup_logger(log_file: str = "e2e_tests.log", log_level: str = "INFO"):
"""配置日志"""
logger.remove()
logger.add(
sys.stdout,
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
level=log_level,
colorize=True
)
logger.add(
log_file,
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
level=log_level,
rotation="10 MB",
retention="7 days",
compression="zip"
)
return logger
setup_logger()
@@ -0,0 +1,204 @@
"""
测试数据管理工具(简化版)
"""
import asyncio
from typing import List, Dict, Any, Callable
from httpx import AsyncClient
from loguru import logger
class TestDataManager:
"""测试数据管理器"""
def __init__(self, client: AsyncClient):
self.client = client
self._users: List[int] = []
self._roles: List[int] = []
self._menus: List[int] = []
self._dictionaries: List[int] = []
self._dict_types: List[int] = []
self._configs: List[int] = []
self._notices: List[int] = []
self._files: List[int] = []
self._messages: List[int] = []
def add_user(self, user_id: int):
"""添加用户到清理列表"""
self._users.append(user_id)
def add_role(self, role_id: int):
"""添加角色到清理列表"""
self._roles.append(role_id)
def add_menu(self, menu_id: int):
"""添加菜单到清理列表"""
self._menus.append(menu_id)
def add_dictionary(self, dict_id: int):
"""添加字典到清理列表"""
self._dictionaries.append(dict_id)
def add_dict_type(self, dict_type_id: int):
"""添加字典类型到清理列表"""
self._dict_types.append(dict_type_id)
def add_config(self, config_id: int):
"""添加系统配置到清理列表"""
self._configs.append(config_id)
def add_notice(self, notice_id: int):
"""添加系统公告到清理列表"""
self._notices.append(notice_id)
def add_file(self, file_id: int):
"""添加文件到清理列表"""
self._files.append(file_id)
def add_message(self, message_id: int):
"""添加消息到清理列表"""
self._messages.append(message_id)
async def cleanup_all(self):
"""清理所有测试数据"""
logger.info("Starting test data cleanup...")
cleanup_tasks = []
if self._messages:
cleanup_tasks.extend([self._delete_message(msg_id) for msg_id in self._messages])
self._messages.clear()
if self._files:
cleanup_tasks.extend([self._delete_file(file_id) for file_id in self._files])
self._files.clear()
if self._notices:
cleanup_tasks.extend([self._delete_notice(notice_id) for notice_id in self._notices])
self._notices.clear()
if self._configs:
cleanup_tasks.extend([self._delete_config(config_id) for config_id in self._configs])
self._configs.clear()
if self._dictionaries:
cleanup_tasks.extend([self._delete_dictionary(dict_id) for dict_id in self._dictionaries])
self._dictionaries.clear()
if self._dict_types:
cleanup_tasks.extend([self._delete_dict_type(dict_type_id) for dict_type_id in self._dict_types])
self._dict_types.clear()
if self._users:
cleanup_tasks.extend([self._delete_user(user_id) for user_id in self._users])
self._users.clear()
if self._roles:
cleanup_tasks.extend([self._delete_role(role_id) for role_id in self._roles])
self._roles.clear()
if self._menus:
cleanup_tasks.extend([self._delete_menu(menu_id) for menu_id in self._menus])
self._menus.clear()
if cleanup_tasks:
results = await asyncio.gather(*cleanup_tasks, return_exceptions=True)
failed_count = sum(1 for r in results if isinstance(r, Exception))
if failed_count > 0:
logger.warning(f"Failed to cleanup {failed_count} resources")
logger.info("Test data cleanup completed")
async def _delete_user(self, user_id: int):
"""删除用户"""
try:
await self.client.delete(f"/api/users/{user_id}")
logger.info(f"Cleaned up user {user_id}")
except Exception as e:
logger.warning(f"Failed to cleanup user {user_id}: {e}")
async def _delete_role(self, role_id: int):
"""删除角色"""
try:
await self.client.delete(f"/api/roles/{role_id}")
logger.info(f"Cleaned up role {role_id}")
except Exception as e:
logger.warning(f"Failed to cleanup role {role_id}: {e}")
async def _delete_menu(self, menu_id: int):
"""删除菜单"""
try:
await self.client.delete(f"/api/menus/{menu_id}")
logger.info(f"Cleaned up menu {menu_id}")
except Exception as e:
logger.warning(f"Failed to cleanup menu {menu_id}: {e}")
async def _delete_dictionary(self, dict_id: int):
"""删除字典"""
try:
await self.client.delete(f"/api/dictionaries/{dict_id}")
logger.info(f"Cleaned up dictionary {dict_id}")
except Exception as e:
logger.warning(f"Failed to cleanup dictionary {dict_id}: {e}")
async def _delete_dict_type(self, dict_type_id: int):
"""删除字典类型"""
try:
await self.client.delete(f"/api/dict/types/{dict_type_id}")
logger.info(f"Cleaned up dict type {dict_type_id}")
except Exception as e:
logger.warning(f"Failed to cleanup dict type {dict_type_id}: {e}")
async def _delete_config(self, config_id: int):
"""删除系统配置"""
try:
await self.client.delete(f"/api/config/{config_id}")
logger.info(f"Cleaned up config {config_id}")
except Exception as e:
logger.warning(f"Failed to cleanup config {config_id}: {e}")
async def _delete_notice(self, notice_id: int):
"""删除系统公告"""
try:
await self.client.delete(f"/api/notices/{notice_id}")
logger.info(f"Cleaned up notice {notice_id}")
except Exception as e:
logger.warning(f"Failed to cleanup notice {notice_id}: {e}")
async def _delete_file(self, file_id: int):
"""删除文件"""
try:
await self.client.delete(f"/api/files/{file_id}")
logger.info(f"Cleaned up file {file_id}")
except Exception as e:
logger.warning(f"Failed to cleanup file {file_id}: {e}")
async def _delete_message(self, message_id: int):
"""删除消息"""
try:
await self.client.delete(f"/api/messages/{message_id}")
logger.info(f"Cleaned up message {message_id}")
except Exception as e:
logger.warning(f"Failed to cleanup message {message_id}: {e}")
def get_stats(self) -> Dict[str, int]:
"""获取统计信息"""
return {
"users": len(self._users),
"roles": len(self._roles),
"menus": len(self._menus),
"dictionaries": len(self._dictionaries),
"dict_types": len(self._dict_types),
"configs": len(self._configs),
"notices": len(self._notices),
"files": len(self._files),
"messages": len(self._messages)
}
def has_data(self) -> bool:
"""检查是否有待清理数据"""
return any([
self._users, self._roles, self._menus,
self._dictionaries, self._dict_types, self._configs,
self._notices, self._files, self._messages
])