2020-06-16 15:42:10 -07:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-06-16 15:55:02 -07:00
|
|
|
|
public static ClaimsPrincipal SetupUser(
|
2020-06-16 15:42:10 -07:00
|
|
|
|
Mock<IUserManager> userManagerMock,
|
|
|
|
|
Mock<IHttpContextAccessor> httpContextAccessorMock,
|
|
|
|
|
string role,
|
|
|
|
|
IEnumerable<AccessSchedule>? accessSchedules = null)
|
|
|
|
|
{
|
|
|
|
|
var user = new User(
|
|
|
|
|
"jellyfin",
|
2021-03-06 15:43:01 -07:00
|
|
|
|
typeof(DefaultAuthenticationProvider).FullName!,
|
|
|
|
|
typeof(DefaultPasswordResetProvider).FullName!);
|
2020-06-16 15:42:10 -07:00
|
|
|
|
|
2021-03-17 18:24:14 -07:00
|
|
|
|
user.AddDefaultPermissions();
|
|
|
|
|
user.AddDefaultPreferences();
|
|
|
|
|
|
2020-06-16 15:42:10 -07:00
|
|
|
|
// Set administrator flag.
|
|
|
|
|
user.SetPermission(PermissionKind.IsAdministrator, role.Equals(UserRoles.Administrator, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
// Add access schedules if set.
|
2022-12-05 07:01:13 -07:00
|
|
|
|
if (accessSchedules is not null)
|
2020-06-16 15:42:10 -07:00
|
|
|
|
{
|
|
|
|
|
foreach (var accessSchedule in accessSchedules)
|
|
|
|
|
{
|
|
|
|
|
user.AccessSchedules.Add(accessSchedule);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
|
|
|
|
new Claim(ClaimTypes.Role, role),
|
|
|
|
|
new Claim(ClaimTypes.Name, "jellyfin"),
|
2020-10-14 17:21:15 -07:00
|
|
|
|
new Claim(InternalClaimTypes.UserId, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)),
|
2020-06-16 15:42:10 -07:00
|
|
|
|
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
|
2020-11-13 09:21:22 -07:00
|
|
|
|
.Setup(h => h.HttpContext!.Connection.RemoteIpAddress)
|
2020-06-16 15:55:02 -07:00
|
|
|
|
.Returns(new IPAddress(0));
|
2020-06-16 15:42:10 -07:00
|
|
|
|
|
2020-06-16 15:55:02 -07:00
|
|
|
|
return new ClaimsPrincipal(identity);
|
2020-06-16 15:42:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetupConfigurationManager(in Mock<IConfigurationManager> configurationManagerMock, bool startupWizardCompleted)
|
|
|
|
|
{
|
|
|
|
|
var commonConfiguration = new BaseApplicationConfiguration
|
|
|
|
|
{
|
|
|
|
|
IsStartupWizardCompleted = startupWizardCompleted
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configurationManagerMock
|
|
|
|
|
.Setup(c => c.CommonConfiguration)
|
|
|
|
|
.Returns(commonConfiguration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|