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)
This commit is contained in:
1 parent
e309611f80
commit
ee7894646f
13 files changed
+145
-66
No files matched your search
@@ -6,16 +6,14 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class AgentsControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class AgentsControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
|
||||
public AgentsControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public AgentsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
[Fact]
|
||||
public async Task RegisterAgent_WithValidToken_ReturnsApiKey()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
|
||||
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
||||
{
|
||||
@@ -44,7 +42,7 @@ public class AgentsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
[Fact]
|
||||
public async Task AgentHeartbeat_WithApiKey_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
|
||||
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
||||
{
|
||||
@@ -67,7 +65,7 @@ public class AgentsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
var agentId = agentContent.GetProperty("agentId").GetGuid();
|
||||
var apiKey = agentContent.GetProperty("apiKey").GetString()!;
|
||||
|
||||
var heartbeatClient = _factory.CreateClient();
|
||||
var heartbeatClient = Factory.CreateClient();
|
||||
heartbeatClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", apiKey);
|
||||
var heartbeatResponse = await heartbeatClient.PostAsJsonAsync(
|
||||
@@ -78,7 +76,7 @@ public class AgentsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
[Fact]
|
||||
public async Task RegisterAgent_WithInvalidToken_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/agents/register", new
|
||||
{
|
||||
token = "invalid-token",
|
||||
|
||||
@@ -5,24 +5,23 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class AssignmentsControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class AssignmentsControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
private HttpClient? _cachedClient;
|
||||
|
||||
public AssignmentsControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public AssignmentsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
private async Task<HttpClient> AdminClient()
|
||||
{
|
||||
if (_cachedClient is not null) return _cachedClient;
|
||||
_cachedClient = await _factory.CreateAdminClientAsync();
|
||||
_cachedClient = await Factory.CreateAdminClientAsync();
|
||||
return _cachedClient;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AssignCamera_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/assignments", new
|
||||
{
|
||||
cameraId = Guid.NewGuid(),
|
||||
|
||||
@@ -5,15 +5,15 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class AuthControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class AuthControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private static readonly SemaphoreSlim _seedLock = new(1, 1);
|
||||
private static bool _seeded;
|
||||
|
||||
public AuthControllerTests(HeimdallWebApplicationFactory factory)
|
||||
public AuthControllerTests(PostgreSqlContainerFixture postgres) : base(postgres)
|
||||
{
|
||||
_client = factory.CreateClient();
|
||||
_client = Factory.CreateClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -5,16 +5,14 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class CamerasControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class CamerasControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
|
||||
public CamerasControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public CamerasControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
[Fact]
|
||||
public async Task CreateCamera_WithAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
|
||||
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
||||
{
|
||||
@@ -38,7 +36,7 @@ public class CamerasControllerTests : IClassFixture<HeimdallWebApplicationFactor
|
||||
[Fact]
|
||||
public async Task ListCameras_BySite_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var response = await client.GetAsync($"api/cameras/by-site/{Guid.NewGuid()}?page=1&pageSize=10");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
@@ -11,19 +11,18 @@ namespace Heimdall.IntegrationTests.Controllers;
|
||||
/// timeouts in unit tests. These tests focus on the API contract and
|
||||
/// non-network operations.
|
||||
/// </summary>
|
||||
public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class DiscoveryControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
private HttpClient? _cachedAdminClient;
|
||||
|
||||
public DiscoveryControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public DiscoveryControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
/// <summary>Shared admin client — reused to avoid rate-limit exhaustion.</summary>
|
||||
private async Task<HttpClient> AdminClientAsync()
|
||||
{
|
||||
if (_cachedAdminClient is not null)
|
||||
return _cachedAdminClient;
|
||||
_cachedAdminClient = await _factory.CreateAdminClientAsync();
|
||||
_cachedAdminClient = await Factory.CreateAdminClientAsync();
|
||||
return _cachedAdminClient;
|
||||
}
|
||||
|
||||
@@ -134,7 +133,7 @@ public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFact
|
||||
[Fact]
|
||||
public async Task ConfirmDiscovery_WithoutAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/discovery/confirm", new
|
||||
{
|
||||
siteId = Guid.NewGuid(),
|
||||
@@ -149,7 +148,7 @@ public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFact
|
||||
[Fact]
|
||||
public async Task ProbeIp_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/discovery/probe", new
|
||||
{
|
||||
siteId = Guid.NewGuid(),
|
||||
@@ -162,7 +161,7 @@ public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFact
|
||||
[Fact]
|
||||
public async Task SearchDatabase_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.GetAsync("api/discovery/search?q=test");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||||
}
|
||||
@@ -186,7 +185,7 @@ public class DiscoveryControllerTests : IClassFixture<HeimdallWebApplicationFact
|
||||
[Fact]
|
||||
public async Task CheckDuplicate_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/discovery/check", new
|
||||
{
|
||||
siteId = Guid.NewGuid(),
|
||||
|
||||
@@ -5,17 +5,16 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class EventsControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class EventsControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
private HttpClient? _cachedClient;
|
||||
|
||||
public EventsControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public EventsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
private async Task<HttpClient> AdminClient()
|
||||
{
|
||||
if (_cachedClient is not null) return _cachedClient;
|
||||
_cachedClient = await _factory.CreateAdminClientAsync();
|
||||
_cachedClient = await Factory.CreateAdminClientAsync();
|
||||
return _cachedClient;
|
||||
}
|
||||
|
||||
@@ -23,7 +22,7 @@ public class EventsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
public async Task CreateEvent_WithoutAuth_ReturnsUnauthorized()
|
||||
{
|
||||
// Events endpoint requires ApiKey auth (agents), not JWT
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/events", new
|
||||
{
|
||||
cameraId = Guid.NewGuid(),
|
||||
@@ -36,7 +35,7 @@ public class EventsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
[Fact]
|
||||
public async Task GetEventById_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.GetAsync($"api/events/{Guid.NewGuid()}");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||||
}
|
||||
@@ -44,7 +43,7 @@ public class EventsControllerTests : IClassFixture<HeimdallWebApplicationFactory
|
||||
[Fact]
|
||||
public async Task QueryEvents_RequiresAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.GetAsync("api/events");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using Heimdall.IntegrationTests.Factories;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for integration tests that need a real PostgreSQL database
|
||||
/// via Testcontainers. All test classes in the "Postgres" collection share
|
||||
/// the same PostgreSQL container.
|
||||
/// </summary>
|
||||
[Collection("Postgres")]
|
||||
public abstract class PostgresTestBase : IAsyncLifetime
|
||||
{
|
||||
private readonly Lazy<HeimdallWebApplicationFactory> _factoryLazy;
|
||||
|
||||
protected PostgresTestBase(PostgreSqlContainerFixture postgres)
|
||||
{
|
||||
_factoryLazy = new Lazy<HeimdallWebApplicationFactory>(
|
||||
() => new HeimdallWebApplicationFactory(postgres.ConnectionString));
|
||||
}
|
||||
|
||||
/// <summary>HTTP client factory connected to the real PostgreSQL Testcontainer.</summary>
|
||||
protected HeimdallWebApplicationFactory Factory => _factoryLazy.Value;
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (_factoryLazy.IsValueCreated)
|
||||
await _factoryLazy.Value.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -12,16 +12,15 @@ namespace Heimdall.IntegrationTests.Controllers;
|
||||
/// NOTE: Rate limit is 10 auth req/min so we cache admin + viewer tokens
|
||||
/// statically (xUnit creates a new class instance per test).
|
||||
/// </summary>
|
||||
public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class SettingsControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
private static string? _adminToken;
|
||||
private static string? _viewerToken;
|
||||
private static readonly object TokenLock = new();
|
||||
|
||||
public SettingsControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public SettingsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
/// <summary>Get a shared admin-authenticated HttpClient (lazy, cached token).</summary>
|
||||
private async Task<HttpClient> GetAdminClientAsync()
|
||||
@@ -34,7 +33,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
token = _adminToken!;
|
||||
}
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
return client;
|
||||
}
|
||||
@@ -50,7 +49,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
token = _viewerToken!;
|
||||
}
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
return client;
|
||||
}
|
||||
@@ -58,7 +57,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
private async Task<string> RegisterAdminAsync()
|
||||
{
|
||||
var email = $"admin-{Guid.NewGuid()}@heimdall.test";
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/auth/register-admin", new
|
||||
{
|
||||
email,
|
||||
@@ -75,7 +74,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
await EnsurePublicRegistrationAsync();
|
||||
|
||||
var email = $"viewer-{Guid.NewGuid()}@heimdall.test";
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.PostAsJsonAsync("api/auth/register", new
|
||||
{
|
||||
email,
|
||||
@@ -95,7 +94,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
if (_seeded) return;
|
||||
lock (_seedLock) { if (_seeded) return; }
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
|
||||
// First registration is auto-admin (no prior users in the InMemory DB)
|
||||
var adminEmail = $"seed-admin-{Guid.NewGuid()}@heimdall.test";
|
||||
@@ -109,7 +108,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
var token = content.GetProperty("accessToken").GetString()!;
|
||||
|
||||
// Use admin token to create the public registration setting
|
||||
var adminClient = _factory.CreateClient();
|
||||
var adminClient = Factory.CreateClient();
|
||||
adminClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
await adminClient.PostAsJsonAsync("api/settings", new
|
||||
{
|
||||
@@ -156,7 +155,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
[Fact]
|
||||
public async Task ListAll_WithoutAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
|
||||
var response = await client.GetAsync("api/settings");
|
||||
|
||||
@@ -176,7 +175,7 @@ public class SettingsControllerTests : IClassFixture<HeimdallWebApplicationFacto
|
||||
[Fact]
|
||||
public async Task CreateSetting_WithoutAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
|
||||
var response = await client.PostAsJsonAsync("api/settings", new
|
||||
{
|
||||
|
||||
@@ -4,16 +4,14 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class SitesControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class SitesControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
|
||||
public SitesControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public SitesControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
[Fact]
|
||||
public async Task GetSites_WithoutAuth_ReturnsUnauthorized()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var client = Factory.CreateClient();
|
||||
var response = await client.GetAsync("api/sites?page=1&pageSize=10");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||||
}
|
||||
@@ -21,7 +19,7 @@ public class SitesControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
[Fact]
|
||||
public async Task CreateSite_WithAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var response = await client.PostAsJsonAsync("api/sites", new
|
||||
{
|
||||
name = "Site Test",
|
||||
@@ -36,7 +34,7 @@ public class SitesControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
[Fact]
|
||||
public async Task ListSites_WithAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var response = await client.GetAsync("api/sites?page=1&pageSize=10");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
@@ -5,16 +5,14 @@ using Shouldly;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
public class UsersControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
public class UsersControllerTests : PostgresTestBase
|
||||
{
|
||||
private readonly HeimdallWebApplicationFactory _factory;
|
||||
|
||||
public UsersControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||||
public UsersControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||||
|
||||
[Fact]
|
||||
public async Task ListUsers_WithAdminAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var response = await client.GetAsync("api/users?page=1&pageSize=20");
|
||||
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||||
}
|
||||
@@ -22,7 +20,7 @@ public class UsersControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
[Fact]
|
||||
public async Task CreateUser_WithAdminAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var response = await client.PostAsJsonAsync("api/users", new
|
||||
{
|
||||
email = $"newuser-{Guid.NewGuid()}@heimdall.test",
|
||||
@@ -36,7 +34,7 @@ public class UsersControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
[Fact]
|
||||
public async Task LockUser_WithAdminAuth_ReturnsOk()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var createResponse = await client.PostAsJsonAsync("api/users", new
|
||||
{
|
||||
email = $"lockuser-{Guid.NewGuid()}@heimdall.test",
|
||||
@@ -54,7 +52,7 @@ public class UsersControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||||
[Fact]
|
||||
public async Task ResetPassword_WithAdminAuth_ReturnsTemporaryPassword()
|
||||
{
|
||||
var client = await _factory.CreateAdminClientAsync();
|
||||
var client = await Factory.CreateAdminClientAsync();
|
||||
var createResponse = await client.PostAsJsonAsync("api/users", new
|
||||
{
|
||||
email = $"resetpwd-{Guid.NewGuid()}@heimdall.test",
|
||||
|
||||
@@ -5,17 +5,36 @@ namespace Heimdall.IntegrationTests.Factories;
|
||||
|
||||
/// <summary>
|
||||
/// WebApplicationFactory for integration tests.
|
||||
/// Uses unique InMemory database name per instance to prevent test pollution.
|
||||
/// 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 _dbName = $"HeimdallTest_{Guid.NewGuid():N}";
|
||||
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", "*");
|
||||
builder.UseSetting("InMemoryDbName", _dbName);
|
||||
|
||||
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!");
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using Testcontainers.PostgreSql;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Factories;
|
||||
|
||||
/// <summary>
|
||||
/// xUnit collection fixture that manages a PostgreSQL Testcontainer
|
||||
/// for integration tests. One container per test collection (shared
|
||||
/// across all test classes in the same collection).
|
||||
/// </summary>
|
||||
public class PostgreSqlContainerFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly PostgreSqlContainer _container = new PostgreSqlBuilder("postgres:17-alpine")
|
||||
.WithDatabase("heimdall_test")
|
||||
.WithUsername("heimdall")
|
||||
.WithPassword("heimdall_test_pwd")
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
|
||||
/// <summary>Connection string for the running PostgreSQL container.</summary>
|
||||
public string ConnectionString => _container.GetConnectionString();
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _container.StartAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _container.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the "Postgres" test collection. All test classes in this
|
||||
/// collection share the same PostgreSQL container via the fixture.
|
||||
/// </summary>
|
||||
[CollectionDefinition("Postgres")]
|
||||
public class PostgresCollectionFixture : ICollectionFixture<PostgreSqlContainerFixture>
|
||||
{
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="NSubstitute" Version="6.0.0" />
|
||||
<PackageReference Include="Shouldly" Version="4.3.0" />
|
||||
<PackageReference Include="Testcontainers.PostgreSql" Version="4.13.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in new issue
Block a user