53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System.Security.Claims;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using SlpModularCms.Core.Identity.Authorization;
|
|
using Xunit;
|
|
|
|
namespace SlpModularCms.Core.Tests.Identity;
|
|
|
|
public class HierarchicalRoleHandlerTests
|
|
{
|
|
private readonly HierarchicalRoleHandler _handler;
|
|
|
|
public HierarchicalRoleHandlerTests()
|
|
{
|
|
_handler = new HierarchicalRoleHandler();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Owner", "Owner", true)]
|
|
[InlineData("Owner", "Administrator", true)]
|
|
[InlineData("Owner", "User", true)]
|
|
[InlineData("Administrator", "Administrator", true)]
|
|
[InlineData("Administrator", "User", true)]
|
|
[InlineData("Administrator", "Owner", false)]
|
|
[InlineData("User", "User", true)]
|
|
[InlineData("User", "Administrator", false)]
|
|
[InlineData("User", "Owner", false)]
|
|
public async Task HandleAsync_ShouldValidateHierarchyCorrectly(string userRole, string requiredRole, bool shouldSucceed)
|
|
{
|
|
// Arrange
|
|
var requirement = new HierarchicalRoleRequirement(requiredRole);
|
|
var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.Role, userRole)
|
|
}));
|
|
|
|
var context = new AuthorizationHandlerContext(new[] { requirement }, user, null);
|
|
|
|
// Act
|
|
await _handler.HandleAsync(context);
|
|
|
|
// Assert
|
|
if (shouldSucceed)
|
|
{
|
|
context.HasSucceeded.Should().BeTrue();
|
|
}
|
|
else
|
|
{
|
|
context.HasSucceeded.Should().BeFalse();
|
|
}
|
|
}
|
|
}
|