Skip to content

Task 4-1: Integration Tests - Authentication Flows

Phase: 4 - Testing Priority: Medium Module: authentication Depends on: task-1-1 Reference: docs/BountyHunter-Backend/details/feature-authentication/SPEC.md

Background

Cần test coverage đầy đủ cho tất cả auth flows sau khi security hardening.

Test scenarios

Test setup note: Test class nên dùng @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) hoặc @AutoConfigureMockMvc để test toàn bộ HTTP stack. Các dependencies cần có: - spring-boot-starter-test trong pom.xml/build.gradle. - Nếu dùng Redis trong test: cần embedded Redis (it.ozimov:embedded-redis hoặc com.github.kstyrc:embedded-redis) hoặc Testcontainers Redis. - Nếu dùng DB thật: cần Testcontainers PostgreSQL/MySQL hoặc H2 in-memory (kiểm tra compatibility với JPA entities). - Mock external providers (Web3Auth, Immutable API) bằng @MockBean hoặc WireMock.

New user flow (WEB3_AUTH)

  • [ ] POST /api/auth/generate-code với valid Web3Auth token → nhận CodeLoginResponse với is_first_login=true
  • [ ] Verify DB: UserModel, UserWalletModel, RUserModel được tạo
  • [ ] POST /api/auth/sign-in với code đúng → nhận UserAuthResponse
  • [ ] JWT có thể dùng cho authenticated endpoints

Existing user flow

  • [ ] generate-code cùng email lần 2 → không tạo user mới
  • [ ] is_first_login=false trong response
  • [ ] Wallet được update (không duplicate)

Banned user

  • [ ] Set user status=NO_ACTIVEsign-in trả YOUR_ACCOUNT_HAS_BEEN_BANNED
  • [ ] generate-code vẫn hoạt động (code được tạo) nhưng sign-in bị chặn

Invalid inputs

  • [ ] Sai login_typeINVALID_LOGIN_TYPE
  • [ ] sign-in không có x-api-key header → SECRET_KEY_IS_INVALID
  • [ ] sign-in với sai x-api-keySECRET_KEY_IS_INVALID
  • [ ] check-email với email không tồn tại → EMAIL_NOT_FOUND

Concurrent login

  • [ ] 2 request generate-code cùng verifier_id đồng thời → chỉ tạo 1 user (lock mechanism)
  • [ ] Login thiết bị mới → device token cũ bị xóa, notification gửi tới thiết bị cũ

Logout

  • [ ] DELETE /api/auth/logout/{firebaseToken} → device token bị xóa khỏi DB + session Redis invalidated
  • [ ] JWT từ session đã invalidate → 401 khi dùng lại

Rate limit (sau task-1-1)

  • [ ] 2 request generate-code trong 5 giây → lần 2 nhận 429

Verification / Acceptance Criteria

  • [ ] Compile check: Test class compile thành công — test dependencies (spring-boot-starter-test, embedded Redis/Testcontainers) có trong build file; mock beans được khai báo đúng.
  • [ ] All scenarios green: Tất cả [ ] scenarios ở trên pass khi chạy mvn test hoặc ./gradlew test cho module authentication.
  • [ ] New user DB records: Sau generate-code với new verifier, DB có đúng 1 record UserModel, 1 UserWalletModel, 1 RUserModel.
  • [ ] Rate limit enforced: Test rate limit (2 requests < 5s) → HTTP 429 trên request thứ 2 (yêu cầu task-1-1 đã hoàn thành).
  • [ ] JWT validity: JWT nhận được từ sign-in pass xác thực trên authenticated endpoint (HTTP 200); JWT từ session đã logout trả HTTP 401.
  • [ ] Test class reference: Xác nhận tên class test (ví dụ AuthIntegrationTest) và package để AI agent có thể tìm file dễ dàng.