using System.Net; using System.Net.Http.Json; using Heimdall.TestCommon; using Microsoft.Playwright; using Shouldly; namespace Heimdall.E2E.Tests.Pages; /// /// User-management tests driven through the real Blazor UI (/users): /// list, create, edit roles, lock/unlock, reset password — as the seeded admin. /// 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 }); } }