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 LoginTests : PageTestBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private static AuthResponse SuccessResponse() => new(
|
|
|
|
|
|
"access-token", "refresh-token", DateTime.UtcNow.AddHours(1), false,
|
|
|
|
|
|
new UserInfo(Guid.NewGuid(), "admin@heimdall.local", "Admin User", ["Admin"]));
|
|
|
|
|
|
|
|
|
|
|
|
private BunitNavigationManager Navigate()
|
|
|
|
|
|
=> Services.GetRequiredService<BunitNavigationManager>();
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void LoginPage_AfficheFormulaire_EmailMotDePasseEtBouton()
|
|
|
|
|
|
{
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.Markup.ShouldContain("Heimdall");
|
|
|
|
|
|
cut.Markup.ShouldContain("Email");
|
|
|
|
|
|
cut.Markup.ShouldContain("Mot de passe");
|
|
|
|
|
|
cut.Markup.ShouldContain("Se connecter");
|
|
|
|
|
|
cut.FindAll("input").Count.ShouldBe(2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_IdentifiantsInvalides_AfficheErreur()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("wrong@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].Input("mauvais-mot-de-passe");
|
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(() =>
|
|
|
|
|
|
cut.Markup.ShouldContain("Email ou mot de passe invalide"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_Succes_NavigueVersDashboard()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", SuccessResponse());
|
|
|
|
|
|
JSInterop.Setup<bool>("heimdallAuth.signIn", _ => true).SetResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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 LoginPage_Succes_SansCookie_AfficheErreurSession()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", SuccessResponse());
|
|
|
|
|
|
// JS interop non configuré -> SignInAsync retourne false
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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:35:00 +01:00
|
|
|
|
cut.Markup.ShouldContain("La session n’a pas pu être créée. Veuillez réessayer."));
|
2026-08-10 12:46:15 +02:00
|
|
|
|
nav.Uri.ShouldBe("http://localhost/");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_2faRequis_AfficheCode2FA()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
var response = SuccessResponse() with { Requires2fa = true };
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", response);
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
cut.Markup.ShouldContain("Code 2FA");
|
|
|
|
|
|
cut.Markup.ShouldContain("Vérifier");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_2faCodeVide_AfficheErreur()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
var response = SuccessResponse() with { Requires2fa = true };
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", response);
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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
|
|
|
|
|
|
|
|
|
|
// Le formulaire 2FA apparaît puis on soumet sans code
|
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Code 2FA"));
|
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(() => cut.Markup.ShouldContain("Le code 2FA est requis"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_2faSucces_NavigueVersDashboard()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
var requires2fa = SuccessResponse() with { Requires2fa = true };
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", requires2fa);
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/2fa/verify", SuccessResponse());
|
|
|
|
|
|
JSInterop.Setup<bool>("heimdallAuth.signIn", _ => true).SetResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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(() => cut.Markup.ShouldContain("Code 2FA"));
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("123456");
|
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]
|
|
|
|
|
|
public void LoginPage_DejaConnecte_RedirigeVersDashboard()
|
|
|
|
|
|
{
|
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_ReturnUrl_UtilisePourLaNavigation()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", SuccessResponse());
|
|
|
|
|
|
JSInterop.Setup<bool>("heimdallAuth.signIn", _ => true).SetResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
nav.NavigateTo("/login?returnUrl=/sites");
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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/sites"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
|
public async Task LoginPage_ReturnUrlExterne_IgnoreEtVaSurDashboard()
|
2026-08-10 12:46:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
Handler.On(HttpMethod.Post, "api/auth/login", SuccessResponse());
|
|
|
|
|
|
JSInterop.Setup<bool>("heimdallAuth.signIn", _ => true).SetResult(true);
|
|
|
|
|
|
|
|
|
|
|
|
var nav = Navigate();
|
|
|
|
|
|
nav.NavigateTo("/login?returnUrl=https://evil.example.com");
|
|
|
|
|
|
var cut = RenderPage<Login>();
|
|
|
|
|
|
|
|
|
|
|
|
cut.FindAll("input")[0].Input("admin@heimdall.local");
|
|
|
|
|
|
cut.FindAll("input")[1].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"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|