test(p3-1): 第二十三波 - auth登录/刷新/登出use case 38个单测 #729

Merged
xiaoxia merged 2 commits from test/p3-1-wave23-cosyvoice-service-tests into develop 2026-07-22 22:41:24 +08:00
Owner

P3-1 单元测试补充 - 第二十三波

新增测试文件

  • tests/unit/test_auth_login_use_cases.py — 38个测试

覆盖范围

Helper functions (10个)

  • _is_legacy_sha256_hash — 7个用例(有效hex、bcrypt非legacy、空串、短字符串、非hex 64字符、纯hex 64字符、大小写混合hex)
  • _legacy_sha256 — 3个用例(匹配hashlib、空密码、unicode密码)

LoginRequest (4个)

  • email strip+lowercase、默认device_info、默认ip、自定义device/ip

LoginUseCase (11个)

  • 成功登录、带设备信息登录、空email、空密码、用户不存在、密码错误
  • legacy SHA256哈希登录成功并自动升级为bcrypt、legacy哈希密码错误不升级
  • 异常处理、JWT claims验证

RefreshTokenUseCase (8个)

  • 刷新成功、空token、无效token、session缺session_id、session缺user_id
  • token不匹配、用户不存在、异常处理

LogoutUseCase (6个)

  • 单设备登出成功、无session_id、session不存在、全设备登出
  • 全设备登出无需session_id、异常处理

累计

  • 第二十三波:38个
  • 累计:约1290+个单测
## P3-1 单元测试补充 - 第二十三波 ### 新增测试文件 - `tests/unit/test_auth_login_use_cases.py` — 38个测试 ### 覆盖范围 **Helper functions (10个)** - `_is_legacy_sha256_hash` — 7个用例(有效hex、bcrypt非legacy、空串、短字符串、非hex 64字符、纯hex 64字符、大小写混合hex) - `_legacy_sha256` — 3个用例(匹配hashlib、空密码、unicode密码) **LoginRequest (4个)** - email strip+lowercase、默认device_info、默认ip、自定义device/ip **LoginUseCase (11个)** - 成功登录、带设备信息登录、空email、空密码、用户不存在、密码错误 - legacy SHA256哈希登录成功并自动升级为bcrypt、legacy哈希密码错误不升级 - 异常处理、JWT claims验证 **RefreshTokenUseCase (8个)** - 刷新成功、空token、无效token、session缺session_id、session缺user_id - token不匹配、用户不存在、异常处理 **LogoutUseCase (6个)** - 单设备登出成功、无session_id、session不存在、全设备登出 - 全设备登出无需session_id、异常处理 ### 累计 - 第二十三波:38个 - 累计:约1290+个单测
xiaoxia added 1 commit 2026-07-22 18:28:04 +08:00
test(p3-1): add 38 unit tests for auth login/refresh/logout use cases
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 22s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 52s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 38s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 30s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 2m30s
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 40s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 44s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Failing after 55s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 58s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 9m47s
AI Code Review / AI Code Review (pull_request) Successful in 3m47s
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
6bdf72fbc3
Covers:
- _is_legacy_sha256_hash helper (7 cases)
- _legacy_sha256 helper (3 cases)
- LoginRequest (4 cases)
- LoginUseCase (11 cases: success, device/ip, empty email/password,
  user not found, wrong password, legacy sha256 upgrade, legacy wrong pw,
  exception handling, JWT claims verification)
- RefreshTokenUseCase (8 cases: success, empty token, invalid token,
  missing session_id/user_id, token mismatch, user not found, exception)
- LogoutUseCase (6 cases: single device success, no session_id,
  session not found, all devices, all with empty sid, exception)

🚀 预览环境已部署

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

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

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

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

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #729 | | 预览链接 | [https://pr-729.preview.xiaoxiajianji.com](https://pr-729.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
xiaoxia added 1 commit 2026-07-22 19:09:28 +08:00
fix(ci): auto-fix lint/format issues
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1m11s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 49s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 45s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 42s
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 1m25s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 3m33s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 1m27s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Failing after 1m3s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 44s
AI Code Review / AI Code Review (pull_request) Successful in 5m28s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 12m9s
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 / Unit Tests (pull_request) Successful in 1m35s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Failing after 3m15s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 27m13s
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
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m28s
512f685484
auto-approve-bot approved these changes 2026-07-22 19:39:20 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

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

CI全绿,自动审批通过。

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

代码审查结果 - PR #729

⚠️ 问题(0个需要修改)

💡 建议(3个可选)

  1. tests/unit/test_auth_login_use_cases.py 第78行:Mock类的delete_session方法逻辑存在瑕疵,在检查session是否存在之前就将其添加到了deleted_sessions列表中。这会导致即使删除不存在的session,列表中也会有记录,与返回值False的行为不一致,可能导致依赖该列表的测试误判。建议将self.deleted_sessions.append(session_id)移入if代码块内部。
  2. tests/unit/test_auth_login_use_cases.py 第295行:测试用例硬编码了过期时间30 * 24 * 3600。这使得测试与业务配置强耦合,如果未来修改了默认Token有效期,该测试会失败。建议改为仅验证expires_in_seconds > 0,或者从配置中读取该常量进行断言。
  3. tests/unit/test_auth_login_use_cases.py 第353行:在测试密码哈希升级时,断言startswith("$2b$")较为脆弱。Bcrypt哈希可能因库版本不同产生$2a$$2y$等前缀。由于下一行已经使用了password_hasher.verify_password进行功能性验证,建议删除该前缀断言以增强测试的健壮性。

格式检查通过 | 逻辑审查通过 | 性能良好


🤖 由 AI 代码审查机器人自动生成 | 2026-07-22 19:43:18 | 模型:

## 代码审查结果 - PR #729 ### ⚠️ 问题(0个需要修改) 无 ### 💡 建议(3个可选) 1. **tests/unit/test_auth_login_use_cases.py 第78行**:Mock类的`delete_session`方法逻辑存在瑕疵,在检查session是否存在之前就将其添加到了`deleted_sessions`列表中。这会导致即使删除不存在的session,列表中也会有记录,与返回值`False`的行为不一致,可能导致依赖该列表的测试误判。建议将`self.deleted_sessions.append(session_id)`移入`if`代码块内部。 2. **tests/unit/test_auth_login_use_cases.py 第295行**:测试用例硬编码了过期时间`30 * 24 * 3600`。这使得测试与业务配置强耦合,如果未来修改了默认Token有效期,该测试会失败。建议改为仅验证`expires_in_seconds > 0`,或者从配置中读取该常量进行断言。 3. **tests/unit/test_auth_login_use_cases.py 第353行**:在测试密码哈希升级时,断言`startswith("$2b$")`较为脆弱。Bcrypt哈希可能因库版本不同产生`$2a$`或`$2y$`等前缀。由于下一行已经使用了`password_hasher.verify_password`进行功能性验证,建议删除该前缀断言以增强测试的健壮性。 --- ✅ 格式检查通过 | ✅ 逻辑审查通过 | ✅ 性能良好 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-07-22 19:43:18 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia merged commit 7421a76e5a into develop 2026-07-22 22:41:24 +08:00

🗑️ 预览环境已清理

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

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

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