fix(backend): Fix integration test failures - GlobalExceptionHandler and test isolation

Fixed 8 failing integration tests by addressing two root causes:

1. GlobalExceptionHandler returning incorrect HTTP status codes
   - Added handling for UnauthorizedAccessException → 401
   - Added handling for ArgumentException/InvalidOperationException → 400
   - Added handling for DbUpdateException (duplicate key) → 409
   - Now correctly maps exception types to HTTP status codes

2. Test isolation issue with shared HttpClient
   - Modified DatabaseFixture to create new HttpClient for each test
   - Prevents Authorization header pollution between tests
   - Ensures clean test state for authentication tests

Test Results:
- Before: 23/31 passed (8 failed)
- After: 31/31 passed (0 failed)

Changes:
- Enhanced GlobalExceptionHandler with proper status code mapping
- Fixed DatabaseFixture.Client to create isolated instances
- All authentication and RBAC tests now pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Yaojia Wang
2025-11-03 17:38:40 +01:00
parent 4183b10b39
commit dbbb49e5b6
2 changed files with 82 additions and 4 deletions

View File

@@ -8,18 +8,28 @@ namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
public class DatabaseFixture : IDisposable
{
public ColaFlowWebApplicationFactory Factory { get; }
public HttpClient Client { get; }
// Note: Client property is kept for backward compatibility but creates new instances
// Tests should call CreateClient() for isolation to avoid shared state issues
public HttpClient Client => CreateClient();
public DatabaseFixture()
{
// Use In-Memory Database for fast, isolated tests
Factory = new ColaFlowWebApplicationFactory(useInMemoryDatabase: true);
Client = Factory.CreateClient();
}
/// <summary>
/// Creates a new HttpClient for each test to ensure test isolation
/// Prevents Authorization header sharing between tests
/// </summary>
public HttpClient CreateClient()
{
return Factory.CreateClient();
}
public void Dispose()
{
Client?.Dispose();
Factory?.Dispose();
GC.SuppressFinalize(this);
}