Skip to content

Task 4-2: Concurrent Login Test

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

Background

Test lock mechanism khi 2 request generate-code cùng verifier_id chạy đồng thời, và behavior khi login từ thiết bị mới.

Test scenarios

Test setup note: Concurrent test cần dùng ExecutorService hoặc CountDownLatch để gửi 2 request cùng lúc từ Java test:

ExecutorService executor = Executors.newFixedThreadPool(2);
CountDownLatch latch = new CountDownLatch(1);
Future<?> f1 = executor.submit(() -> { latch.await(); /* send request */ });
Future<?> f2 = executor.submit(() -> { latch.await(); /* send request */ });
latch.countDown(); // release both threads simultaneously
Import: java.util.concurrent.ExecutorService, java.util.concurrent.Executors, java.util.concurrent.CountDownLatch, java.util.concurrent.Future. Session isolation test cần tạo 2 site contexts khác nhau — kiểm tra xem siteId được truyền qua header hay JWT claim.

Concurrent generate-code (same verifier_id)

  • [ ] Gửi 2 request generate-code cùng lúc với cùng verifier_id
  • [ ] Verify: chỉ 1 user record được tạo trong DB (distributed lock hoạt động)
  • [ ] Verify: cả 2 request trả về code hợp lệ cho cùng 1 userId

New device login

  • [ ] User đã có session trên thiết bị A
  • [ ] Login từ thiết bị B → device token cũ (thiết bị A) bị xóa
  • [ ] Session thiết bị A bị invalidate → JWT cũ trả 401
  • [ ] Notification gửi tới thiết bị A (nếu có push notification)

Session isolation per site

  • [ ] Login vào site 1 → nhận JWT site 1
  • [ ] Login vào site 2 (cùng user) → nhận JWT site 2 độc lập
  • [ ] Logout site 1 → JWT site 1 invalid, JWT site 2 vẫn valid

Verification / Acceptance Criteria

  • [ ] Compile check: Test class compile thành công — ExecutorService, CountDownLatch, Future được import; mock beans (@MockBean) cho external services được khai báo đúng.
  • [ ] Distributed lock works: Sau 2 concurrent generate-code với cùng verifier_id, DB chỉ có đúng 1 UserModel record cho verifier đó.
  • [ ] New device invalidates old session: Login từ thiết bị B → JWT cũ (thiết bị A) trả HTTP 401 khi dùng.
  • [ ] New device token removed: Device token của thiết bị A bị xóa khỏi DB sau khi thiết bị B login.
  • [ ] Session isolation: Logout site 1 → JWT site 1 invalid (HTTP 401); JWT site 2 vẫn hoạt động (HTTP 200).
  • [ ] Test reference: Test class được đặt tên rõ ràng (ví dụ AuthConcurrentTest, AuthSessionIsolationTest) để dễ tìm kiếm.