39 lines
1.2 KiB
C#
39 lines
1.2 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>
|
||
|
|
public static async Task<HttpClient> 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<JsonElement>(Json);
|
||
|
|
var token = content.GetProperty("accessToken").GetString()!;
|
||
|
|
|
||
|
|
client.DefaultRequestHeaders.Authorization =
|
||
|
|
new AuthenticationHeaderValue("Bearer", token);
|
||
|
|
|
||
|
|
return client;
|
||
|
|
}
|
||
|
|
}
|