jellyfin/tests/Jellyfin.Api.Tests/Auth/IgnoreSchedulePolicy/IgnoreScheduleHandlerTests.cs

63 lines
2.3 KiB
C#
Raw Normal View History

2020-06-16 15:42:10 -07:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.AutoMoq;
2020-08-06 07:17:45 -07:00
using Jellyfin.Api.Auth.IgnoreParentalControlPolicy;
2020-06-16 15:42:10 -07:00
using Jellyfin.Api.Constants;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Moq;
using Xunit;
namespace Jellyfin.Api.Tests.Auth.IgnoreSchedulePolicy
{
public class IgnoreScheduleHandlerTests
{
private readonly Mock<IConfigurationManager> _configurationManagerMock;
private readonly List<IAuthorizationRequirement> _requirements;
2020-08-06 07:17:45 -07:00
private readonly IgnoreParentalControlHandler _sut;
2020-06-16 15:42:10 -07:00
private readonly Mock<IUserManager> _userManagerMock;
private readonly Mock<IHttpContextAccessor> _httpContextAccessor;
2020-06-16 15:55:02 -07:00
/// <summary>
/// Globally disallow access.
/// </summary>
2020-06-16 15:42:10 -07:00
private readonly AccessSchedule[] _accessSchedules = { new AccessSchedule(DynamicDayOfWeek.Everyday, 0, 0, Guid.Empty) };
public IgnoreScheduleHandlerTests()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
_configurationManagerMock = fixture.Freeze<Mock<IConfigurationManager>>();
2020-08-06 07:17:45 -07:00
_requirements = new List<IAuthorizationRequirement> { new IgnoreParentalControlRequirement() };
2020-06-16 15:42:10 -07:00
_userManagerMock = fixture.Freeze<Mock<IUserManager>>();
_httpContextAccessor = fixture.Freeze<Mock<IHttpContextAccessor>>();
2020-08-06 07:17:45 -07:00
_sut = fixture.Create<IgnoreParentalControlHandler>();
2020-06-16 15:42:10 -07:00
}
[Theory]
[InlineData(UserRoles.Administrator, true)]
[InlineData(UserRoles.User, true)]
[InlineData(UserRoles.Guest, true)]
public async Task ShouldAllowScheduleCorrectly(string role, bool shouldSucceed)
{
TestHelpers.SetupConfigurationManager(_configurationManagerMock, true);
2020-06-16 15:55:02 -07:00
var claims = TestHelpers.SetupUser(
2020-06-16 15:42:10 -07:00
_userManagerMock,
_httpContextAccessor,
role,
_accessSchedules);
var context = new AuthorizationHandlerContext(_requirements, claims, null);
await _sut.HandleAsync(context);
Assert.Equal(shouldSucceed, context.HasSucceeded);
}
}
}