2026-08-10 20:51:57 +02:00
|
|
|
using Bunit;
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Heimdall.Web.Components.Pages;
|
|
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MudBlazor;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
|
|
|
|
|
|
public class AgentDetailTests : PageTestBase
|
|
|
|
|
{
|
|
|
|
|
private static readonly Guid AgentId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
private static AgentItem Agent() => new()
|
|
|
|
|
{
|
|
|
|
|
Id = AgentId,
|
|
|
|
|
SiteId = Guid.NewGuid(),
|
|
|
|
|
SiteName = "Site Principal",
|
|
|
|
|
Name = "agent-01",
|
|
|
|
|
Hostname = "agent-01",
|
|
|
|
|
Version = "1.0.0",
|
|
|
|
|
Status = "Online",
|
|
|
|
|
LastHeartbeat = DateTime.UtcNow,
|
|
|
|
|
PublicIp = "10.0.0.5",
|
|
|
|
|
CreatedAt = DateTime.UtcNow.AddDays(-5)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static AppSettingItem Setting(string key, string value, string category, string valueType, bool agentScoped = true, string? description = null) => new(
|
|
|
|
|
Guid.NewGuid(), key, value, description, category, valueType, null, DateTime.UtcNow, agentScoped);
|
|
|
|
|
|
|
|
|
|
private static AgentSettingItem Override(string key, string value) => new(
|
|
|
|
|
Guid.NewGuid(), AgentId, key, value, DateTime.UtcNow);
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AgentDetail_AfficheGlobauxHerites_AvecBadge()
|
|
|
|
|
{
|
|
|
|
|
// Ordre important : les routes spécifiques /settings AVANT api/agents/{id}
|
|
|
|
|
// (RoutedHandler matche par Contains)
|
|
|
|
|
Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List<AgentSettingItem>());
|
|
|
|
|
Handler.OnGet("api/settings", new List<AppSettingItem>
|
|
|
|
|
{
|
|
|
|
|
Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection"),
|
|
|
|
|
Setting("recording.enabled", "true", "Recording", "boolean", true, "Enregistrement activé"),
|
|
|
|
|
Setting("server.internal.port", "8080", "Server", "number", false, "Port interne (non agent)")
|
|
|
|
|
});
|
|
|
|
|
Handler.OnGet($"api/agents/{AgentId}", Agent());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AgentDetail>(p => p.Add(x => x.Id, AgentId));
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("agent-01"));
|
|
|
|
|
|
|
|
|
|
// Les globaux agent-scoped sont affichés avec leurs libellés
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Seuil de détection");
|
|
|
|
|
cut.Markup.ShouldContain("Enregistrement activé");
|
|
|
|
|
cut.Markup.ShouldContain("Hérité (global)");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Le setting non agent-scoped n'apparaît pas
|
|
|
|
|
cut.Markup.ShouldNotContain("server.internal.port");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AgentDetail_ModifierGlobal_CreeSurcharge()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List<AgentSettingItem>());
|
|
|
|
|
Handler.On(HttpMethod.Put, $"api/settings/agent/{AgentId}/overrides", System.Net.HttpStatusCode.OK);
|
|
|
|
|
Handler.OnGet("api/settings", new List<AppSettingItem>
|
|
|
|
|
{
|
|
|
|
|
Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection")
|
|
|
|
|
});
|
|
|
|
|
Handler.OnGet($"api/agents/{AgentId}", Agent());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AgentDetail>(p => p.Add(x => x.Id, AgentId));
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Seuil de détection"));
|
|
|
|
|
|
|
|
|
|
// Modifier la valeur dans la carte → crée un override
|
|
|
|
|
cut.FindAll(".settings-card .mud-input input")[0].Change("0.9");
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Put
|
|
|
|
|
&& r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides")).ShouldBeTrue());
|
|
|
|
|
|
|
|
|
|
var put = Handler.Requests.First(r => r.Method == HttpMethod.Put
|
|
|
|
|
&& r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides"));
|
|
|
|
|
put.RequestUri!.AbsolutePath.ShouldContain(AgentId.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AgentDetail_SurchargeExistante_AfficheBadgeEtReinitialise()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List<AgentSettingItem>
|
|
|
|
|
{
|
|
|
|
|
Override("detection.threshold", "0.9")
|
|
|
|
|
});
|
|
|
|
|
Handler.On(HttpMethod.Delete, $"api/settings/agent/{AgentId}/overrides", System.Net.HttpStatusCode.OK);
|
|
|
|
|
Handler.OnGet("api/settings", new List<AppSettingItem>
|
|
|
|
|
{
|
|
|
|
|
Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection")
|
|
|
|
|
});
|
|
|
|
|
Handler.OnGet($"api/agents/{AgentId}", Agent());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AgentDetail>(p => p.Add(x => x.Id, AgentId));
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Seuil de détection"));
|
|
|
|
|
|
|
|
|
|
// La carte affiche la valeur surchargée et le badge
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Surchargé pour cet agent");
|
|
|
|
|
cut.Markup.ShouldContain("0.9");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Le bouton Restore (réinitialiser) déclenche le DELETE
|
|
|
|
|
cut.FindAll("button.mud-icon-button").First(b =>
|
|
|
|
|
b.QuerySelector("svg") is not null).Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Delete
|
2026-08-30 09:45:37 +02:00
|
|
|
&& r.RequestUri!.AbsolutePath.Contains($"/api/settings/agent/{AgentId}/overrides")).ShouldBeTrue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AgentDetail_BoutonSupprimer_Present()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet($"/api/settings/agent/{AgentId}/overrides", new List<AgentSettingItem>());
|
|
|
|
|
Handler.OnGet("api/settings", new List<AppSettingItem>());
|
|
|
|
|
Handler.OnGet($"/api/agents/{AgentId}", Agent());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AgentDetail>(p => p.Add(x => x.Id, AgentId));
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("agent-01"));
|
|
|
|
|
|
|
|
|
|
// Le bouton Supprimer est présent dans la zone d'actions du bas
|
|
|
|
|
cut.Markup.ShouldContain("Supprimer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AgentDetail_Supprimer_OuvreDialogConfirmation()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet($"/api/settings/agent/{AgentId}/overrides", new List<AgentSettingItem>());
|
|
|
|
|
Handler.OnGet("api/settings", new List<AppSettingItem>());
|
|
|
|
|
Handler.OnGet($"/api/agents/{AgentId}", Agent());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AgentDetail>(p => p.Add(x => x.Id, AgentId));
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("agent-01"));
|
|
|
|
|
|
|
|
|
|
// Cliquer sur "Supprimer" ouvre le dialog de confirmation
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Supprimer")
|
|
|
|
|
&& b.TextContent.Trim() != "Supprimer le réglage").Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Confirmer la suppression");
|
|
|
|
|
cut.Markup.ShouldContain("désassignées");
|
|
|
|
|
});
|
2026-08-10 20:51:57 +02:00
|
|
|
}
|
|
|
|
|
}
|