Task 4-3: Edge Case Tests
Phase: 4 - Testing Priority: Medium Module:
authenticationDepends on: task-4-2 Reference:docs/BountyHunter-Backend/details/feature-authentication/SPEC.md
Background
Test các edge case: expired code, invalid provider token, missing wallet.
Test scenarios
Test setup note: - Expired code test: Để tránh
Thread.sleep()lama, inject mộtClockbean vàoAuthServicevà mock nó trong test để simulate thời gian đã qua. Ví dụ:when(clock.instant()).thenReturn(Instant.now().plusSeconds(3600));. Nếu project chưa dùngClock, có thể trực tiếp setexpireAuthCodevề quá khứ khi tạo test data. - Provider token mocking: Mock external provider calls (Web3Auth validator, Immutable API client) bằng@MockBeanhoặc WireMock. Import:org.mockito.Mockito,org.springframework.boot.test.mock.mockito.MockBean. - Wallet failure simulation: MockWalletService.createSecondaryWallet()để throwRuntimeException; dùng@MockBean WalletService walletService.
Expired auth code
- [ ] Generate code → wait until
expireAuthCodepasses →sign-invới code cũ →CODE_EXPIRED
Invalid provider token
- [ ]
generate-codevới Web3Auth token không hợp lệ → provider-specific error - [ ]
generate-codevới Immutable token không hợp lệ → error từ Immutable API
Missing wallet
- [ ] Simulate wallet creation failure → verify user vẫn được tạo trong DB
- [ ] Verify retry mechanism kick in (từ task-3-1)
- [ ] Verify user có thể login dù chưa có secondary wallet (graceful degradation)
DM2 login (nếu applicable)
- [ ] Test
login_type=DM2flow (nếu handler tồn tại) - [ ] Test unsupported
login_type→INVALID_LOGIN_TYPE
check-email edge cases
- [ ] Email tồn tại → response có thông tin user
- [ ] Email không tồn tại →
EMAIL_NOT_FOUND - [ ] Email format invalid → validation error
Verification / Acceptance Criteria
- [ ] Compile check: Test class compile thành công —
@MockBean WalletService, mockClock(nếu dùng), importjava.time.Instant,org.mockito.Mockitokhông gây lỗi. - [ ] Expired code:
sign-invới code đã hết hạn → HTTP 400 với error codeCODE_EXPIRED(yêu cầu task-1-2 đã hoàn thành). - [ ] Invalid Web3Auth token:
generate-codevới token giả → provider-specific error (HTTP 400 hoặc 401). - [ ] Wallet failure graceful: Mock
WalletService.createSecondaryWallet()throw exception → user vẫn được tạo trong DB;sign-intrả về thành công; retry được schedule (kiểm tra log hoặc mock invocation). - [ ] DM2 login:
sign-invớilogin_type=DM2(nếu handler tồn tại) → flow thành công;login_typekhông hợp lệ →INVALID_LOGIN_TYPE. - [ ] check-email: Email tồn tại → HTTP 200 với user info; email không tồn tại → HTTP 404/400 với
EMAIL_NOT_FOUND; email format sai → HTTP 400 validation error. - [ ] Test reference: Test class được đặt tên rõ ràng (ví dụ
AuthEdgeCaseTest) để dễ tìm kiếm.