aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Jellyfin.Server.Implementations.Tests')
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs69
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Jellyfin.Server.Implementations.Tests.csproj16
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunHostTests.cs13
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Plugins/PluginManagerTests.cs45
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ForceKeepAlive.json1
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/Partial.json1
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ValidPartial.json1
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/discover.json (renamed from tests/Jellyfin.Server.Implementations.Tests/LiveTv/discover.json)0
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/lineup.json (renamed from tests/Jellyfin.Server.Implementations.Tests/LiveTv/lineup.json)0
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs28
10 files changed, 156 insertions, 18 deletions
diff --git a/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs
new file mode 100644
index 000000000..1ce2096ea
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/HttpServer/WebSocketConnectionTests.cs
@@ -0,0 +1,69 @@
+using System;
+using System.Buffers;
+using System.IO;
+using System.Text.Json;
+using Emby.Server.Implementations.HttpServer;
+using Microsoft.Extensions.Logging.Abstractions;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.HttpServer
+{
+ public class WebSocketConnectionTests
+ {
+ [Fact]
+ public void DeserializeWebSocketMessage_SingleSegment_Success()
+ {
+ var con = new WebSocketConnection(new NullLogger<WebSocketConnection>(), null!, null!, null!);
+ var bytes = File.ReadAllBytes("Test Data/HttpServer/ForceKeepAlive.json");
+ con.DeserializeWebSocketMessage(new ReadOnlySequence<byte>(bytes), out var bytesConsumed);
+ Assert.Equal(109, bytesConsumed);
+ }
+
+ [Fact]
+ public void DeserializeWebSocketMessage_MultipleSegments_Success()
+ {
+ const int SplitPos = 64;
+ var con = new WebSocketConnection(new NullLogger<WebSocketConnection>(), null!, null!, null!);
+ var bytes = File.ReadAllBytes("Test Data/HttpServer/ForceKeepAlive.json");
+ var seg1 = new BufferSegment(new Memory<byte>(bytes, 0, SplitPos));
+ var seg2 = seg1.Append(new Memory<byte>(bytes, SplitPos, bytes.Length - SplitPos));
+ con.DeserializeWebSocketMessage(new ReadOnlySequence<byte>(seg1, 0, seg2, seg2.Memory.Length - 1), out var bytesConsumed);
+ Assert.Equal(109, bytesConsumed);
+ }
+
+ [Fact]
+ public void DeserializeWebSocketMessage_ValidPartial_Success()
+ {
+ var con = new WebSocketConnection(new NullLogger<WebSocketConnection>(), null!, null!, null!);
+ var bytes = File.ReadAllBytes("Test Data/HttpServer/ValidPartial.json");
+ con.DeserializeWebSocketMessage(new ReadOnlySequence<byte>(bytes), out var bytesConsumed);
+ Assert.Equal(109, bytesConsumed);
+ }
+
+ [Fact]
+ public void DeserializeWebSocketMessage_Partial_ThrowJsonException()
+ {
+ var con = new WebSocketConnection(new NullLogger<WebSocketConnection>(), null!, null!, null!);
+ var bytes = File.ReadAllBytes("Test Data/HttpServer/Partial.json");
+ Assert.Throws<JsonException>(() => con.DeserializeWebSocketMessage(new ReadOnlySequence<byte>(bytes), out var bytesConsumed));
+ }
+
+ internal class BufferSegment : ReadOnlySequenceSegment<byte>
+ {
+ public BufferSegment(Memory<byte> memory)
+ {
+ Memory = memory;
+ }
+
+ public BufferSegment Append(Memory<byte> memory)
+ {
+ var segment = new BufferSegment(memory)
+ {
+ RunningIndex = RunningIndex + Memory.Length
+ };
+ Next = segment;
+ return segment;
+ }
+ }
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Jellyfin.Server.Implementations.Tests.csproj b/tests/Jellyfin.Server.Implementations.Tests/Jellyfin.Server.Implementations.Tests.csproj
index 80259a55f..14b8cbd54 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Jellyfin.Server.Implementations.Tests.csproj
+++ b/tests/Jellyfin.Server.Implementations.Tests/Jellyfin.Server.Implementations.Tests.csproj
@@ -14,13 +14,19 @@
</PropertyGroup>
<ItemGroup>
+ <None Include="Test Data\**\*.*">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+
+ <ItemGroup>
<PackageReference Include="AutoFixture" Version="4.15.0" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.15.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
- <PackageReference Include="Moq" Version="4.15.2" />
+ <PackageReference Include="Moq" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
- <PackageReference Include="coverlet.collector" Version="1.3.0" />
+ <PackageReference Include="coverlet.collector" Version="3.0.3" />
</ItemGroup>
<!-- Code Analyzers -->
@@ -33,11 +39,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Emby.Server.Implementations\Emby.Server.Implementations.csproj" />
- </ItemGroup>
-
- <ItemGroup>
- <EmbeddedResource Include="LiveTv\discover.json" />
- <EmbeddedResource Include="LiveTv\lineup.json" />
+ <ProjectReference Include="..\..\Jellyfin.Server.Implementations\Jellyfin.Server.Implementations.csproj" />
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
diff --git a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunHostTests.cs b/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunHostTests.cs
index fb7cf6a47..8847239d9 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunHostTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunHostTests.cs
@@ -1,5 +1,5 @@
using System;
-using System.Net;
+using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@@ -22,24 +22,15 @@ namespace Jellyfin.Server.Implementations.Tests.LiveTv
public HdHomerunHostTests()
{
- const string BaseResourcePath = "Jellyfin.Server.Implementations.Tests.LiveTv.";
-
var messageHandler = new Mock<HttpMessageHandler>();
messageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns<HttpRequestMessage, CancellationToken>(
(m, _) =>
{
- var resource = BaseResourcePath + m.RequestUri?.Segments[^1];
- var stream = typeof(HdHomerunHostTests).Assembly.GetManifestResourceStream(resource);
- if (stream == null)
- {
- throw new NullReferenceException("Resource doesn't exist: " + resource);
- }
-
return Task.FromResult(new HttpResponseMessage()
{
- Content = new StreamContent(stream)
+ Content = new StreamContent(File.OpenRead("Test Data/LiveTv/" + m.RequestUri?.Segments[^1]))
});
});
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Plugins/PluginManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Plugins/PluginManagerTests.cs
new file mode 100644
index 000000000..bc6a44741
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Plugins/PluginManagerTests.cs
@@ -0,0 +1,45 @@
+using System;
+using System.IO;
+using Emby.Server.Implementations.Plugins;
+using MediaBrowser.Common.Plugins;
+using Microsoft.Extensions.Logging.Abstractions;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Plugins
+{
+ public class PluginManagerTests
+ {
+ private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
+
+ [Fact]
+ public void SaveManifest_RoundTrip_Success()
+ {
+ var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, null!, new Version(1, 0));
+ var manifest = new PluginManifest()
+ {
+ Version = "1.0"
+ };
+
+ var tempPath = Path.Combine(_testPathRoot, "manifest-" + Path.GetRandomFileName());
+ Directory.CreateDirectory(tempPath);
+
+ Assert.True(pluginManager.SaveManifest(manifest, tempPath));
+
+ var res = pluginManager.LoadManifest(tempPath);
+
+ Assert.Equal(manifest.Category, res.Manifest.Category);
+ Assert.Equal(manifest.Changelog, res.Manifest.Changelog);
+ Assert.Equal(manifest.Description, res.Manifest.Description);
+ Assert.Equal(manifest.Id, res.Manifest.Id);
+ Assert.Equal(manifest.Name, res.Manifest.Name);
+ Assert.Equal(manifest.Overview, res.Manifest.Overview);
+ Assert.Equal(manifest.Owner, res.Manifest.Owner);
+ Assert.Equal(manifest.TargetAbi, res.Manifest.TargetAbi);
+ Assert.Equal(manifest.Timestamp, res.Manifest.Timestamp);
+ Assert.Equal(manifest.Version, res.Manifest.Version);
+ Assert.Equal(manifest.Status, res.Manifest.Status);
+ Assert.Equal(manifest.AutoUpdate, res.Manifest.AutoUpdate);
+ Assert.Equal(manifest.ImagePath, res.Manifest.ImagePath);
+ }
+ }
+}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ForceKeepAlive.json b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ForceKeepAlive.json
new file mode 100644
index 000000000..0472a3cd0
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ForceKeepAlive.json
@@ -0,0 +1 @@
+{"MessageType":"ForceKeepAlive","MessageId":"00000000-0000-0000-0000-000000000000","ServerId":null,"Data":60}
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/Partial.json b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/Partial.json
new file mode 100644
index 000000000..72f810725
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/Partial.json
@@ -0,0 +1 @@
+{"MessageType":"KeepAlive","MessageId":"d29ef449-6965-4000
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ValidPartial.json b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ValidPartial.json
new file mode 100644
index 000000000..62d9099c8
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Test Data/HttpServer/ValidPartial.json
@@ -0,0 +1 @@
+{"MessageType":"ForceKeepAlive","MessageId":"00000000-0000-0000-0000-000000000000","ServerId":null,"Data":60}{"MessageType":"KeepAlive","MessageId":"d29ef449-6965-4000
diff --git a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/discover.json b/tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/discover.json
index 851f17bb2..851f17bb2 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/discover.json
+++ b/tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/discover.json
diff --git a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/lineup.json b/tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/lineup.json
index 4cb5ebc8e..4cb5ebc8e 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/lineup.json
+++ b/tests/Jellyfin.Server.Implementations.Tests/Test Data/LiveTv/lineup.json
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs
new file mode 100644
index 000000000..867dda29d
--- /dev/null
+++ b/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs
@@ -0,0 +1,28 @@
+using System;
+using Jellyfin.Server.Implementations.Users;
+using Xunit;
+
+namespace Jellyfin.Server.Implementations.Tests.Users
+{
+ public class UserManagerTests
+ {
+ [Theory]
+ [InlineData("this_is_valid")]
+ [InlineData("this is also valid")]
+ [InlineData("0@_-' .")]
+ public void ThrowIfInvalidUsername_WhenValidUsername_DoesNotThrowArgumentException(string username)
+ {
+ var ex = Record.Exception(() => UserManager.ThrowIfInvalidUsername(username));
+ Assert.Null(ex);
+ }
+
+ [Theory]
+ [InlineData(" ")]
+ [InlineData("")]
+ [InlineData("special characters like & $ ? are not allowed")]
+ public void ThrowIfInvalidUsername_WhenInvalidUsername_ThrowsArgumentException(string username)
+ {
+ Assert.Throws<ArgumentException>(() => UserManager.ThrowIfInvalidUsername(username));
+ }
+ }
+}