Pages couvertes : Login, Register, Dashboard, SitesPage, SiteDetail, CamerasPage (+ AuthState, HeimdallAuthStateProvider) — 105 tests Web verts. Infrastructure nécessaire pour rendre les pages testables avec bUnit : - MudTestBase : JSInterop en mode Loose + wrapper générique IAsyncDisposable→IDisposable (Castle DynamicProxy) pour les services MudBlazor (PopoverService, PointerEventsNoneService...) qui cassent le Dispose synchrone de bUnit - HostWithProviders.razor : fournit MudPopoverProvider + MudDialogProvider — sans eux les MudDialog inline des pages ne sont jamais rendus - PageTestBase.RenderPage<T> : rend les pages dans ce host - bunit 2.7.2 → 2.9.0, AngleSharp → 1.7.0, SDK Razor pour les .razor locaux Correctifs de tests : ShouldContain avec prédicat (sous-chaîne au lieu d'égalité stricte sur les messages snackbar), .Input()/.Change() selon Immediate des champs, format P0 (87% sans espace), index des boutons d'action (copier vs supprimer).
123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using System.Security.Claims;
|
|
using Heimdall.Web.Services;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
public class AuthStateTests
|
|
{
|
|
private static readonly Guid UserId = Guid.NewGuid();
|
|
|
|
private static AuthResponse CreateResponse(params string[] roles) => new(
|
|
"access-token",
|
|
"refresh-token",
|
|
DateTime.UtcNow.AddMinutes(15),
|
|
false,
|
|
new UserInfo(UserId, "admin@heimdall.test", "Admin", roles));
|
|
|
|
[Fact]
|
|
public void Login_SetsTokensAndUser()
|
|
{
|
|
var state = new AuthState();
|
|
var response = CreateResponse("Admin");
|
|
|
|
state.Login(response);
|
|
|
|
state.AccessToken.ShouldBe("access-token");
|
|
state.RefreshToken.ShouldBe("refresh-token");
|
|
state.User.ShouldNotBeNull();
|
|
state.User!.Email.ShouldBe("admin@heimdall.test");
|
|
state.IsLoggedIn.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Login_ResolvesPermissionsFromRoles()
|
|
{
|
|
var state = new AuthState();
|
|
var response = CreateResponse("Admin");
|
|
|
|
state.Login(response);
|
|
|
|
// Admin role maps to permissions via AppPermissions.RolePermissions
|
|
state.Permissions.Count.ShouldBeGreaterThan(0);
|
|
state.HasPermission("Site.Read").ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Login_WithoutRoles_HasNoPermissions()
|
|
{
|
|
var state = new AuthState();
|
|
var response = CreateResponse();
|
|
|
|
state.Login(response);
|
|
|
|
state.Permissions.ShouldBeEmpty();
|
|
state.IsLoggedIn.ShouldBeTrue(); // user present, but no roles
|
|
}
|
|
|
|
[Fact]
|
|
public void Logout_ClearsEverything()
|
|
{
|
|
var state = new AuthState();
|
|
state.Login(CreateResponse("Admin"));
|
|
|
|
state.Logout();
|
|
|
|
state.AccessToken.ShouldBeNull();
|
|
state.RefreshToken.ShouldBeNull();
|
|
state.User.ShouldBeNull();
|
|
state.IsLoggedIn.ShouldBeFalse();
|
|
state.Permissions.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void LoginFromPrincipal_RestoresIdentityAndPermissions()
|
|
{
|
|
var state = new AuthState();
|
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(
|
|
[
|
|
new Claim(ClaimTypes.NameIdentifier, UserId.ToString()),
|
|
new Claim(ClaimTypes.Name, "Admin User"),
|
|
new Claim(ClaimTypes.Email, "admin@heimdall.test"),
|
|
new Claim(ClaimTypes.Role, "Admin")
|
|
], "Test"));
|
|
|
|
state.LoginFromPrincipal(principal);
|
|
|
|
state.User.ShouldNotBeNull();
|
|
state.User!.Id.ShouldBe(UserId);
|
|
state.User.Email.ShouldBe("admin@heimdall.test");
|
|
state.User!.Roles.ShouldNotBeNull();
|
|
state.User.Roles!.ShouldContain("Admin");
|
|
state.HasPermission("Site.Read").ShouldBeTrue();
|
|
// No JWT in the cookie path
|
|
state.AccessToken.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void LoginFromPrincipal_WithoutNameIdentifier_LogsOut()
|
|
{
|
|
var state = new AuthState();
|
|
state.Login(CreateResponse("Admin"));
|
|
var principal = new ClaimsPrincipal(new ClaimsIdentity([], "Test"));
|
|
|
|
state.LoginFromPrincipal(principal);
|
|
|
|
state.IsLoggedIn.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void LoginFromPrincipal_WithInvalidId_LogsOut()
|
|
{
|
|
var state = new AuthState();
|
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(
|
|
[
|
|
new Claim(ClaimTypes.NameIdentifier, "not-a-guid")
|
|
], "Test"));
|
|
|
|
state.LoginFromPrincipal(principal);
|
|
|
|
state.IsLoggedIn.ShouldBeFalse();
|
|
}
|
|
}
|