2026-08-10 13:10:37 +02:00
|
|
|
using Bunit;
|
|
|
|
|
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 AuditLogsPageTests : PageTestBase
|
|
|
|
|
{
|
|
|
|
|
private static AuditLogItem Log(
|
|
|
|
|
string action = "UserCreated",
|
|
|
|
|
string resourceType = "User",
|
|
|
|
|
string? userEmail = "admin@heimdall.local",
|
|
|
|
|
string? ip = "192.168.1.10") => new()
|
2026-08-12 19:49:30 +02:00
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
UserEmail = userEmail,
|
|
|
|
|
Action = action,
|
|
|
|
|
ResourceType = resourceType,
|
|
|
|
|
ResourceId = Guid.NewGuid().ToString(),
|
|
|
|
|
IpAddress = ip,
|
|
|
|
|
Timestamp = DateTime.UtcNow.AddHours(-1)
|
|
|
|
|
};
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AuditLogsPage_AfficheLogs()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>(
|
|
|
|
|
[Log("UserCreated", "User", "admin@heimdall.local", "192.168.1.10"),
|
|
|
|
|
Log("Site.Created", "Site", "admin@heimdall.local", "192.168.1.10")], 2, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("admin@heimdall.local");
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.Markup.ShouldContain("Création utilisateur");
|
|
|
|
|
cut.Markup.ShouldContain("Site créé");
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.Markup.ShouldContain("192.168.1.10");
|
|
|
|
|
cut.Markup.ShouldContain("User");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AuditLogsPage_AucunLog_AfficheMessage()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>([], 0, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucune entrée d'audit"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AuditLogsPage_ErreurChargement_AfficheAlerte()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", System.Net.HttpStatusCode.InternalServerError);
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
cut.Markup.ShouldContain("Erreur lors du chargement du journal d'audit."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task AuditLogsPage_Filtrer_EnvoieTypeRessource()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>([Log()], 1, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Création utilisateur"));
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
// Champ resourceType (Immediate=true → Input), puis submit du form de filtre
|
2026-08-30 09:48:47 +02:00
|
|
|
cut.FindAll("input")[1].Input("User");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.Find("form").Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Get
|
|
|
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/audit-logs")
|
|
|
|
|
&& r.RequestUri.Query.Contains("resourceType=User")).ShouldBeTrue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task AuditLogsPage_ReinitialiserFiltres_RelanceSansFiltre()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>([Log()], 1, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Création utilisateur"));
|
2026-08-10 13:10:37 +02:00
|
|
|
|
2026-08-30 09:48:47 +02:00
|
|
|
cut.FindAll("input")[1].Input("User");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.Find("form").Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Any(r => r.RequestUri!.Query.Contains("resourceType=User")).ShouldBeTrue());
|
|
|
|
|
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Réinitialiser")).Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Last(r => r.Method == HttpMethod.Get
|
|
|
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/audit-logs"))
|
|
|
|
|
.RequestUri!.Query.ShouldNotContain("resourceType"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AuditLogsPage_Pagination_Suivant_ChargePage2()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>(
|
|
|
|
|
Enumerable.Range(1, 20).Select(i => Log($"Action{i}")).ToArray(), 25, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AuditLogsPage>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Page 1 sur 2"));
|
|
|
|
|
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Suivant")).Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Get
|
|
|
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/audit-logs")
|
|
|
|
|
&& r.RequestUri.Query.Contains("page=2")).ShouldBeTrue());
|
|
|
|
|
}
|
|
|
|
|
}
|