test(storage): P3-1第十二波 SharedStorageService单测 55个 #716

Merged
xiaoxia merged 4 commits from feat/p3-1-storage-tests into develop 2026-07-22 16:26:56 +08:00
Owner

P3-1 第十二波单元测试

新增

  • tests/unit/test_storage_service.py - 55个测试

覆盖范围

  • _normalize_storage_key (15个): 纯key/前导斜杠/http/https/带query/双斜杠/URL解码空格/中文/特殊字符/非URL不解码/空串/根路径/多级路径/带端口
  • _is_local_generated_url (6个): 本地前缀/相对路径/完整URL/其他路径/空串/自定义前缀
  • get_url (3个): 正常拼接/空key/自定义bucket+endpoint
  • create_direct_upload_post (13个): 返回结构/POST方法/URL/key归一化/必填字段/HMAC-SHA1签名验证/policy包含bucket&key/size限制/content-type前缀/过期时间/非uploads路径抛错/无凭证抛错/URL形式key归一化
  • get_download_url (5个): 无bucket本地URL透传/无bucket普通key/无bucket URL输入归一化/有bucket调sign_url/sign_url异常fallback
  • 无bucket错误处理 (4个): upload抛错/download抛错/delete静默/file_exists返回false
  • bucket操作调用验证 (6个): 路径上传/文件对象上传/delete调用/delete异常不抛/file_exists代理/false场景
  • 单例&兼容别名 (1个): get_storage_service是get_shared_storage_service别名
  • 初始化endpoint处理 (3个): 无https自动加前缀/已有https保留/已有http保留
## P3-1 第十二波单元测试 ### 新增 - `tests/unit/test_storage_service.py` - 55个测试 ### 覆盖范围 - **_normalize_storage_key** (15个): 纯key/前导斜杠/http/https/带query/双斜杠/URL解码空格/中文/特殊字符/非URL不解码/空串/根路径/多级路径/带端口 - **_is_local_generated_url** (6个): 本地前缀/相对路径/完整URL/其他路径/空串/自定义前缀 - **get_url** (3个): 正常拼接/空key/自定义bucket+endpoint - **create_direct_upload_post** (13个): 返回结构/POST方法/URL/key归一化/必填字段/HMAC-SHA1签名验证/policy包含bucket&key/size限制/content-type前缀/过期时间/非uploads路径抛错/无凭证抛错/URL形式key归一化 - **get_download_url** (5个): 无bucket本地URL透传/无bucket普通key/无bucket URL输入归一化/有bucket调sign_url/sign_url异常fallback - **无bucket错误处理** (4个): upload抛错/download抛错/delete静默/file_exists返回false - **bucket操作调用验证** (6个): 路径上传/文件对象上传/delete调用/delete异常不抛/file_exists代理/false场景 - **单例&兼容别名** (1个): get_storage_service是get_shared_storage_service别名 - **初始化endpoint处理** (3个): 无https自动加前缀/已有https保留/已有http保留
xiaoxia added 3 commits 2026-07-22 14:31:29 +08:00
test(p3-1): 第十波 - url_security 安全模块单测 72个
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 10s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 46s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 32s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 30s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 56s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 1m19s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m32s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 48s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 49s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m12s
AI Code Review / AI Code Review (pull_request) Successful in 5m35s
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
911478495a
覆盖 url_security.py 核心安全逻辑:
- validate_url_safety: scheme/端口/主机名/URL长度 等基础校验
- SSRF防护: 内网IP/回环/链路本地/组播/未指定/保留地址
- 内网主机名防护: localhost/.local/.internal/metadata
- 可信域名白名单: 精确匹配+子域名匹配
- is_url_safe 便捷函数
- 文件魔数校验: PNG/JPEG/GIF/WEBP/BMP/WAV/MP3/OGG/FLAC
- safe_download_file / safe_download_bytes (mock网络)
- 大小限制 / MIME类型白名单 / Content-Length预检
test(pagination): 补充通用分页器52个单元测试
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 24s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 47s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 26s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 25s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 41s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 46s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 50s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 37s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 5m59s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 6m6s
AI Code Review / AI Code Review (pull_request) Successful in 3m20s
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
a9bc314fed
覆盖 PaginationParams 默认值/校验/offset/limit、
PaginationMeta.from_params 边界场景、
PaginatedResponse.create 工厂方法、
paginate 内存分页函数
test(storage): 补充SharedStorageService 55个单元测试
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 21s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 1m29s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 27s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 49s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 41s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 54s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 1m39s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 52s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m16s
AI Code Review / AI Code Review (pull_request) Successful in 5m2s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 8m51s
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
92b8044cb3
覆盖 _normalize_storage_key/URL解码、_is_local_generated_url、
get_url、create_direct_upload_post/policy+HMAC签名验证、
get_download_url fallback、未配置OSS错误处理、
bucket操作调用验证、单例模式、endpoint https前缀处理

🚀 预览环境已部署

