Anomalies corrigées : - MudPopoverProvider manquant dans les layouts → circuit Blazor mourait sur l'ouverture de menus/popovers (dashboard jamais rendu après login) - AddIdentity redéfinissait le schéma par défaut sur Identity.Application alors que /_auth/signin signait avec Cookies → cookie posé mais jamais validé, tout rechargement de page redirigeait vers /login. Forçage du schéma par défaut sur Cookies + schémas JwtBearer explicites sur les endpoints API d'AuthController et SettingsController - Policy 'AdminUsers' utilisée par register-admin mais jamais enregistrée → 500 à l'appel. Ajoutée (RequireRole Admin) - HeimdallAuthStateProvider ne lisait que le sessionStorage navigateur : pendant le prerender SSR, HttpContext.User (cookie) n'était pas utilisé → pages protégées redirigées vers /login. Fallback cookie ajouté - SameOriginHttpMessageHandler : résolution de l'origine via IServerAddressesFeature quand IHttpContextAccessor est null (circuit Blazor post-hydratation) au lieu de heimdall.local → DNS cassé - UpdateUserValidator rejetait le thème 'Auto' (défaut de l'UI) → 400 à l'édition. Accepte désormais auto (case-insensitive) Tests : - E2E Playwright complets (login/logout/register/user management) : 33/33 - Integration (Testcontainers PostgreSQL) : 120/120 (valeur 'bool' du valueType, bootstrap admin via /register auto-admin, header X-API-Key, /proc/meminfo optionnel pour les LXC restreints) - Exécution E2E séquentielle (xunit.runner.json) pour éliminer le flakiness
61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Heimdall.IntegrationTests.Factories;
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
public static class TestHelpers
|
|
{
|
|
private static readonly JsonSerializerOptions Json = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
/// <summary>
|
|
/// Register a new admin user and return an authenticated HttpClient.
|
|
/// Uses the test-only register-admin endpoint.
|
|
/// </summary>
|
|
/// <summary>
|
|
/// Bootstrap an admin user and return an authenticated HttpClient.
|
|
/// First user registered becomes Admin automatically.
|
|
/// Also enables the auth.public-registration.enabled flag so viewer users can be created.
|
|
/// </summary>
|
|
public static async Task<HttpClient> CreateAdminClientAsync(this HeimdallWebApplicationFactory factory)
|
|
{
|
|
var email = $"admin-{Guid.NewGuid()}@heimdall.test";
|
|
var client = factory.CreateClient();
|
|
|
|
// Le premier utilisateur enregistré devient automatiquement Admin
|
|
var response = await client.PostAsJsonAsync("api/auth/register", new
|
|
{
|
|
email,
|
|
password = "TestAdminPwd123!",
|
|
displayName = "Test Admin"
|
|
});
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>(Json);
|
|
var token = content.GetProperty("accessToken").GetString()!;
|
|
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
// Activer les feature flags pour les autres tests
|
|
await EnableSetting(client, "auth.public-registration.enabled", "true");
|
|
await EnableSetting(client, "auth.admin-registration.enabled", "true");
|
|
|
|
return client;
|
|
}
|
|
|
|
private static async Task EnableSetting(HttpClient client, string key, string value)
|
|
{
|
|
var response = await client.PostAsJsonAsync("api/settings", new { key, value, valueType = "bool", category = "Auth", description = key });
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
// La setting existe peut-être déjà — essayer PUT
|
|
response = await client.PutAsJsonAsync($"api/settings/{key}", new { value, valueType = "bool" });
|
|
}
|
|
}
|
|
}
|