diff options
| author | Anthony Lavado <anthony@lavado.ca> | 2020-08-08 13:22:36 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-08 13:22:36 -0400 |
| commit | b9fdbaeef326a06ba824cbb78a91f58afc535aab (patch) | |
| tree | 915b3f9e787cde081af88465bf9c3f20228be3f3 /tests/Jellyfin.Api.Tests/TestHelpers.cs | |
| parent | 7e49358ba9c1fcf12f9e7b30601a9df568a65242 (diff) | |
| parent | a15be774ac606ec71f3ab0849a56ae08b8cc2f4d (diff) | |
Merge pull request #3812 from jellyfin/api-migration
Merge API Migration into master
Diffstat (limited to 'tests/Jellyfin.Api.Tests/TestHelpers.cs')
| -rw-r--r-- | tests/Jellyfin.Api.Tests/TestHelpers.cs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/Jellyfin.Api.Tests/TestHelpers.cs b/tests/Jellyfin.Api.Tests/TestHelpers.cs new file mode 100644 index 000000000..a4dd4e409 --- /dev/null +++ b/tests/Jellyfin.Api.Tests/TestHelpers.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Net; +using System.Security.Claims; +using Jellyfin.Api.Constants; +using Jellyfin.Data.Entities; +using Jellyfin.Data.Enums; +using Jellyfin.Server.Implementations.Users; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Configuration; +using Microsoft.AspNetCore.Http; +using Moq; +using AccessSchedule = Jellyfin.Data.Entities.AccessSchedule; + +namespace Jellyfin.Api.Tests +{ + public static class TestHelpers + { + public static ClaimsPrincipal SetupUser( + Mock<IUserManager> userManagerMock, + Mock<IHttpContextAccessor> httpContextAccessorMock, + string role, + IEnumerable<AccessSchedule>? accessSchedules = null) + { + var user = new User( + "jellyfin", + typeof(DefaultAuthenticationProvider).FullName, + typeof(DefaultPasswordResetProvider).FullName); + + // Set administrator flag. + user.SetPermission(PermissionKind.IsAdministrator, role.Equals(UserRoles.Administrator, StringComparison.OrdinalIgnoreCase)); + + // Add access schedules if set. + if (accessSchedules != null) + { + foreach (var accessSchedule in accessSchedules) + { + user.AccessSchedules.Add(accessSchedule); + } + } + + var claims = new[] + { + new Claim(ClaimTypes.Role, role), + new Claim(ClaimTypes.Name, "jellyfin"), + new Claim(InternalClaimTypes.UserId, Guid.Empty.ToString("N", CultureInfo.InvariantCulture)), + new Claim(InternalClaimTypes.DeviceId, Guid.Empty.ToString("N", CultureInfo.InvariantCulture)), + new Claim(InternalClaimTypes.Device, "test"), + new Claim(InternalClaimTypes.Client, "test"), + new Claim(InternalClaimTypes.Version, "test"), + new Claim(InternalClaimTypes.Token, "test"), + }; + + var identity = new ClaimsIdentity(claims); + + userManagerMock + .Setup(u => u.GetUserById(It.IsAny<Guid>())) + .Returns(user); + + httpContextAccessorMock + .Setup(h => h.HttpContext.Connection.RemoteIpAddress) + .Returns(new IPAddress(0)); + + return new ClaimsPrincipal(identity); + } + + public static void SetupConfigurationManager(in Mock<IConfigurationManager> configurationManagerMock, bool startupWizardCompleted) + { + var commonConfiguration = new BaseApplicationConfiguration + { + IsStartupWizardCompleted = startupWizardCompleted + }; + + configurationManagerMock + .Setup(c => c.CommonConfiguration) + .Returns(commonConfiguration); + } + } +} |
