Heimdall Gitea / Detect CI scope (pull_request) Successful in 4s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m32s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m36s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 11m34s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s
85 lines
3.0 KiB
C#
85 lines
3.0 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>
|
|
/// Bootstrap an admin user and return an authenticated HttpClient.
|
|
/// </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/bootstrap-admin", 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);
|
|
|
|
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" });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a Viewer as an admin, then return a Viewer-authenticated client.
|
|
/// </summary>
|
|
public static async Task<HttpClient> CreateViewerClientAsync(
|
|
this HeimdallWebApplicationFactory factory, HttpClient? existingAdminClient = null)
|
|
{
|
|
var adminClient = existingAdminClient ?? await CreateAdminClientAsync(factory);
|
|
|
|
var email = $"viewer-{Guid.NewGuid()}@heimdall.test";
|
|
var createResponse = await adminClient.PostAsJsonAsync("api/users", new
|
|
{
|
|
email,
|
|
password = "ViewerPwd123!",
|
|
displayName = "Test Viewer",
|
|
roles = new[] { "Viewer" }
|
|
});
|
|
createResponse.EnsureSuccessStatusCode();
|
|
|
|
var client = factory.CreateClient();
|
|
var response = await client.PostAsJsonAsync("api/auth/login", new
|
|
{
|
|
email,
|
|
password = "ViewerPwd123!"
|
|
});
|
|
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;
|
|
}
|
|
}
|