2026-07-17 13:15:25 +00:00
|
|
|
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>
|
2026-07-28 15:14:54 +00:00
|
|
|
/// <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>
|
2026-07-17 13:15:25 +00:00
|
|
|
public static async Task<HttpClient> CreateAdminClientAsync(this HeimdallWebApplicationFactory factory)
|
|
|
|
|
{
|
|
|
|
|
var email = $"admin-{Guid.NewGuid()}@heimdall.test";
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
|
2026-07-28 15:14:54 +00:00
|
|
|
// Le premier utilisateur enregistré devient automatiquement Admin
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/auth/register", new
|
2026-07-17 13:15:25 +00:00
|
|
|
{
|
|
|
|
|
email,
|
2026-07-28 15:14:54 +00:00
|
|
|
password = "TestAdminPwd123!",
|
2026-07-17 13:15:25 +00:00
|
|
|
displayName = "Test Admin"
|
|
|
|
|
});
|
2026-07-28 15:14:54 +00:00
|
|
|
response.EnsureSuccessStatusCode();
|
2026-07-17 13:15:25 +00:00
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>(Json);
|
|
|
|
|
var token = content.GetProperty("accessToken").GetString()!;
|
|
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization =
|
|
|
|
|
new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
|
|
2026-07-28 15:14:54 +00:00
|
|
|
// Activer les feature flags pour les autres tests
|
|
|
|
|
await EnableSetting(client, "auth.public-registration.enabled", "true");
|
|
|
|
|
await EnableSetting(client, "auth.admin-registration.enabled", "true");
|
|
|
|
|
|
2026-07-17 13:15:25 +00:00
|
|
|
return client;
|
|
|
|
|
}
|
2026-07-28 15:14:54 +00:00
|
|
|
|
|
|
|
|
private static async Task EnableSetting(HttpClient client, string key, string value)
|
|
|
|
|
{
|
2026-08-09 23:43:11 +02:00
|
|
|
var response = await client.PostAsJsonAsync("api/settings", new { key, value, valueType = "bool", category = "Auth", description = key });
|
2026-07-28 15:14:54 +00:00
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
// La setting existe peut-être déjà — essayer PUT
|
2026-08-09 23:43:11 +02:00
|
|
|
response = await client.PutAsJsonAsync($"api/settings/{key}", new { value, valueType = "bool" });
|
2026-07-28 15:14:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-10 17:50:43 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Bootstrap a viewer-authenticated HttpClient (second registered user,
|
|
|
|
|
/// default role = Viewer). Requires an admin to have been created first
|
|
|
|
|
/// (which enables public registration).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task<HttpClient> CreateViewerClientAsync(this HeimdallWebApplicationFactory factory)
|
|
|
|
|
{
|
|
|
|
|
var email = $"viewer-{Guid.NewGuid()}@heimdall.test";
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/auth/register", new
|
|
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "ViewerPwd123!",
|
|
|
|
|
displayName = "Test Viewer"
|
|
|
|
|
});
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>(Json);
|
|
|
|
|
var token = content.GetProperty("accessToken").GetString()!;
|
|
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Authorization =
|
|
|
|
|
new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
2026-07-17 13:15:25 +00:00
|
|
|
}
|