2026-08-09 23:43:11 +02:00
|
|
|
|
using System.Net.Http.Json;
|
2026-10-27 22:19:52 +01:00
|
|
|
|
using Heimdall.Domain.Entities;
|
2026-08-09 23:43:11 +02:00
|
|
|
|
using Heimdall.TestCommon;
|
2026-10-27 22:19:52 +01:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-08-09 23:43:11 +02:00
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
|
|
using Shouldly;
|
2026-11-04 13:33:30 +01:00
|
|
|
|
using Heimdall.Infrastructure.Identity;
|
2026-08-09 23:43:11 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.E2E.Tests.Pages;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Full authentication flow tests driven through the real Blazor UI:
|
|
|
|
|
|
/// login success/failure, validation, logout, protected-route redirects
|
2026-10-27 22:19:52 +01:00
|
|
|
|
/// and the one-time administrator setup gate.
|
2026-08-09 23:43:11 +02:00
|
|
|
|
/// </summary>
|
2026-11-04 13:33:30 +01:00
|
|
|
|
[Collection("E2E")]
|
2026-08-09 23:43:11 +02:00
|
|
|
|
public class AuthFlowTests : E2ETestBase
|
|
|
|
|
|
{
|
|
|
|
|
|
public AuthFlowTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Login_page_renders()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Page.GotoAsync($"{BaseUrl}/login");
|
|
|
|
|
|
await Page.WaitForSelectorAsync("input[autocomplete='email']", new() { Timeout = 15000 });
|
|
|
|
|
|
|
|
|
|
|
|
(await Page.Locator("h1").TextContentAsync())!.ShouldContain("Heimdall");
|
|
|
|
|
|
var regCount = await Page.Locator("a[href='/register']").CountAsync();
|
2026-10-27 22:19:52 +01:00
|
|
|
|
regCount.ShouldBe(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task First_run_requires_setup_and_creates_the_initial_admin()
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var scope = App.Services.CreateScope())
|
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
|
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityAppUser>>();
|
2026-10-27 22:19:52 +01:00
|
|
|
|
foreach (var user in users.Users.ToList())
|
|
|
|
|
|
{
|
|
|
|
|
|
var deleted = await users.DeleteAsync(user);
|
|
|
|
|
|
deleted.Succeeded.ShouldBeTrue();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await Page.GotoAsync($"{BaseUrl}/login");
|
|
|
|
|
|
await Page.WaitForURLAsync("**/setup**", new() { Timeout = 15000 });
|
|
|
|
|
|
await GotoWithCircuitAsync("/setup");
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='name']", "Initial Admin");
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='email']", $"bootstrap-{Guid.NewGuid():N}@heimdall.test");
|
|
|
|
|
|
var passwordFields = Page.Locator("input[autocomplete='new-password']");
|
|
|
|
|
|
await passwordFields.Nth(0).FillAsync("FirstAdmin!Pass123456");
|
|
|
|
|
|
await passwordFields.Nth(1).FillAsync("FirstAdmin!Pass123456");
|
|
|
|
|
|
await Page.ClickAsync("button:has-text('Créer l’administrateur')");
|
|
|
|
|
|
await Page.WaitForURLAsync("**/dashboard**", new() { Timeout = 20000 });
|
|
|
|
|
|
|
|
|
|
|
|
using var http = new HttpClient { BaseAddress = new Uri(BaseUrl) };
|
|
|
|
|
|
var status = await http.GetFromJsonAsync<BootstrapStatus>("/api/auth/bootstrap-status");
|
|
|
|
|
|
status!.SetupRequired.ShouldBeFalse();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
using var scope = App.Services.CreateScope();
|
2026-11-04 13:33:30 +01:00
|
|
|
|
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityAppUser>>();
|
2026-10-27 22:19:52 +01:00
|
|
|
|
foreach (var user in users.Users.ToList())
|
|
|
|
|
|
await users.DeleteAsync(user);
|
|
|
|
|
|
await Program.SeedAsync(App, App.Configuration);
|
|
|
|
|
|
}
|
2026-08-09 23:43:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Login_with_valid_admin_credentials_redirects_to_dashboard()
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoginViaUiAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// Blazor renders the dashboard content asynchronously after navigation.
|
|
|
|
|
|
await Page.WaitForSelectorAsync("text=Bienvenue", new() { Timeout = 15000 });
|
|
|
|
|
|
(await Page.Locator("body").TextContentAsync())!.ShouldContain("Bienvenue");
|
|
|
|
|
|
// Nav bar should show the main items for an admin
|
|
|
|
|
|
var navCount = await Page.Locator("a[href='/users']").CountAsync();
|
|
|
|
|
|
navCount.ShouldBeGreaterThanOrEqualTo(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Login_with_wrong_password_shows_error()
|
|
|
|
|
|
{
|
|
|
|
|
|
await GotoWithCircuitAsync("/login");
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='email']", AdminEmail);
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='current-password']", "WrongPassword123!");
|
|
|
|
|
|
await Page.ClickAsync("button:has-text('Se connecter')");
|
|
|
|
|
|
|
|
|
|
|
|
await Page.WaitForSelectorAsync(".auth-error", new() { Timeout = 10000 });
|
|
|
|
|
|
(await Page.Locator(".auth-error").TextContentAsync())!.ShouldContain("Email ou mot de passe invalide");
|
|
|
|
|
|
Page.Url.ShouldContain("/login");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Login_with_unknown_email_shows_error()
|
|
|
|
|
|
{
|
|
|
|
|
|
await GotoWithCircuitAsync("/login");
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='email']", "nobody@heimdall.test");
|
|
|
|
|
|
await Page.FillAsync("input[autocomplete='current-password']", AdminPassword);
|
|
|
|
|
|
await Page.ClickAsync("button:has-text('Se connecter')");
|
|
|
|
|
|
|
|
|
|
|
|
await Page.WaitForSelectorAsync(".auth-error", new() { Timeout = 10000 });
|
|
|
|
|
|
(await Page.Locator(".auth-error").TextContentAsync())!.ShouldContain("Email ou mot de passe invalide");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Protected_page_redirects_to_login_when_unauthenticated()
|
|
|
|
|
|
{
|
|
|
|
|
|
await Page.GotoAsync($"{BaseUrl}/users");
|
|
|
|
|
|
await Page.WaitForURLAsync("**/login**", new() { Timeout = 15000 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Logout_returns_to_login()
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoginViaUiAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// Logout is a MudMenuItem in the user menu
|
|
|
|
|
|
await Page.ClickAsync("text=Déconnexion").ContinueWith(_ => { });
|
|
|
|
|
|
// Fallback: navigate via the /_auth/signout endpoint if the button isn't present
|
|
|
|
|
|
if (Page.Url.Contains("/dashboard"))
|
|
|
|
|
|
{
|
|
|
|
|
|
await Page.EvaluateAsync("fetch('/_auth/signout', { method: 'POST' })");
|
|
|
|
|
|
await Page.GotoAsync($"{BaseUrl}/dashboard");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Page.WaitForURLAsync("**/login**", new() { Timeout = 15000 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-27 22:19:52 +01:00
|
|
|
|
public async Task Register_route_redirects_to_login_after_initial_setup()
|
2026-08-09 23:43:11 +02:00
|
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
|
await Page.GotoAsync($"{BaseUrl}/register");
|
|
|
|
|
|
await Page.WaitForURLAsync("**/login**", new() { Timeout = 15000 });
|
2026-08-09 23:43:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-27 22:19:52 +01:00
|
|
|
|
public async Task AnonymousRegistrationApi_route_is_not_available()
|
2026-08-09 23:43:11 +02:00
|
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
|
using var http = new HttpClient { BaseAddress = new Uri(BaseUrl) };
|
|
|
|
|
|
var response = await http.PostAsJsonAsync("/api/auth/register", new
|
2026-08-09 23:43:11 +02:00
|
|
|
|
{
|
2026-10-27 22:19:52 +01:00
|
|
|
|
email = $"e2e-register-{Guid.NewGuid():N}@heimdall.test",
|
|
|
|
|
|
password = "RegisterPass123!",
|
|
|
|
|
|
displayName = "E2E Register"
|
|
|
|
|
|
});
|
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
|
2026-08-09 23:43:11 +02:00
|
|
|
|
}
|
2026-10-27 22:19:52 +01:00
|
|
|
|
|
|
|
|
|
|
private sealed record BootstrapStatus(bool SetupRequired);
|
2026-08-09 23:43:11 +02:00
|
|
|
|
}
|