using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Heimdall.Application.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Heimdall.IntegrationTests.Factories; /// /// WebApplicationFactory for integration tests. /// Requires a PostgreSQL connection string from . /// public class HeimdallWebApplicationFactory : WebApplicationFactory { private readonly string _connectionString; /// Creates a factory connected to a real PostgreSQL instance via Testcontainers. public HeimdallWebApplicationFactory(string connectionString) { _connectionString = connectionString; } protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Testing"); builder.UseSetting("Cors:AllowedOrigins", "*"); builder.UseSetting("ConnectionStrings:Default", _connectionString); 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"); // Stub HIBP: integration tests must never depend on an external breach-check // API nor on a test password that happens not to be in the HIBP database // (that made `api/auth/register` return 400: "password has appeared in a // data breach"). Force "not breached" in tests only — production code path // (HaveIBeenPwnedBreachChecker) is untouched. builder.ConfigureServices(services => { services.RemoveAll(); services.AddScoped(); }); } } /// Test-only stub: never flags a password as breached. Avoids network + HIBP flakiness. internal sealed class NoopPasswordBreachChecker : IPasswordBreachChecker { public Task IsPasswordBreachedAsync(string password, CancellationToken ct = default) => Task.FromResult(false); }