项目 详情
PR号 #716
预览链接 https://pr-716.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #716 | | 预览链接 | [https://pr-716.preview.xiaoxiajianji.com](https://pr-716.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
xiaoxia added 1 commit 2026-07-22 15:08:49 +08:00
fix(ci): auto-fix lint/format issues
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 56s
AI Code Review / AI Code Review (pull_request) Successful in 6m44s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 11m3s
CI/CD Pipeline / Check if frontend-only change (pull_request) Has been skipped
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been skipped
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Successful in 16s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m7s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 24m42s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 35m23s
PR Automation / Auto Approve on CI Green (pull_request) Has been skipped
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 6s
44441dc43f
auto-approve-bot approved these changes 2026-07-22 15:45:28 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot approved these changes 2026-07-22 15:45:30 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
Collaborator

📊 审查概览

  • 整体评价:通过
  • 严重问题数量:0 个
  • 一般建议数量:2 个

需修改的问题(严重)

💡 改进建议(一般)

  1. [tests/unit/test_storage_service.py: 68] Python 版本兼容性建议

    • 具体内容:在 _make_service 函数中使用了 with (patch(...), patch.dict(...)): 的括号上下文管理器语法。该语法仅在 Python 3.10+ 版本中支持。如果项目需要兼容 Python 3.9 或更早版本,此处会导致语法错误。建议确认项目的最低 Python 版本要求,或改用传统的嵌套 with 写法。
  2. [tests/unit/test_storage_service.py: 490] 增强日志验证

    • 具体内容:在 test_delete_file_exception_logged_not_raised 测试中,仅验证了异常未抛出,但注释提到“logged”。建议引入 pytest 的 caplog fixture 来显式验证日志中确实记录了该异常信息,以确保错误处理逻辑的完整性。

良好实践

  1. 测试覆盖全面test_pagination.py 对边界条件(如 total=0, page 超出范围)和 test_storage_service.py 对 URL 规范化、签名算法的覆盖非常详尽,特别是手动复现 HMAC-SHA1 签名来验证 OSS 签名逻辑,体现了高质量的测试深度。
  2. Mock 使用得当:在 test_storage_service.py 中,对 oss2 和环境变量的 mock 封装在 _make_service fixture 中,有效避免了重复代码,保持了测试的整洁性。
  3. 关注副作用test_does_not_mutate_original_list 测试验证了分页函数不会修改原始列表,这是纯函数测试中非常重要的细节。

注意:由于 Diff 被截断,仅审查了 test_pagination.pytest_storage_service.py 的前半部分以及 test_url_security.py 的文件头。基于已审查的代码部分,未发现逻辑错误或安全隐患。


🤖 由 AI 代码审查机器人自动生成 | 2026-07-22 15:51:04 | 模型:

### 📊 审查概览 - 整体评价:通过 - 严重问题数量:0 个 - 一般建议数量:2 个 ### ❌ 需修改的问题(严重) 无 ### 💡 改进建议(一般) 1. **[tests/unit/test_storage_service.py: 68] Python 版本兼容性建议** - 具体内容:在 `_make_service` 函数中使用了 `with (patch(...), patch.dict(...)):` 的括号上下文管理器语法。该语法仅在 Python 3.10+ 版本中支持。如果项目需要兼容 Python 3.9 或更早版本,此处会导致语法错误。建议确认项目的最低 Python 版本要求,或改用传统的嵌套 `with` 写法。 2. **[tests/unit/test_storage_service.py: 490] 增强日志验证** - 具体内容:在 `test_delete_file_exception_logged_not_raised` 测试中,仅验证了异常未抛出,但注释提到“logged”。建议引入 pytest 的 `caplog` fixture 来显式验证日志中确实记录了该异常信息,以确保错误处理逻辑的完整性。 ### ✅ 良好实践 1. **测试覆盖全面**:`test_pagination.py` 对边界条件(如 total=0, page 超出范围)和 `test_storage_service.py` 对 URL 规范化、签名算法的覆盖非常详尽,特别是手动复现 HMAC-SHA1 签名来验证 OSS 签名逻辑,体现了高质量的测试深度。 2. **Mock 使用得当**:在 `test_storage_service.py` 中,对 `oss2` 和环境变量的 mock 封装在 `_make_service` fixture 中,有效避免了重复代码,保持了测试的整洁性。 3. **关注副作用**:`test_does_not_mutate_original_list` 测试验证了分页函数不会修改原始列表,这是纯函数测试中非常重要的细节。 --- **注意**:由于 Diff 被截断,仅审查了 `test_pagination.py` 和 `test_storage_service.py` 的前半部分以及 `test_url_security.py` 的文件头。基于已审查的代码部分,未发现逻辑错误或安全隐患。 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-07-22 15:51:04 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia merged commit 576722f60b into develop 2026-07-22 16:26:56 +08:00
xiaoxia deleted branch feat/p3-1-storage-tests 2026-07-22 16:26:56 +08:00

🗑️ 预览环境已清理

PR #716 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #716 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.