Files
Heimdall/tests/Heimdall.IntegrationTests/Factories/HeimdallWebApplicationFactory.cs
T
Arnaud Dartois ee7894646f test: remplace InMemory par Testcontainers PostgreSQL
- Ajoute Testcontainers.PostgreSql v4.13.0\n- PostgreSqlContainerFixture: collection fixture xUnit avec postgres:17-alpine\n- HeimdallWebApplicationFactory: accepte connectionString pour vrai PostgreSQL\n- PostgresTestBase: classe de base pour tous les tests integration\n- Ajoute [Collection("Postgres")] aux 9 classes de test\n- Database:MigrateOnStartup active quand PostgreSQL est utilise\n- Programme le Hangfire (skip en test via isTesting)\n\nAvant: InMemory (pas de contraintes, comportement different)\nApres: Vrai PostgreSQL dans un container temporaire\n\nTests: 226/226 passed (61 integration avec vrai PostgreSQL)
2026-07-21 10:24:11 +00:00

45 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
namespace Heimdall.IntegrationTests.Factories;
/// <summary>
/// WebApplicationFactory for integration tests.
/// By default creates a factory without a connection string.
/// Use <see cref="WithPostgreSql"/> to get a factory connected to a
/// real PostgreSQL Testcontainer (see PostgreSqlContainerFixture).
/// </summary>
public class HeimdallWebApplicationFactory : WebApplicationFactory<Program>
{
private readonly string? _connectionString;
/// <summary>Creates a factory. The SUT uses InMemory unless a connection string is provided.</summary>
public HeimdallWebApplicationFactory() { }
/// <summary>Creates a factory connected to a real PostgreSQL instance (e.g. Testcontainer).</summary>
public HeimdallWebApplicationFactory(string connectionString)
{
_connectionString = connectionString;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.UseSetting("Cors:AllowedOrigins", "*");
if (_connectionString is not null)
{
// Real PostgreSQL — override the InMemory provider from appsettings.Testing.json
builder.UseSetting("ConnectionStrings:Default", _connectionString);
builder.UseSetting("Database:Provider", "PostgreSQL");
builder.UseSetting("Database:MigrateOnStartup", "true");
}
// JWT config
builder.UseSetting("Jwt:Key", "ThisIsATestKeyThatIsAtLeast32BytesLong!");
builder.UseSetting("Jwt:RefreshKey", "ThisIsATestRefreshKeyThatIsAlso32Bytes!");
builder.UseSetting("Jwt:Issuer", "heimdall-test");
builder.UseSetting("Jwt:Audience", "heimdall-test");
}
}