Skip to content

Task 4-3: Edge Case Tests

Phase: 4 - Testing Priority: Medium Module: authentication Depends 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ột Clock bean vào AuthService và 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ùng Clock, có thể trực tiếp set expireAuthCode về quá khứ khi tạo test data. - Provider token mocking: Mock external provider calls (Web3Auth validator, Immutable API client) bằng @MockBean hoặc WireMock. Import: org.mockito.Mockito, org.springframework.boot.test.mock.mockito.MockBean. - Wallet failure simulation: Mock WalletService.createSecondaryWallet() để throw RuntimeException; dùng @MockBean WalletService walletService.

Expired auth code

  • [ ] Generate code → wait until expireAuthCode passes → sign-in với code cũ → CODE_EXPIRED

Invalid provider token

  • [ ] generate-code với Web3Auth token không hợp lệ → provider-specific error
  • [ ] generate-code vớ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=DM2 flow (nếu handler tồn tại)
  • [ ] Test unsupported login_typeINVALID_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, mock Clock (nếu dùng), import java.time.Instant, org.mockito.Mockito không gây lỗi.
  • [ ] Expired code: sign-in với code đã hết hạn → HTTP 400 với error code CODE_EXPIRED (yêu cầu task-1-2 đã hoàn thành).
  • [ ] Invalid Web3Auth token: generate-code vớ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-in trả về thành công; retry được schedule (kiểm tra log hoặc mock invocation).
  • [ ] DM2 login: sign-in với login_type=DM2 (nếu handler tồn tại) → flow thành công; login_type khô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.