2026-07-16 21:19:16 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using Heimdall.IntegrationTests.Factories;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public class AuthControllerTests : PostgresTestBase
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
2026-07-22 19:31:51 +00:00
|
|
|
private HttpClient? _client;
|
|
|
|
|
private HttpClient Client => _client ?? throw new InvalidOperationException("Client not initialized");
|
2026-07-20 09:50:43 +00:00
|
|
|
private static readonly SemaphoreSlim _seedLock = new(1, 1);
|
|
|
|
|
private static bool _seeded;
|
2026-10-27 22:19:52 +01:00
|
|
|
private static string _adminEmail = "";
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-22 19:31:51 +00:00
|
|
|
public AuthControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clear static cache and create HTTP client AFTER DB reset.
|
|
|
|
|
/// Creating Factory.CreateClient() before InitializeAsync would start the
|
|
|
|
|
/// server (with role seeding) but then ResetDatabaseAsync() truncates the
|
|
|
|
|
/// seeded roles — causing "Role ADMIN does not exist" on first request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override async Task InitializeAsync()
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
2026-07-22 19:31:51 +00:00
|
|
|
await base.InitializeAsync();
|
|
|
|
|
_seeded = false;
|
2026-07-21 10:24:11 +00:00
|
|
|
_client = Factory.CreateClient();
|
2026-07-16 21:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-20 09:50:43 +00:00
|
|
|
/// <summary>
|
2026-10-27 22:19:52 +01:00
|
|
|
/// Creates the first admin through the one-time setup endpoint.
|
2026-07-20 09:50:43 +00:00
|
|
|
/// Runs once per test class (factory is shared).
|
|
|
|
|
/// </summary>
|
2026-10-27 22:19:52 +01:00
|
|
|
private async Task EnsureAdminCreatedAsync()
|
2026-07-20 09:50:43 +00:00
|
|
|
{
|
|
|
|
|
if (_seeded) return;
|
|
|
|
|
await _seedLock.WaitAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_seeded) return;
|
|
|
|
|
|
2026-07-21 12:19:43 +00:00
|
|
|
// First registration is auto-admin (no prior users in the DB)
|
2026-07-20 09:50:43 +00:00
|
|
|
var adminEmail = $"admin-{Guid.NewGuid()}@heimdall.test";
|
2026-10-27 22:19:52 +01:00
|
|
|
_adminEmail = adminEmail;
|
|
|
|
|
var regResponse = await Client.PostAsJsonAsync("api/auth/bootstrap-admin", new
|
2026-07-20 09:50:43 +00:00
|
|
|
{
|
|
|
|
|
email = adminEmail,
|
|
|
|
|
password = "TestPassword123!",
|
|
|
|
|
displayName = "Admin"
|
|
|
|
|
});
|
|
|
|
|
regResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
// Extract access token from the registration response
|
|
|
|
|
var authResult = await regResponse.Content.ReadFromJsonAsync<AuthTestResponse>();
|
|
|
|
|
authResult.ShouldNotBeNull();
|
|
|
|
|
|
|
|
|
|
_seeded = true;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_seedLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
[Fact]
|
2026-10-27 22:19:52 +01:00
|
|
|
public async Task AnonymousRegistrationRoute_IsRemoved()
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
await EnsureAdminCreatedAsync();
|
2026-07-20 09:50:43 +00:00
|
|
|
|
2026-07-22 19:31:51 +00:00
|
|
|
var response = await Client.PostAsJsonAsync("api/auth/register", new
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
|
|
|
|
email = $"test-{Guid.NewGuid()}@heimdall.test",
|
|
|
|
|
password = "TestPassword123!",
|
|
|
|
|
displayName = "Test User"
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-27 22:19:52 +01:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
|
2026-07-16 21:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-27 22:19:52 +01:00
|
|
|
public async Task BootstrapAdmin_AfterSetup_ReturnsConflict()
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
await EnsureAdminCreatedAsync();
|
2026-07-20 09:50:43 +00:00
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
var email = $"dup-{Guid.NewGuid()}@heimdall.test";
|
|
|
|
|
|
2026-10-27 22:19:52 +01:00
|
|
|
var second = await Client.PostAsJsonAsync("api/auth/bootstrap-admin", new
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "TestPassword123!",
|
|
|
|
|
displayName = "User 2"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
second.StatusCode.ShouldBe(System.Net.HttpStatusCode.Conflict);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 22:19:52 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task ConcurrentBootstrapRequests_CreateExactlyOneAdministrator()
|
|
|
|
|
{
|
|
|
|
|
var requests = Enumerable.Range(0, 3).Select(index =>
|
|
|
|
|
Client.PostAsJsonAsync("api/auth/bootstrap-admin", new
|
|
|
|
|
{
|
|
|
|
|
email = $"concurrent-{index}-{Guid.NewGuid():N}@heimdall.test",
|
|
|
|
|
password = "ConcurrentTest!Password123",
|
|
|
|
|
displayName = $"Admin {index}"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
var responses = await Task.WhenAll(requests);
|
|
|
|
|
responses.Count(response => response.StatusCode == System.Net.HttpStatusCode.OK).ShouldBe(1);
|
|
|
|
|
responses.Count(response => response.StatusCode == System.Net.HttpStatusCode.Conflict).ShouldBe(2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_ValidCredentials_ReturnsOk()
|
|
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
await EnsureAdminCreatedAsync();
|
2026-07-20 09:50:43 +00:00
|
|
|
|
2026-10-27 22:19:52 +01:00
|
|
|
var email = _adminEmail;
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-22 19:31:51 +00:00
|
|
|
var response = await Client.PostAsJsonAsync("api/auth/login", new
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "TestPassword123!"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Login_InvalidPassword_ReturnsUnauthorized()
|
|
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
await EnsureAdminCreatedAsync();
|
2026-07-20 09:50:43 +00:00
|
|
|
|
2026-10-27 22:19:52 +01:00
|
|
|
var email = _adminEmail;
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-22 19:31:51 +00:00
|
|
|
var response = await Client.PostAsJsonAsync("api/auth/login", new
|
2026-07-16 21:19:16 +00:00
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "WrongPassword!"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
2026-07-20 09:50:43 +00:00
|
|
|
|
|
|
|
|
/// <summary>DTO matching the API auth response for extracting the access token.</summary>
|
|
|
|
|
private sealed record AuthTestResponse(
|
|
|
|
|
string AccessToken,
|
|
|
|
|
string RefreshToken,
|
|
|
|
|
DateTime ExpiresAt,
|
|
|
|
|
bool Requires2fa,
|
|
|
|
|
UserInfo? User);
|
|
|
|
|
|
|
|
|
|
private sealed record UserInfo(Guid Id, string Email, string DisplayName, IList<string>? Roles);
|
2026-07-16 21:19:16 +00:00
|
|
|
}
|