using Heimdall.Web.Services; using Shouldly; namespace Heimdall.Web.Tests.Services; public class AuthStateTests { private static AuthResponse AuthResponse(string token = "access") => new( token, "refresh", DateTime.UtcNow.AddHours(1), false, new UserInfo(Guid.NewGuid(), "admin@heimdall.test", "Admin", ["Admin"])); [Fact] public void Login_EffaceLeCookieCapture() { var state = new AuthState { AuthCookie = "CfDJ8-vieux-cookie" }; state.Login(AuthResponse()); // Le JWT prend le relais : l'ancien cookie ne doit plus être propagé state.AuthCookie.ShouldBeNull(); state.AccessToken.ShouldBe("access"); } [Fact] public void Logout_EffaceAussiLeCookieCapture() { var state = new AuthState { AuthCookie = "CfDJ8-cookie" }; state.Login(AuthResponse()); state.AuthCookie.ShouldBeNull(); // Simule une restauration SSR puis une déconnexion state.AuthCookie = "CfDJ8-cookie"; state.Logout(); state.AuthCookie.ShouldBeNull(); state.IsLoggedIn.ShouldBeFalse(); } [Fact] public void Login_UsesPersistedPermissionsFromTheAuthResponse() { var response = new AuthResponse("access", "refresh", DateTime.UtcNow.AddHours(1), false, new UserInfo(Guid.NewGuid(), "viewer@heimdall.test", "Viewer", ["Viewer"], ["Camera.Write"])); var state = new AuthState(); state.Login(response); state.HasPermission("Camera.Write").ShouldBeTrue(); state.HasPermission("Camera.Live").ShouldBeFalse(); } }