2026-08-10 12:46:15 +02:00
|
|
|
|
using Bunit;
|
|
|
|
|
|
using Bunit.TestDoubles;
|
|
|
|
|
|
using Heimdall.Web.Components.Pages;
|
|
|
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
|
|
|
|
|
|
|
|
public class RegisterTests : PageTestBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private static AuthResponse SuccessResponse() => new(
|
|
|
|
|
|
"access-token", "refresh-token", DateTime.UtcNow.AddHours(1), false,
|
|
|
|
|
|
new UserInfo(Guid.NewGuid(), "user@heimdall.local", "Nouvel Utilisateur", ["Viewer"]));
|
|
|
|
|
|
|
|
|
|
|
|
private BunitNavigationManager Navigate()
|
|
|
|
|
|
=> Services.GetRequiredService<BunitNavigationManager>();
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void RegisterPage_AfficheFormulaire()
|
|
|
|
|
|
{
|
|
|
|
|
|
var cut = RenderPage<Register>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.Markup.ShouldContain("Créer un compte");
|
2026-10-25 06:30:33 +01:00
|
|
|
|
cut.Markup.ShouldContain("Nom d’affichage");
|
2026-08-10 12:46:15 +02:00
|
|
|
|
cut.Markup.ShouldContain("Email");
|
|
|
|
|
|
cut.Markup.ShouldContain("Mot de passe");
|
2026-10-25 06:30:33 +01:00
|
|
|
|
cut.Markup.ShouldContain("S’inscrire");
|
2026-08-10 12:46:15 +02:00
|
|
|
|
cut.FindAll("input").Count.ShouldBe(3);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task RegisterPage_EchecInscription_AfficheErreur()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/register", System.Net.HttpStatusCode.BadRequest);
|
|
|
|
|
|
var cut = RenderPage<Register>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("Nouvel Utilisateur");
|
|
|
|
|
|
cut.FindAll("input")[1].Input("user@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[2].Input("motdepasse-securise");
|
2026-08-11 16:56:19 +02:00
|
|
|
|
await cut.InvokeAsync(() => cut.Find("form").Submit());
|
2026-08-10 12:46:15 +02:00
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
2026-10-25 06:30:33 +01:00
|
|
|
|
cut.Markup.ShouldContain("Erreur lors de l’inscription"));
|
2026-08-10 12:46:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task RegisterPage_Succes_NavigueVersDashboard()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/register", SuccessResponse());
|
|
|
|
|
|
JSInterop.Setup<bool>("heimdallAuth.signIn", _ => true).SetResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
var cut = RenderPage<Register>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("Nouvel Utilisateur");
|
|
|
|
|
|
cut.FindAll("input")[1].Input("user@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[2].Input("motdepasse-securise");
|
2026-08-11 16:56:19 +02:00
|
|
|
|
await cut.InvokeAsync(() => cut.Find("form").Submit());
|
2026-08-10 12:46:15 +02:00
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
|
2026-08-11 16:56:19 +02:00
|
|
|
|
await AuthProvider.Received(1).NotifyLoginAsync(Arg.Any<AuthResponse>());
|
2026-08-10 12:46:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task RegisterPage_Exception_AfficheErreur()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/register",
|
|
|
|
|
|
() => throw new InvalidOperationException("boom"));
|
|
|
|
|
|
var cut = RenderPage<Register>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("Nouvel Utilisateur");
|
|
|
|
|
|
cut.FindAll("input")[1].Input("user@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[2].Input("motdepasse-securise");
|
2026-08-11 16:56:19 +02:00
|
|
|
|
await cut.InvokeAsync(() => cut.Find("form").Submit());
|
2026-08-10 12:46:15 +02:00
|
|
|
|
|
2026-08-10 23:33:06 +02:00
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Erreur lors de l'opération"));
|
2026-08-10 12:46:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|