- Flag AgentScoped sur AppSetting (entité + migration AddAgentScopedSetting + DTO + commandes Create/Update + validateurs + client Web) - Écran Configuration : toggle 'Exposé aux agents' à la création - AgentDetail : n'affiche que les globaux agent-scoped, par catégories en cartes visuelles avec badge Hérité (global) / Surchargé pour cet agent, édition inline → crée l'override, bouton restore → retour à la valeur globale. Tests bUnit (3) : héritage, surcharge, réinitialisation. - Tests validateur : aliases boolean/number/integer/dropdown
126 lines
5.2 KiB
C#
126 lines
5.2 KiB
C#
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
|
|
&& r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides")).ShouldBeTrue());
|
|
}
|
|
}
|