2019-12-22 15:54:46 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
|
|
|
using Jellyfin.Api.Constants;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Tests.Auth.RequiresElevationPolicy
|
|
|
|
{
|
|
|
|
public class RequiresElevationHandlerTests
|
|
|
|
{
|
|
|
|
private readonly RequiresElevationHandler _sut;
|
|
|
|
|
|
|
|
public RequiresElevationHandlerTests()
|
|
|
|
{
|
|
|
|
_sut = new RequiresElevationHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
[InlineData(UserRoles.Administrator, true)]
|
|
|
|
[InlineData(UserRoles.User, false)]
|
|
|
|
[InlineData(UserRoles.Guest, false)]
|
|
|
|
public async Task ShouldHandleRolesCorrectly(string role, bool shouldSucceed)
|
|
|
|
{
|
2020-03-24 08:12:06 -07:00
|
|
|
var requirements = new List<IAuthorizationRequirement> { new RequiresElevationRequirement() };
|
2019-12-22 15:54:46 -07:00
|
|
|
|
2020-03-24 08:12:06 -07:00
|
|
|
var claims = new[] { new Claim(ClaimTypes.Role, role) };
|
2019-12-22 15:54:46 -07:00
|
|
|
var identity = new ClaimsIdentity(claims);
|
|
|
|
var user = new ClaimsPrincipal(identity);
|
|
|
|
|
|
|
|
var context = new AuthorizationHandlerContext(requirements, user, null);
|
|
|
|
|
|
|
|
await _sut.HandleAsync(context);
|
|
|
|
Assert.Equal(shouldSucceed, context.HasSucceeded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|