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
};
///
/// Register a new admin user and return an authenticated HttpClient.
/// Uses the test-only register-admin endpoint.
///
public static async Task CreateAdminClientAsync(this HeimdallWebApplicationFactory factory)
{
var email = $"admin-{Guid.NewGuid()}@heimdall.test";
var client = factory.CreateClient();
var response = await client.PostAsJsonAsync("api/auth/register-admin", new
{
email,
password = "AdminPwd123!",
displayName = "Test Admin"
});
var content = await response.Content.ReadFromJsonAsync(Json);
var token = content.GetProperty("accessToken").GetString()!;
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
return client;
}
}