CustomAuthenticationHandlerTests.cs 6.4 KB

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