122 lines
3.5 KiB
C#
122 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();
|
||
|
|
}
|
||
|
|
}
|