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
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-28 15:14:54 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Bootstrap an admin user and return an authenticated HttpClient.
|
|
|
|
|
/// </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-10-27 22:19:52 +01:00
|
|
|
var response = await client.PostAsJsonAsync("api/auth/bootstrap-admin", 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
|
|
|
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>
|
2026-10-27 22:19:52 +01:00
|
|
|
/// Create a Viewer as an admin, then return a Viewer-authenticated client.
|
2026-10-10 17:50:43 +02:00
|
|
|
/// </summary>
|
2026-10-27 22:19:52 +01:00
|
|
|
public static async Task<HttpClient> CreateViewerClientAsync(
|
|
|
|
|
this HeimdallWebApplicationFactory factory, HttpClient? existingAdminClient = null)
|
2026-10-10 17:50:43 +02:00
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
var adminClient = existingAdminClient ?? await CreateAdminClientAsync(factory);
|
2026-10-10 19:47:09 +02:00
|
|
|
|
2026-10-10 17:50:43 +02:00
|
|
|
var email = $"viewer-{Guid.NewGuid()}@heimdall.test";
|
2026-10-27 22:19:52 +01:00
|
|
|
var createResponse = await adminClient.PostAsJsonAsync("api/users", new
|
2026-10-10 17:50:43 +02:00
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "ViewerPwd123!",
|
2026-10-27 22:19:52 +01:00
|
|
|
displayName = "Test Viewer",
|
|
|
|
|
roles = new[] { "Viewer" }
|
|
|
|
|
});
|
|
|
|
|
createResponse.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/auth/login", new
|
|
|
|
|
{
|
|
|
|
|
email,
|
|
|
|
|
password = "ViewerPwd123!"
|
2026-10-10 17:50:43 +02:00
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|