jellyfin-server/tests/Jellyfin.Api.Tests/Auth/FirstTimeSetupOrElevatedPolicy/FirstTimeSetupOrElevatedHandlerTests.cs

72 lines
2.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.AutoMoq;
using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Configuration;
2020-06-16 20:12:40 +00:00
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
2020-06-16 20:12:40 +00:00
using Microsoft.AspNetCore.Http;
using Moq;
using Xunit;
namespace Jellyfin.Api.Tests.Auth.FirstTimeSetupOrElevatedPolicy
{
public class FirstTimeSetupOrElevatedHandlerTests
{
private readonly Mock<IConfigurationManager> _configurationManagerMock;
private readonly List<IAuthorizationRequirement> _requirements;
private readonly FirstTimeSetupOrElevatedHandler _sut;
2020-06-16 20:12:40 +00:00
private readonly Mock<IUserManager> _userManagerMock;
private readonly Mock<IHttpContextAccessor> _httpContextAccessor;
public FirstTimeSetupOrElevatedHandlerTests()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
_configurationManagerMock = fixture.Freeze<Mock<IConfigurationManager>>();
2020-03-24 15:12:06 +00:00
_requirements = new List<IAuthorizationRequirement> { new FirstTimeSetupOrElevatedRequirement() };
2020-06-16 20:12:40 +00:00
_userManagerMock = fixture.Freeze<Mock<IUserManager>>();
_httpContextAccessor = fixture.Freeze<Mock<IHttpContextAccessor>>();
_sut = fixture.Create<FirstTimeSetupOrElevatedHandler>();
}
[Theory]
[InlineData(UserRoles.Administrator)]
[InlineData(UserRoles.Guest)]
[InlineData(UserRoles.User)]
public async Task ShouldSucceedIfStartupWizardIncomplete(string userRole)
{
2020-06-16 22:42:10 +00:00
TestHelpers.SetupConfigurationManager(_configurationManagerMock, false);
2020-06-16 22:55:02 +00:00
var claims = TestHelpers.SetupUser(
2020-06-16 22:42:10 +00:00
_userManagerMock,
_httpContextAccessor,
2020-06-16 22:55:02 +00:00
userRole);
2020-06-16 20:12:40 +00:00
var context = new AuthorizationHandlerContext(_requirements, claims, null);
await _sut.HandleAsync(context);
Assert.True(context.HasSucceeded);
}
[Theory]
[InlineData(UserRoles.Administrator, true)]
[InlineData(UserRoles.Guest, false)]
[InlineData(UserRoles.User, false)]
public async Task ShouldRequireAdministratorIfStartupWizardComplete(string userRole, bool shouldSucceed)
{
2020-06-16 22:42:10 +00:00
TestHelpers.SetupConfigurationManager(_configurationManagerMock, true);
2020-06-16 22:55:02 +00:00
var claims = TestHelpers.SetupUser(
2020-06-16 22:42:10 +00:00
_userManagerMock,
_httpContextAccessor,
2020-06-16 22:55:02 +00:00
userRole);
2020-06-16 20:12:40 +00:00
var context = new AuthorizationHandlerContext(_requirements, claims, null);
await _sut.HandleAsync(context);
Assert.Equal(shouldSucceed, context.HasSucceeded);
}
}
}