2
0

CustomAuthenticationHandlerTests.cs 5.8 KB

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