Files
Heimdall/tests/Heimdall.E2E.Tests/Pages/UserManagementTests.cs
T
arnaud.dartois 2bf3dbc95b
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m8s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Failing after 6m10s
Heimdall Gitea / Build and scan server image (pull_request) Failing after 8m2s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s
fix(identity): harden user lifecycle and permissions
2026-11-04 10:26:07 +01:00

177 lines
7.7 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Heimdall.TestCommon;
using Microsoft.Playwright;
using Shouldly;
namespace Heimdall.E2E.Tests.Pages;
/// <summary>
/// User-management tests driven through the real Blazor UI (/users):
/// list, create, edit roles, lock/unlock, reset password — as the seeded admin.
/// </summary>
[Collection("E2E")]
public class UserManagementTests : E2ETestBase
{
public UserManagementTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
[Fact]
public async Task Users_page_lists_admin_when_authenticated()
{
await LoginViaUiAsync();
await GotoWithCircuitAsync("/users");
// Wait for the specific element that indicates the page is fully loaded
await Page.WaitForSelectorAsync("text=Nouvel utilisateur", new() { Timeout = 60000 });
await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
await Task.Delay(3000);
var body = (await Page.Locator("body").TextContentAsync())!;
body.ShouldContain(AdminEmail);
body.ShouldContain("Admin Test");
}
[Fact]
public async Task Admin_can_create_a_viewer_user_via_the_dialog()
{
await LoginViaUiAsync();
await GotoWithCircuitAsync("/users");
await Page.WaitForSelectorAsync("text=Nouvel utilisateur", new() { Timeout = 20000 });
await Page.ClickAsync("text=Nouvel utilisateur");
var dialog = Page.Locator(".mud-dialog");
await dialog.WaitForAsync(new() { Timeout = 10000 });
await dialog.Locator("input[autocomplete='email']").WaitForAsync(new() { Timeout = 10000 });
var email = $"e2e-user-{Guid.NewGuid():N}@heimdall.test";
await dialog.Locator("input[autocomplete='email']").FillAsync(email);
await dialog.Locator("input[autocomplete='name']").FillAsync("E2E Viewer");
await dialog.Locator("input[autocomplete='new-password']").FillAsync("ViewerPass123!");
// Role defaults to Viewer in the select — create directly
await dialog.Locator("button:has-text('Créer')").ClickAsync();
// Dialog closes, table reloads and contains the new user
await Page.WaitForSelectorAsync($"text={email}", new() { Timeout = 15000 });
var body = (await Page.Locator("body").TextContentAsync())!;
body.ShouldContain("E2E Viewer");
}
[Fact]
public async Task New_user_can_log_in_with_viewer_role()
{
// Create a viewer through the API (admin token), then sign in via the UI
var email = $"e2e-viewer-{Guid.NewGuid():N}@heimdall.test";
var password = "ViewerPass123!";
var token = await LoginViaApiAsync();
using var http = new HttpClient { BaseAddress = new Uri(BaseUrl) };
http.DefaultRequestHeaders.Authorization = new("Bearer", token);
var createResp = await http.PostAsJsonAsync("/api/users", new
{
email,
password,
displayName = "E2E Viewer Login",
roles = new[] { "Viewer" }
});
createResp.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Created);
await GotoWithCircuitAsync("/login");
await Page.FillAsync("input[autocomplete='email']", email);
await Page.FillAsync("input[autocomplete='current-password']", password);
await Page.ClickAsync("button:has-text('Se connecter')");
await Page.WaitForURLAsync("**/dashboard**", new() { Timeout = 20000 });
await Page.WaitForSelectorAsync("text=Bienvenue", new() { Timeout = 15000 });
(await Page.Locator("body").TextContentAsync())!.ShouldContain("Bienvenue");
// A viewer must NOT see the admin Users page in the nav
var usersNav = await Page.Locator("a[href='/users']").CountAsync();
usersNav.ShouldBe(0);
}
[Fact]
public async Task Admin_can_edit_user_display_name_and_roles()
{
// Create a throwaway user via the API
var email = $"e2e-edit-{Guid.NewGuid():N}@heimdall.test";
var token = await LoginViaApiAsync();
using var http = new HttpClient { BaseAddress = new Uri(BaseUrl) };
http.DefaultRequestHeaders.Authorization = new("Bearer", token);
var createResp = await http.PostAsJsonAsync("/api/users", new
{
email,
password = "EditPass123!",
displayName = "Avant Modification",
roles = new[] { "Viewer" }
});
createResp.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Created);
await LoginViaUiAsync();
await GotoWithCircuitAsync("/users");
await Page.WaitForSelectorAsync($"text={email}", new() { Timeout = 20000 });
// Open the edit dialog via the pencil icon in the user row
var row = Page.Locator($"tr:has-text('{email}')");
await row.GetByRole(AriaRole.Button, new() { Name = "Modifier l'utilisateur", Exact = true }).ClickAsync();
// Rename + grant Supervisor (scope to the edit dialog)
var dialog = Page.Locator(".mud-dialog");
await dialog.WaitForAsync(new() { Timeout = 10000 });
var nameInput = dialog.GetByLabel("Nom d'affichage", new() { Exact = true });
await nameInput.WaitForAsync(new() { Timeout = 10000 });
await nameInput.FillAsync("Après Modification");
await dialog.Locator("label:has-text('Supervisor')").ClickAsync();
await dialog.Locator("button:has-text('Enregistrer')").ClickAsync();
await Page.WaitForSelectorAsync("text=Après Modification", new() { Timeout = 15000 });
}
[Fact]
public async Task Admin_can_reset_user_password()
{
var email = $"e2e-reset-{Guid.NewGuid():N}@heimdall.test";
var token = await LoginViaApiAsync();
using var http = new HttpClient { BaseAddress = new Uri(BaseUrl) };
http.DefaultRequestHeaders.Authorization = new("Bearer", token);
var createResp = await http.PostAsJsonAsync("/api/users", new
{
email,
password = "ResetPass123!",
displayName = "E2E Reset",
roles = new[] { "Viewer" }
});
createResp.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Created);
await LoginViaUiAsync();
await GotoWithCircuitAsync("/users");
await Page.WaitForSelectorAsync($"text={email}", new() { Timeout = 20000 });
var row = Page.Locator($"tr:has-text('{email}')");
await row.GetByRole(AriaRole.Button, new() { Name = "Modifier l'utilisateur", Exact = true }).ClickAsync();
var dialog = Page.Locator(".mud-dialog");
await dialog.WaitForAsync(new() { Timeout = 10000 });
await dialog.Locator("button:has-text('Réinitialiser le mot de passe')").ClickAsync();
// The dialog reveals a temporary password
await dialog.Locator("text=temporaire").WaitForAsync(new() { Timeout = 10000 });
}
[Fact]
public async Task Admin_can_edit_role_permissions_in_the_permissions_ui()
{
await LoginViaUiAsync();
await GotoWithCircuitAsync("/admin/claims");
await Page.WaitForSelectorAsync("text=Matrice des permissions", new() { Timeout = 20000 });
var row = Page.Locator("tr").Filter(new() { HasText = "Écriture des caméras" });
await row.WaitForAsync(new() { Timeout = 10000 });
var roleChecks = row.Locator("input[type='checkbox']");
await roleChecks.Nth(1).UncheckAsync(); // Supervisor; Admin stays immutable.
var saveButtons = Page.GetByRole(AriaRole.Button, new() { Name = "Enregistrer", Exact = true });
await saveButtons.Nth(0).ClickAsync();
await Page.WaitForSelectorAsync("text=Droits du rôle enregistrés", new() { Timeout = 10000 });
(await roleChecks.Nth(1).IsCheckedAsync()).ShouldBeFalse();
(await roleChecks.Nth(0).IsCheckedAsync()).ShouldBeTrue();
}
}