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 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(() =>
|
|
|
|
|
{
|
2026-10-25 05:17:23 +01:00
|
|
|
cut.Markup.ShouldContain("Profil");
|
2026-08-10 13:10:37 +02:00
|
|
|
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");
|
2026-10-25 05:17:23 +01:00
|
|
|
cut.Markup.ShouldContain("Activée"); // 2FA
|
2026-08-10 13:10:37 +02:00
|
|
|
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]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task Profile_EnregistrerProfil_AfficheSucces()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
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");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.FindAll("form")[0].Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
|
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
|
|
|
.ShouldContain(m => m.Contains("Profil mis à jour", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task Profile_EnregistrerProfil_NomVide_AfficheErreur()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
SetupHappyPath();
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<Profile>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Informations personnelles"));
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.FindAll("form")[0].Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
cut.Markup.ShouldContain("Le nom d'affichage est requis"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task Profile_ChangerMotDePasse_AfficheSucces()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
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");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
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]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task Profile_ChangerMotDePasse_TropCourt_AfficheErreur()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
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");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
cut.Markup.ShouldContain("Le nouveau mot de passe doit contenir au moins 12 caractères"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task Profile_ChangerMotDePasse_NonCorrespondance_AfficheErreur()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
|
|
|
|
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");
|
2026-08-11 16:56:19 +02:00
|
|
|
await cut.InvokeAsync(() => cut.FindAll("form")[1].Submit());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
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() ?? "")
|
2026-10-25 05:17:23 +01:00
|
|
|
.ShouldContain(m => m.Contains("Configuration des notifications mise à jour", StringComparison.OrdinalIgnoreCase));
|
2026-08-10 13:10:37 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|