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
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("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"));
|
||
}
|
||
}
|