using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoFixture; using AutoFixture.AutoMoq; using Jellyfin.Api.Auth.IgnoreSchedulePolicy; 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 _configurationManagerMock; private readonly List _requirements; private readonly IgnoreScheduleHandler _sut; private readonly Mock _userManagerMock; private readonly Mock _httpContextAccessor; private readonly AccessSchedule[] _accessSchedules = { new AccessSchedule(DynamicDayOfWeek.Everyday, 0, 0, Guid.Empty) }; public IgnoreScheduleHandlerTests() { var fixture = new Fixture().Customize(new AutoMoqCustomization()); _configurationManagerMock = fixture.Freeze>(); _requirements = new List { new IgnoreScheduleRequirement() }; _userManagerMock = fixture.Freeze>(); _httpContextAccessor = fixture.Freeze>(); _sut = fixture.Create(); } [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); var (_, claims) = TestHelpers.SetupUser( _userManagerMock, _httpContextAccessor, role, TestHelpers.InternalIp, _accessSchedules); var context = new AuthorizationHandlerContext(_requirements, claims, null); await _sut.HandleAsync(context); Assert.Equal(shouldSucceed, context.HasSucceeded); } } }