CustomAuthenticationHandlerTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Linq;
  3. using System.Security.Claims;
  4. using System.Threading.Tasks;
  5. using AutoFixture;
  6. using AutoFixture.AutoMoq;
  7. using Jellyfin.Api.Auth;
  8. using Jellyfin.Api.Constants;
  9. using Jellyfin.Data.Entities;
  10. using Jellyfin.Data.Enums;
  11. using MediaBrowser.Controller.Net;
  12. using Microsoft.AspNetCore.Authentication;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Logging.Abstractions;
  16. using Microsoft.Extensions.Options;
  17. using Moq;
  18. using Xunit;
  19. namespace Jellyfin.Api.Tests.Auth
  20. {
  21. public class CustomAuthenticationHandlerTests
  22. {
  23. private readonly IFixture _fixture;
  24. private readonly Mock<IAuthService> _jellyfinAuthServiceMock;
  25. private readonly CustomAuthenticationHandler _sut;
  26. private readonly AuthenticationScheme _scheme;
  27. public CustomAuthenticationHandlerTests()
  28. {
  29. var fixtureCustomizations = new AutoMoqCustomization
  30. {
  31. ConfigureMembers = true
  32. };
  33. _fixture = new Fixture().Customize(fixtureCustomizations);
  34. AllowFixtureCircularDependencies();
  35. _jellyfinAuthServiceMock = _fixture.Freeze<Mock<IAuthService>>();
  36. var optionsMonitorMock = _fixture.Freeze<Mock<IOptionsMonitor<AuthenticationSchemeOptions>>>();
  37. var serviceProviderMock = _fixture.Freeze<Mock<IServiceProvider>>();
  38. var authenticationServiceMock = _fixture.Freeze<Mock<IAuthenticationService>>();
  39. _fixture.Register<ILoggerFactory>(() => new NullLoggerFactory());
  40. serviceProviderMock.Setup(s => s.GetService(typeof(IAuthenticationService)))
  41. .Returns(authenticationServiceMock.Object);
  42. optionsMonitorMock.Setup(o => o.Get(It.IsAny<string>()))
  43. .Returns(new AuthenticationSchemeOptions
  44. {
  45. ForwardAuthenticate = null
  46. });
  47. HttpContext context = new DefaultHttpContext
  48. {
  49. RequestServices = serviceProviderMock.Object
  50. };
  51. _scheme = new AuthenticationScheme(
  52. _fixture.Create<string>(),
  53. null,
  54. typeof(CustomAuthenticationHandler));
  55. _sut = _fixture.Create<CustomAuthenticationHandler>();
  56. _sut.InitializeAsync(_scheme, context).Wait();
  57. }
  58. [Fact]
  59. public async Task HandleAuthenticateAsyncShouldFailOnSecurityException()
  60. {
  61. var errorMessage = _fixture.Create<string>();
  62. _jellyfinAuthServiceMock.Setup(
  63. a => a.Authenticate(
  64. It.IsAny<HttpRequest>()))
  65. .Throws(new SecurityException(errorMessage));
  66. var authenticateResult = await _sut.AuthenticateAsync();
  67. Assert.False(authenticateResult.Succeeded);
  68. Assert.Equal(errorMessage, authenticateResult.Failure.Message);
  69. }
  70. [Fact]
  71. public async Task HandleAuthenticateAsyncShouldSucceedWithUser()
  72. {
  73. SetupUser();
  74. var authenticateResult = await _sut.AuthenticateAsync();
  75. Assert.True(authenticateResult.Succeeded);
  76. Assert.Null(authenticateResult.Failure);
  77. }
  78. [Fact]
  79. public async Task HandleAuthenticateAsyncShouldAssignNameClaim()
  80. {
  81. var authorizationInfo = SetupUser();
  82. var authenticateResult = await _sut.AuthenticateAsync();
  83. Assert.True(authenticateResult.Principal.HasClaim(ClaimTypes.Name, authorizationInfo.User.Username));
  84. }
  85. [Theory]
  86. [InlineData(true)]
  87. [InlineData(false)]
  88. public async Task HandleAuthenticateAsyncShouldAssignRoleClaim(bool isAdmin)
  89. {
  90. var authorizationInfo = SetupUser(isAdmin);
  91. var authenticateResult = await _sut.AuthenticateAsync();
  92. var expectedRole = authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator) ? UserRoles.Administrator : UserRoles.User;
  93. Assert.True(authenticateResult.Principal.HasClaim(ClaimTypes.Role, expectedRole));
  94. }
  95. [Fact]
  96. public async Task HandleAuthenticateAsyncShouldAssignTicketCorrectScheme()
  97. {
  98. SetupUser();
  99. var authenticatedResult = await _sut.AuthenticateAsync();
  100. Assert.Equal(_scheme.Name, authenticatedResult.Ticket.AuthenticationScheme);
  101. }
  102. private AuthorizationInfo SetupUser(bool isAdmin = false)
  103. {
  104. var authorizationInfo = _fixture.Create<AuthorizationInfo>();
  105. authorizationInfo.User = _fixture.Create<User>();
  106. authorizationInfo.User.SetPermission(PermissionKind.IsAdministrator, isAdmin);
  107. _jellyfinAuthServiceMock.Setup(
  108. a => a.Authenticate(
  109. It.IsAny<HttpRequest>()))
  110. .Returns(authorizationInfo);
  111. return authorizationInfo;
  112. }
  113. private void AllowFixtureCircularDependencies()
  114. {
  115. // A circular dependency exists in the User entity around parent folders,
  116. // this allows Autofixture to generate a User regardless, rather than throw
  117. // an error.
  118. _fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
  119. .ForEach(b => _fixture.Behaviors.Remove(b));
  120. _fixture.Behaviors.Add(new OmitOnRecursionBehavior());
  121. }
  122. }
  123. }