Après .Input()/.Change() (MudTextField Immediate), le dialog re-rend de manière asynchrone : un FindAll() puis .Click()/.Submit() hors InvokeAsync peut référencer un event handler périmé → bUnit UnknownEventHandlerIdException en CI (flaky, non reproductible en local). Workaround officiel bUnit : wrapper le Find+action dans cut.InvokeAsync(...). Appliqué à tous les patterns Input/Change → Click/Submit des tests de pages (Sites, Cameras, Groups, Users, Settings, Snapshots, Register, Profile, AuditLogs, Login, SiteDetail, Discoveries).
184 lines
6.6 KiB
C#
184 lines
6.6 KiB
C#
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("Erreur lors de la création de la session"));
|
|
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"));
|
|
}
|
|
}
|