2026-08-10 12:46:15 +02:00
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
|
|
|
|
|
|
public class AuthStateTests
|
|
|
|
|
{
|
2026-08-11 11:59:18 +02:00
|
|
|
private static AuthResponse AuthResponse(string token = "access") => new(
|
|
|
|
|
token, "refresh", DateTime.UtcNow.AddHours(1), false,
|
|
|
|
|
new UserInfo(Guid.NewGuid(), "admin@heimdall.test", "Admin", ["Admin"]));
|
2026-08-10 12:46:15 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 11:59:18 +02:00
|
|
|
public void Login_EffaceLeCookieCapture()
|
2026-08-10 12:46:15 +02:00
|
|
|
{
|
2026-08-11 11:59:18 +02:00
|
|
|
var state = new AuthState { AuthCookie = "CfDJ8-vieux-cookie" };
|
2026-08-10 12:46:15 +02:00
|
|
|
|
2026-08-11 11:59:18 +02:00
|
|
|
state.Login(AuthResponse());
|
2026-08-10 12:46:15 +02:00
|
|
|
|
2026-08-11 11:59:18 +02:00
|
|
|
// Le JWT prend le relais : l'ancien cookie ne doit plus être propagé
|
|
|
|
|
state.AuthCookie.ShouldBeNull();
|
|
|
|
|
state.AccessToken.ShouldBe("access");
|
2026-08-10 12:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 11:59:18 +02:00
|
|
|
public void Logout_EffaceAussiLeCookieCapture()
|
2026-08-10 12:46:15 +02:00
|
|
|
{
|
2026-08-11 11:59:18 +02:00
|
|
|
var state = new AuthState { AuthCookie = "CfDJ8-cookie" };
|
|
|
|
|
state.Login(AuthResponse());
|
|
|
|
|
state.AuthCookie.ShouldBeNull();
|
2026-08-10 12:46:15 +02:00
|
|
|
|
2026-08-11 11:59:18 +02:00
|
|
|
// Simule une restauration SSR puis une déconnexion
|
|
|
|
|
state.AuthCookie = "CfDJ8-cookie";
|
2026-08-10 12:46:15 +02:00
|
|
|
state.Logout();
|
|
|
|
|
|
2026-08-11 11:59:18 +02:00
|
|
|
state.AuthCookie.ShouldBeNull();
|
2026-08-10 12:46:15 +02:00
|
|
|
state.IsLoggedIn.ShouldBeFalse();
|
|
|
|
|
}
|
2026-11-04 13:33:30 +01:00
|
|
|
|
|
|
|
|
[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();
|
|
|
|
|
}
|
2026-08-10 12:46:15 +02:00
|
|
|
}
|