2021-06-28 10:03:46 +00:00
|
|
|
using System;
|
2021-08-14 01:08:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2021-06-28 10:03:46 +00:00
|
|
|
using AutoFixture;
|
|
|
|
using AutoFixture.AutoMoq;
|
|
|
|
using Emby.Server.Implementations.QuickConnect;
|
2021-10-05 14:59:11 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2021-06-28 10:03:46 +00:00
|
|
|
using MediaBrowser.Controller.Authentication;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2021-08-14 01:08:38 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2021-06-28 10:03:46 +00:00
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
using Moq;
|
|
|
|
using Xunit;
|
|
|
|
|
2021-08-14 01:08:38 +00:00
|
|
|
namespace Jellyfin.Server.Implementations.Tests.QuickConnect
|
2021-06-28 10:03:46 +00:00
|
|
|
{
|
|
|
|
public class QuickConnectManagerTests
|
|
|
|
{
|
2021-08-14 01:08:38 +00:00
|
|
|
private static readonly AuthorizationInfo _quickConnectAuthInfo = new AuthorizationInfo
|
|
|
|
{
|
|
|
|
Device = "Device",
|
|
|
|
DeviceId = "DeviceId",
|
|
|
|
Client = "Client",
|
|
|
|
Version = "1.0.0"
|
|
|
|
};
|
|
|
|
|
2021-06-28 10:03:46 +00:00
|
|
|
private readonly Fixture _fixture;
|
|
|
|
private readonly ServerConfiguration _config;
|
|
|
|
private readonly QuickConnectManager _quickConnectManager;
|
|
|
|
|
|
|
|
public QuickConnectManagerTests()
|
|
|
|
{
|
|
|
|
_config = new ServerConfiguration();
|
|
|
|
var configManager = new Mock<IServerConfigurationManager>();
|
|
|
|
configManager.Setup(x => x.Configuration).Returns(_config);
|
|
|
|
|
|
|
|
_fixture = new Fixture();
|
|
|
|
_fixture.Customize(new AutoMoqCustomization
|
|
|
|
{
|
|
|
|
ConfigureMembers = true
|
|
|
|
}).Inject(configManager.Object);
|
2021-08-14 01:08:38 +00:00
|
|
|
|
|
|
|
// User object contains circular references.
|
|
|
|
_fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
|
|
|
|
.ForEach(b => _fixture.Behaviors.Remove(b));
|
|
|
|
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
|
|
|
|
|
2021-06-28 10:03:46 +00:00
|
|
|
_quickConnectManager = _fixture.Create<QuickConnectManager>();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void IsEnabled_QuickConnectUnavailable_False()
|
|
|
|
=> Assert.False(_quickConnectManager.IsEnabled);
|
|
|
|
|
2021-10-05 14:59:11 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData("", "DeviceId", "Client", "1.0.0")]
|
|
|
|
[InlineData("Device", "", "Client", "1.0.0")]
|
|
|
|
[InlineData("Device", "DeviceId", "", "1.0.0")]
|
|
|
|
[InlineData("Device", "DeviceId", "Client", "")]
|
|
|
|
public void TryConnect_InvalidAuthorizationInfo_ThrowsArgumentException(string device, string deviceId, string client, string version)
|
|
|
|
=> Assert.Throws<ArgumentException>(() => _quickConnectManager.TryConnect(
|
|
|
|
new AuthorizationInfo
|
|
|
|
{
|
|
|
|
Device = device,
|
|
|
|
DeviceId = deviceId,
|
|
|
|
Client = client,
|
|
|
|
Version = version
|
|
|
|
}));
|
|
|
|
|
2021-06-28 10:03:46 +00:00
|
|
|
[Fact]
|
|
|
|
public void TryConnect_QuickConnectUnavailable_ThrowsAuthenticationException()
|
2021-08-14 01:08:38 +00:00
|
|
|
=> Assert.Throws<AuthenticationException>(() => _quickConnectManager.TryConnect(_quickConnectAuthInfo));
|
2021-06-28 10:03:46 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CheckRequestStatus_QuickConnectUnavailable_ThrowsAuthenticationException()
|
|
|
|
=> Assert.Throws<AuthenticationException>(() => _quickConnectManager.CheckRequestStatus(string.Empty));
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void AuthorizeRequest_QuickConnectUnavailable_ThrowsAuthenticationException()
|
2021-08-14 01:08:38 +00:00
|
|
|
=> Assert.ThrowsAsync<AuthenticationException>(() => _quickConnectManager.AuthorizeRequest(Guid.Empty, string.Empty));
|
2021-06-28 10:03:46 +00:00
|
|
|
|
2021-10-05 14:59:11 +00:00
|
|
|
[Fact]
|
|
|
|
public void GetAuthorizedRequest_QuickConnectUnavailable_ThrowsAuthenticationException()
|
|
|
|
=> Assert.Throws<AuthenticationException>(() => _quickConnectManager.GetAuthorizedRequest(string.Empty));
|
|
|
|
|
2021-06-28 10:03:46 +00:00
|
|
|
[Fact]
|
|
|
|
public void IsEnabled_QuickConnectAvailable_True()
|
|
|
|
{
|
|
|
|
_config.QuickConnectAvailable = true;
|
|
|
|
Assert.True(_quickConnectManager.IsEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CheckRequestStatus_QuickConnectAvailable_Success()
|
|
|
|
{
|
|
|
|
_config.QuickConnectAvailable = true;
|
2021-08-14 01:08:38 +00:00
|
|
|
var res1 = _quickConnectManager.TryConnect(_quickConnectAuthInfo);
|
2021-06-28 10:03:46 +00:00
|
|
|
var res2 = _quickConnectManager.CheckRequestStatus(res1.Secret);
|
|
|
|
Assert.Equal(res1, res2);
|
|
|
|
}
|
|
|
|
|
2021-10-05 14:59:11 +00:00
|
|
|
[Fact]
|
|
|
|
public void CheckRequestStatus_UnknownSecret_ThrowsResourceNotFoundException()
|
|
|
|
{
|
|
|
|
_config.QuickConnectAvailable = true;
|
|
|
|
Assert.Throws<ResourceNotFoundException>(() => _quickConnectManager.CheckRequestStatus("Unknown secret"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetAuthorizedRequest_UnknownSecret_ThrowsResourceNotFoundException()
|
|
|
|
{
|
|
|
|
_config.QuickConnectAvailable = true;
|
|
|
|
Assert.Throws<ResourceNotFoundException>(() => _quickConnectManager.GetAuthorizedRequest("Unknown secret"));
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:03:46 +00:00
|
|
|
[Fact]
|
2021-08-14 01:08:38 +00:00
|
|
|
public async Task AuthorizeRequest_QuickConnectAvailable_Success()
|
2021-06-28 10:03:46 +00:00
|
|
|
{
|
|
|
|
_config.QuickConnectAvailable = true;
|
2021-08-14 01:08:38 +00:00
|
|
|
var res = _quickConnectManager.TryConnect(_quickConnectAuthInfo);
|
|
|
|
Assert.True(await _quickConnectManager.AuthorizeRequest(Guid.Empty, res.Code));
|
2021-06-28 10:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|