From 7ced6d1c8cab999e9d79281a570d9c068efdd46f Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Thu, 3 Sep 2020 21:51:38 +0200 Subject: Enable code coverage and upload OpenAPI spec. Add basic test to write out the actual openapi spec. --- tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs (limited to 'tests') diff --git a/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs b/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs new file mode 100644 index 0000000000..e054908f3e --- /dev/null +++ b/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs @@ -0,0 +1,42 @@ +using System.IO; +using System.Reflection; +using System.Text.Json; +using System.Threading.Tasks; +using MediaBrowser.Model.Branding; +using Xunit; +using Xunit.Abstractions; + +namespace MediaBrowser.Api.Tests +{ + public sealed class OpenApiSpecTests : IClassFixture + { + private readonly JellyfinApplicationFactory _factory; + private readonly ITestOutputHelper _outputHelper; + + public OpenApiSpecTests(JellyfinApplicationFactory factory, ITestOutputHelper outputHelper) + { + _factory = factory; + _outputHelper = outputHelper; + } + + [Fact] + public async Task GetSpec_ReturnsCorrectResponse() + { + // Arrange + var client = _factory.CreateClient(); + + // Act + var response = await client.GetAsync("/api-docs/openapi.json"); + + // Assert + response.EnsureSuccessStatusCode(); + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); + + // Write out for publishing + var responseBody = await response.Content.ReadAsStringAsync(); + string outputPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ".", "openapi.json")); + _outputHelper.WriteLine("Writing OpenAPI Spec JSON to '{0}'.", outputPath); + File.WriteAllText(outputPath, responseBody); + } + } +} -- cgit v1.2.3 From 1c71ddc42664f60036a206e767366cc575af74a7 Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Thu, 3 Sep 2020 23:13:46 +0200 Subject: Merge MediaBrowser.Api.Tests into Jellyfin.Api.Tests --- .vscode/tasks.json | 2 +- MediaBrowser.sln | 6 - tests/Jellyfin.Api.Tests/BrandingServiceTests.cs | 49 +++++++++ tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj | 1 + .../JellyfinApplicationFactory.cs | 121 +++++++++++++++++++++ tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs | 2 +- .../MediaBrowser.Api.Tests/BrandingServiceTests.cs | 49 --------- .../JellyfinApplicationFactory.cs | 121 --------------------- .../MediaBrowser.Api.Tests.csproj | 34 ------ tests/jellyfin-tests.ruleset | 4 +- 10 files changed, 175 insertions(+), 214 deletions(-) create mode 100644 tests/Jellyfin.Api.Tests/BrandingServiceTests.cs create mode 100644 tests/Jellyfin.Api.Tests/JellyfinApplicationFactory.cs delete mode 100644 tests/MediaBrowser.Api.Tests/BrandingServiceTests.cs delete mode 100644 tests/MediaBrowser.Api.Tests/JellyfinApplicationFactory.cs delete mode 100644 tests/MediaBrowser.Api.Tests/MediaBrowser.Api.Tests.csproj (limited to 'tests') diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7ddc49d5ce..09c523eb28 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -17,7 +17,7 @@ "type": "process", "args": [ "test", - "${workspaceFolder}/tests/MediaBrowser.Api.Tests/MediaBrowser.Api.Tests.csproj" + "${workspaceFolder}/tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj" ], "problemMatcher": "$msCompile" } diff --git a/MediaBrowser.sln b/MediaBrowser.sln index 75587da1f8..fd45dca2af 100644 --- a/MediaBrowser.sln +++ b/MediaBrowser.sln @@ -66,8 +66,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jellyfin.Data", "Jellyfin.D EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jellyfin.Server.Implementations", "Jellyfin.Server.Implementations\Jellyfin.Server.Implementations.csproj", "{DAE48069-6D86-4BA6-B148-D1D49B6DDA52}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediaBrowser.Api.Tests", "tests\MediaBrowser.Api.Tests\MediaBrowser.Api.Tests.csproj", "{7C93C84F-105C-48E5-A878-406FA0A5B296}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -178,10 +176,6 @@ Global {DAE48069-6D86-4BA6-B148-D1D49B6DDA52}.Debug|Any CPU.Build.0 = Debug|Any CPU {DAE48069-6D86-4BA6-B148-D1D49B6DDA52}.Release|Any CPU.ActiveCfg = Release|Any CPU {DAE48069-6D86-4BA6-B148-D1D49B6DDA52}.Release|Any CPU.Build.0 = Release|Any CPU - {7C93C84F-105C-48E5-A878-406FA0A5B296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7C93C84F-105C-48E5-A878-406FA0A5B296}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7C93C84F-105C-48E5-A878-406FA0A5B296}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7C93C84F-105C-48E5-A878-406FA0A5B296}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/tests/Jellyfin.Api.Tests/BrandingServiceTests.cs b/tests/Jellyfin.Api.Tests/BrandingServiceTests.cs new file mode 100644 index 0000000000..6fc287420b --- /dev/null +++ b/tests/Jellyfin.Api.Tests/BrandingServiceTests.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using System.Threading.Tasks; +using MediaBrowser.Model.Branding; +using Xunit; + +namespace Jellyfin.Api.Tests +{ + public sealed class BrandingServiceTests : IClassFixture + { + private readonly JellyfinApplicationFactory _factory; + + public BrandingServiceTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task GetConfiguration_ReturnsCorrectResponse() + { + // Arrange + var client = _factory.CreateClient(); + + // Act + var response = await client.GetAsync("/Branding/Configuration"); + + // Assert + response.EnsureSuccessStatusCode(); + Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); + var responseBody = await response.Content.ReadAsStreamAsync(); + _ = await JsonSerializer.DeserializeAsync(responseBody); + } + + [Theory] + [InlineData("/Branding/Css")] + [InlineData("/Branding/Css.css")] + public async Task GetCss_ReturnsCorrectResponse(string url) + { + // Arrange + var client = _factory.CreateClient(); + + // Act + var response = await client.GetAsync(url); + + // Assert + response.EnsureSuccessStatusCode(); + Assert.Equal("text/css; charset=utf-8", response.Content.Headers.ContentType.ToString()); + } + } +} diff --git a/tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj b/tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj index 368b6bf0bd..bcba3a2032 100644 --- a/tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj +++ b/tests/Jellyfin.Api.Tests/Jellyfin.Api.Tests.csproj @@ -16,6 +16,7 @@ + diff --git a/tests/Jellyfin.Api.Tests/JellyfinApplicationFactory.cs b/tests/Jellyfin.Api.Tests/JellyfinApplicationFactory.cs new file mode 100644 index 0000000000..77f1640fa3 --- /dev/null +++ b/tests/Jellyfin.Api.Tests/JellyfinApplicationFactory.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using Emby.Server.Implementations; +using Emby.Server.Implementations.IO; +using Emby.Server.Implementations.Networking; +using Jellyfin.Drawing.Skia; +using Jellyfin.Server; +using MediaBrowser.Common; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Extensions.Logging; + +namespace Jellyfin.Api.Tests +{ + /// + /// Factory for bootstrapping the Jellyfin application in memory for functional end to end tests. + /// + public class JellyfinApplicationFactory : WebApplicationFactory + { + private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data"); + private static readonly ConcurrentBag _disposableComponents = new ConcurrentBag(); + + /// + /// Initializes a new instance of the class. + /// + public JellyfinApplicationFactory() + { + // Perform static initialization that only needs to happen once per test-run + Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); + Program.PerformStaticInitialization(); + } + + /// + protected override IWebHostBuilder CreateWebHostBuilder() + { + return new WebHostBuilder(); + } + + /// + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + // Specify the startup command line options + var commandLineOpts = new StartupOptions + { + NoWebClient = true, + NoAutoRunWebApp = true + }; + + // Use a temporary directory for the application paths + var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); + Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs")); + Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config")); + Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache")); + Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web")); + var appPaths = new ServerApplicationPaths( + webHostPathRoot, + Path.Combine(webHostPathRoot, "logs"), + Path.Combine(webHostPathRoot, "config"), + Path.Combine(webHostPathRoot, "cache"), + Path.Combine(webHostPathRoot, "jellyfin-web")); + + // Create the logging config file + // TODO: We shouldn't need to do this since we are only logging to console + Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult(); + + // Create a copy of the application configuration to use for startup + var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths); + + ILoggerFactory loggerFactory = new SerilogLoggerFactory(); + var serviceCollection = new ServiceCollection(); + _disposableComponents.Add(loggerFactory); + + // Create the app host and initialize it + var appHost = new CoreAppHost( + appPaths, + loggerFactory, + commandLineOpts, + new ManagedFileSystem(loggerFactory.CreateLogger(), appPaths), + new NetworkManager(loggerFactory.CreateLogger()), + serviceCollection); + _disposableComponents.Add(appHost); + appHost.Init(); + + // Configure the web host builder + Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths); + } + + /// + protected override TestServer CreateServer(IWebHostBuilder builder) + { + // Create the test server using the base implementation + var testServer = base.CreateServer(builder); + + // Finish initializing the app host + var appHost = (CoreAppHost)testServer.Services.GetRequiredService(); + appHost.ServiceProvider = testServer.Services; + appHost.InitializeServices().GetAwaiter().GetResult(); + appHost.RunStartupTasksAsync().GetAwaiter().GetResult(); + + return testServer; + } + + /// + protected override void Dispose(bool disposing) + { + foreach (var disposable in _disposableComponents) + { + disposable.Dispose(); + } + + _disposableComponents.Clear(); + + base.Dispose(disposing); + } + } +} diff --git a/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs b/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs index e054908f3e..3a85b55141 100644 --- a/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs +++ b/tests/Jellyfin.Api.Tests/OpenApiSpecTests.cs @@ -6,7 +6,7 @@ using MediaBrowser.Model.Branding; using Xunit; using Xunit.Abstractions; -namespace MediaBrowser.Api.Tests +namespace Jellyfin.Api.Tests { public sealed class OpenApiSpecTests : IClassFixture { diff --git a/tests/MediaBrowser.Api.Tests/BrandingServiceTests.cs b/tests/MediaBrowser.Api.Tests/BrandingServiceTests.cs deleted file mode 100644 index 5d7f7765cd..0000000000 --- a/tests/MediaBrowser.Api.Tests/BrandingServiceTests.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Text.Json; -using System.Threading.Tasks; -using MediaBrowser.Model.Branding; -using Xunit; - -namespace MediaBrowser.Api.Tests -{ - public sealed class BrandingServiceTests : IClassFixture - { - private readonly JellyfinApplicationFactory _factory; - - public BrandingServiceTests(JellyfinApplicationFactory factory) - { - _factory = factory; - } - - [Fact] - public async Task GetConfiguration_ReturnsCorrectResponse() - { - // Arrange - var client = _factory.CreateClient(); - - // Act - var response = await client.GetAsync("/Branding/Configuration"); - - // Assert - response.EnsureSuccessStatusCode(); - Assert.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString()); - var responseBody = await response.Content.ReadAsStreamAsync(); - _ = await JsonSerializer.DeserializeAsync(responseBody); - } - - [Theory] - [InlineData("/Branding/Css")] - [InlineData("/Branding/Css.css")] - public async Task GetCss_ReturnsCorrectResponse(string url) - { - // Arrange - var client = _factory.CreateClient(); - - // Act - var response = await client.GetAsync(url); - - // Assert - response.EnsureSuccessStatusCode(); - Assert.Equal("text/css; charset=utf-8", response.Content.Headers.ContentType.ToString()); - } - } -} diff --git a/tests/MediaBrowser.Api.Tests/JellyfinApplicationFactory.cs b/tests/MediaBrowser.Api.Tests/JellyfinApplicationFactory.cs deleted file mode 100644 index 2029f88e92..0000000000 --- a/tests/MediaBrowser.Api.Tests/JellyfinApplicationFactory.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.IO; -using Emby.Server.Implementations; -using Emby.Server.Implementations.IO; -using Emby.Server.Implementations.Networking; -using Jellyfin.Drawing.Skia; -using Jellyfin.Server; -using MediaBrowser.Common; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Serilog; -using Serilog.Extensions.Logging; - -namespace MediaBrowser.Api.Tests -{ - /// - /// Factory for bootstrapping the Jellyfin application in memory for functional end to end tests. - /// - public class JellyfinApplicationFactory : WebApplicationFactory - { - private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data"); - private static readonly ConcurrentBag _disposableComponents = new ConcurrentBag(); - - /// - /// Initializes a new instance of the class. - /// - public JellyfinApplicationFactory() - { - // Perform static initialization that only needs to happen once per test-run - Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); - Program.PerformStaticInitialization(); - } - - /// - protected override IWebHostBuilder CreateWebHostBuilder() - { - return new WebHostBuilder(); - } - - /// - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - // Specify the startup command line options - var commandLineOpts = new StartupOptions - { - NoWebClient = true, - NoAutoRunWebApp = true - }; - - // Use a temporary directory for the application paths - var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); - Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs")); - Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config")); - Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache")); - Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web")); - var appPaths = new ServerApplicationPaths( - webHostPathRoot, - Path.Combine(webHostPathRoot, "logs"), - Path.Combine(webHostPathRoot, "config"), - Path.Combine(webHostPathRoot, "cache"), - Path.Combine(webHostPathRoot, "jellyfin-web")); - - // Create the logging config file - // TODO: We shouldn't need to do this since we are only logging to console - Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult(); - - // Create a copy of the application configuration to use for startup - var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths); - - ILoggerFactory loggerFactory = new SerilogLoggerFactory(); - var serviceCollection = new ServiceCollection(); - _disposableComponents.Add(loggerFactory); - - // Create the app host and initialize it - var appHost = new CoreAppHost( - appPaths, - loggerFactory, - commandLineOpts, - new ManagedFileSystem(loggerFactory.CreateLogger(), appPaths), - new NetworkManager(loggerFactory.CreateLogger()), - serviceCollection); - _disposableComponents.Add(appHost); - appHost.Init(); - - // Configure the web host builder - Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths); - } - - /// - protected override TestServer CreateServer(IWebHostBuilder builder) - { - // Create the test server using the base implementation - var testServer = base.CreateServer(builder); - - // Finish initializing the app host - var appHost = (CoreAppHost)testServer.Services.GetRequiredService(); - appHost.ServiceProvider = testServer.Services; - appHost.InitializeServices().GetAwaiter().GetResult(); - appHost.RunStartupTasksAsync().GetAwaiter().GetResult(); - - return testServer; - } - - /// - protected override void Dispose(bool disposing) - { - foreach (var disposable in _disposableComponents) - { - disposable.Dispose(); - } - - _disposableComponents.Clear(); - - base.Dispose(disposing); - } - } -} diff --git a/tests/MediaBrowser.Api.Tests/MediaBrowser.Api.Tests.csproj b/tests/MediaBrowser.Api.Tests/MediaBrowser.Api.Tests.csproj deleted file mode 100644 index b3fd853e2c..0000000000 --- a/tests/MediaBrowser.Api.Tests/MediaBrowser.Api.Tests.csproj +++ /dev/null @@ -1,34 +0,0 @@ - - - - netcoreapp3.1 - false - true - enable - - - - - - - - - - - - - - - - - - - - - - - - ../jellyfin-tests.ruleset - - - diff --git a/tests/jellyfin-tests.ruleset b/tests/jellyfin-tests.ruleset index 5a113e955e..e2abaf5bb8 100644 --- a/tests/jellyfin-tests.ruleset +++ b/tests/jellyfin-tests.ruleset @@ -1,5 +1,5 @@ - + @@ -17,6 +17,6 @@ - + -- cgit v1.2.3