Files
Heimdall/tests/Heimdall.Web.Tests/Components/Pages/AuditLogsPageTests.cs
T

128 lines
4.5 KiB
C#
Raw Normal View History

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()
{
Id = Guid.NewGuid(),
UserEmail = userEmail,
Action = action,
ResourceType = resourceType,
ResourceId = Guid.NewGuid().ToString(),
IpAddress = ip,
Timestamp = DateTime.UtcNow.AddHours(-1)
};
[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");
cut.Markup.ShouldContain("UserCreated");
cut.Markup.ShouldContain("Site.Created");
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]
public async Task AuditLogsPage_Filtrer_EnvoieTypeRessource()
{
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>([Log()], 1, 1, 20));
LoginAs();
var cut = RenderPage<AuditLogsPage>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("UserCreated"));
// Champ resourceType (Immediate=true → Input), puis submit du form de filtre
cut.FindAll("input")[1].Input("User");
await cut.InvokeAsync(() => cut.Find("form").Submit());
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]
public async Task AuditLogsPage_ReinitialiserFiltres_RelanceSansFiltre()
{
Handler.OnGet("api/audit-logs", new PagedData<AuditLogItem>([Log()], 1, 1, 20));
LoginAs();
var cut = RenderPage<AuditLogsPage>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("UserCreated"));
cut.FindAll("input")[1].Input("User");
await cut.InvokeAsync(() => cut.Find("form").Submit());
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());
}
}