Files
Heimdall/tests/Heimdall.Web.Tests/Components/Pages/LoginTests.cs
T
Arnaud Dartois e8652b6797
Heimdall Gitea / Build and tests (pull_request) Failing after 9m7s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
test(i18n): align remaining web assertions
2026-10-25 06:35:00 +01:00

184 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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]
public async Task LoginPage_IdentifiantsInvalides_AfficheErreur()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Email ou mot de passe invalide"));
}
[Fact]
public async Task LoginPage_Succes_NavigueVersDashboard()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
await AuthProvider.Received(1).NotifyLoginAsync(Arg.Any<AuthResponse>());
}
[Fact]
public async Task LoginPage_Succes_SansCookie_AfficheErreurSession()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("La session n’a pas pu être créée. Veuillez réessayer."));
nav.Uri.ShouldBe("http://localhost/");
}
[Fact]
public async Task LoginPage_2faRequis_AfficheCode2FA()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Code 2FA");
cut.Markup.ShouldContain("Vérifier");
});
}
[Fact]
public async Task LoginPage_2faCodeVide_AfficheErreur()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
// Le formulaire 2FA apparaît puis on soumet sans code
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Code 2FA"));
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Le code 2FA est requis"));
}
[Fact]
public async Task LoginPage_2faSucces_NavigueVersDashboard()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Code 2FA"));
cut.FindAll("input")[0].Input("123456");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
await AuthProvider.Received(1).NotifyLoginAsync(Arg.Any<AuthResponse>());
}
[Fact]
public void LoginPage_DejaConnecte_RedirigeVersDashboard()
{
LoginAs();
var nav = Navigate();
var cut = RenderPage<Login>();
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
}
[Fact]
public async Task LoginPage_ReturnUrl_UtilisePourLaNavigation()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/sites"));
}
[Fact]
public async Task LoginPage_ReturnUrlExterne_IgnoreEtVaSurDashboard()
{
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");
await cut.InvokeAsync(() => cut.Find("form").Submit());
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/dashboard"));
}
}