Files
ColaFlow/colaflow-api/tests/Modules/Identity/ColaFlow.Modules.Identity.IntegrationTests/Infrastructure/DatabaseFixture.cs
Yaojia Wang dbbb49e5b6 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>
2025-11-03 17:38:40 +01:00

37 lines
1.1 KiB
C#

namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
/// <summary>
/// Database Fixture for In-Memory Database Tests
/// Implements IClassFixture for xUnit test lifecycle management
/// Each test class gets its own isolated database instance
/// </summary>
public class DatabaseFixture : IDisposable
{
public ColaFlowWebApplicationFactory Factory { 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);
}
/// <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()
{
Factory?.Dispose();
GC.SuppressFinalize(this);
}
}