Task 4-1: Integration Tests - Authentication Flows
Phase: 4 - Testing Priority: Medium Module:
authenticationDepends 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-testtrongpom.xml/build.gradle. - Nếu dùng Redis trong test: cần embedded Redis (it.ozimov:embedded-redishoặccom.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@MockBeanhoặc WireMock.
New user flow (WEB3_AUTH)
- [ ]
POST /api/auth/generate-codevới valid Web3Auth token → nhậnCodeLoginResponsevớiis_first_login=true - [ ] Verify DB:
UserModel,UserWalletModel,RUserModelđược tạo - [ ]
POST /api/auth/sign-invới code đúng → nhậnUserAuthResponse - [ ] JWT có thể dùng cho authenticated endpoints
Existing user flow
- [ ]
generate-codecùng email lần 2 → không tạo user mới - [ ]
is_first_login=falsetrong response - [ ] Wallet được update (không duplicate)
Banned user
- [ ] Set user
status=NO_ACTIVE→sign-intrảYOUR_ACCOUNT_HAS_BEEN_BANNED - [ ]
generate-codevẫn hoạt động (code được tạo) nhưng sign-in bị chặn
Invalid inputs
- [ ] Sai
login_type→INVALID_LOGIN_TYPE - [ ]
sign-inkhông cóx-api-keyheader →SECRET_KEY_IS_INVALID - [ ]
sign-invới saix-api-key→SECRET_KEY_IS_INVALID - [ ]
check-emailvới email không tồn tại →EMAIL_NOT_FOUND
Concurrent login
- [ ] 2 request
generate-codecùngverifier_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-codetrong 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ạymvn testhoặc./gradlew testcho moduleauthentication. - [ ] New user DB records: Sau
generate-codevới new verifier, DB có đúng 1 recordUserModel, 1UserWalletModel, 1RUserModel. - [ ] 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-inpass 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.