Files
Arnaud Dartois 2e45c37925
Heimdall Gitea / Build and tests (pull_request) Successful in 13m17s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 5m13s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 11m32s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
test(web): align localized page assertions
2026-10-25 05:17:23 +01:00

192 lines
6.7 KiB
C#

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 ProfileTests : PageTestBase
{
private static UserItem ProfileUser() => new()
{
Id = Guid.NewGuid(),
Email = "jean@heimdall.local",
DisplayName = "Jean Dupont",
Theme = "Auto",
Language = "fr",
Roles = ["Admin"],
TwoFactorEnabled = true,
EmailConfirmed = true,
CreatedAt = DateTime.UtcNow.AddDays(-30)
};
private static NotificationConfigItem NotifConfig() => new()
{
EmailEnabled = false,
NtfyEnabled = true,
NtfyUrl = "http://ntfy:80",
DailyDigest = true,
WeeklyDigest = false
};
private void SetupHappyPath()
{
Handler.OnGet("api/users/me", ProfileUser());
Handler.OnGet("api/notifications/config", NotifConfig());
}
[Fact]
public void Profile_AfficheInformationsEtSections()
{
SetupHappyPath();
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Profil");
cut.Markup.ShouldContain("Jean Dupont");
cut.Markup.ShouldContain("jean@heimdall.local");
cut.Markup.ShouldContain("Informations personnelles");
cut.Markup.ShouldContain("Changer le mot de passe");
cut.Markup.ShouldContain("Notifications");
cut.Markup.ShouldContain("Activée"); // 2FA
cut.Markup.ShouldContain("Oui"); // Email confirmé
});
}
[Fact]
public void Profile_ErreurChargement_AfficheAlerte()
{
Handler.OnGet("api/users/me", System.Net.HttpStatusCode.InternalServerError);
Handler.OnGet("api/notifications/config", NotifConfig());
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Erreur lors du chargement du profil."));
}
[Fact]
public async Task Profile_EnregistrerProfil_AfficheSucces()
{
SetupHappyPath();
Handler.On(HttpMethod.Put, "api/users/me", System.Net.HttpStatusCode.OK);
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Informations personnelles"));
// Le premier input du formulaire profil est le nom d'affichage (Immediate=true)
cut.FindAll("input")[0].Input("Jean Nouveau");
await cut.InvokeAsync(() => cut.FindAll("form")[0].Submit());
cut.WaitForAssertion(() =>
{
var snackbar = Services.GetRequiredService<ISnackbar>();
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
.ShouldContain(m => m.Contains("Profil mis à jour", StringComparison.OrdinalIgnoreCase));
});
}
[Fact]
public async Task Profile_EnregistrerProfil_NomVide_AfficheErreur()
{
SetupHappyPath();
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Informations personnelles"));
cut.FindAll("input")[0].Input("");
await cut.InvokeAsync(() => cut.FindAll("form")[0].Submit());
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Le nom d'affichage est requis"));
}
[Fact]
public async Task Profile_ChangerMotDePasse_AfficheSucces()
{
SetupHappyPath();
Handler.On(HttpMethod.Post, "api/auth/change-password", System.Net.HttpStatusCode.OK);
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Changer le mot de passe"));
cut.Find("input[autocomplete='current-password']").Input("ancien-motdepasse");
cut.FindAll("input[autocomplete='new-password']")[0].Input("nouveau-motdepasse-123");
cut.FindAll("input[autocomplete='new-password']")[1].Input("nouveau-motdepasse-123");
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
cut.WaitForAssertion(() =>
{
var snackbar = Services.GetRequiredService<ISnackbar>();
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
.ShouldContain(m => m.Contains("Mot de passe changé", StringComparison.OrdinalIgnoreCase));
});
}
[Fact]
public async Task Profile_ChangerMotDePasse_TropCourt_AfficheErreur()
{
SetupHappyPath();
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Changer le mot de passe"));
cut.Find("input[autocomplete='current-password']").Input("ancien-motdepasse");
cut.FindAll("input[autocomplete='new-password']")[0].Input("court");
cut.FindAll("input[autocomplete='new-password']")[1].Input("court");
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Le nouveau mot de passe doit contenir au moins 12 caractères"));
}
[Fact]
public async Task Profile_ChangerMotDePasse_NonCorrespondance_AfficheErreur()
{
SetupHappyPath();
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Changer le mot de passe"));
cut.Find("input[autocomplete='current-password']").Input("ancien-motdepasse");
cut.FindAll("input[autocomplete='new-password']")[0].Input("nouveau-motdepasse-123");
cut.FindAll("input[autocomplete='new-password']")[1].Input("autre-motdepasse-456");
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Les mots de passe ne correspondent pas"));
}
[Fact]
public void Profile_EnregistrerNotifications_AfficheSucces()
{
SetupHappyPath();
Handler.On(HttpMethod.Put, "api/notifications/config", System.Net.HttpStatusCode.OK);
LoginAs();
var cut = RenderPage<Profile>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Notifications"));
// Deux boutons "Enregistrer" : celui du profil (submit) puis celui des notifications
cut.FindAll("button").Where(b => b.TextContent.Contains("Enregistrer")).ElementAt(1).Click();
cut.WaitForAssertion(() =>
{
var snackbar = Services.GetRequiredService<ISnackbar>();
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
.ShouldContain(m => m.Contains("Configuration des notifications mise à jour", StringComparison.OrdinalIgnoreCase));
});
}
}