diff options
| author | Bond_009 <bond.009@outlook.com> | 2026-11-21 10:47:20 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2026-11-21 10:47:20 +0100 |
| commit | 47d8ff8d48b2aa34ac524826d7f8c0e14ca551da (patch) | |
| tree | 2fc6cf7e9dd2d0fd0beb1a09ab30ad63f153cb62 /tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs | |
| parent | b7d24ebdf1a06123ca2b5dbb2035b90fffa42f3a (diff) | |
Rework fragile ConfigurationControllerTests
Diffstat (limited to 'tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs')
| -rw-r--r-- | tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs | 188 |
1 files changed, 24 insertions, 164 deletions
diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs index 94672d5c7b..654e75f50d 100644 --- a/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs +++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/ConfigurationControllerTests.cs @@ -26,162 +26,42 @@ using Xunit; namespace Jellyfin.Server.Integration.Tests.Controllers; -public sealed class ConfigurationControllerTests : IDisposable +public sealed class ConfigurationControllerTests : IClassFixture<JellyfinApplicationFactory> { - private readonly Dictionary<string, object> _configurations = new() - { - [string.Empty] = new ServerConfiguration(), - ["encoding"] = new EncodingOptions(), - ["network"] = new NetworkConfiguration(), - ["metadata"] = new MetadataConfiguration(), - ["xbmcmetadata"] = new XbmcMetadataOptions(), - ["livetv"] = new LiveTvOptions() - }; - - private readonly Mock<IServerConfigurationManager> _configurationManager = new(); - private readonly IHost _host; - private readonly HttpClient _client; - - public ConfigurationControllerTests() - { - _configurationManager.Setup(manager => manager.Configuration) - .Returns(() => (ServerConfiguration)_configurations[string.Empty]); - _configurationManager.Setup(manager => manager.ReplaceConfiguration(It.IsAny<BaseApplicationConfiguration>())) - .Callback<BaseApplicationConfiguration>(configuration => _configurations[string.Empty] = configuration); - _configurationManager.Setup(manager => manager.GetConfiguration(It.IsAny<string>())) - .Returns((string key) => _configurations[key]); - _configurationManager.Setup(manager => manager.SaveConfiguration(It.IsAny<string>(), It.IsAny<object>())) - .Callback<string, object>((key, configuration) => _configurations[key] = configuration); - - // Use the real API setup, but keep config in memory for these tests. - _host = new HostBuilder().ConfigureWebHost(webHost => webHost - .UseTestServer() - .ConfigureServices(services => - { - services.AddSingleton(_configurationManager.Object); - services.AddAuthentication(); - services.AddAuthorization(options => - { - options.AddPolicy(Policies.RequiresElevation, policy => policy.RequireAuthenticatedUser()); - }); - services.AddJellyfinApi(Array.Empty<Assembly>(), new NetworkConfiguration()); - }) - .Configure(app => - { - app.UseRouting(); - app.Use((context, next) => - { - context.User = new ClaimsPrincipal(new ClaimsIdentity("Test")); - return next(context); - }); - app.UseAuthorization(); - app.UseEndpoints(endpoints => endpoints.MapControllers()); - })).Start(); - _client = _host.GetTestClient(); - } - - [Theory] - [InlineData("")] - [InlineData("encoding")] - [InlineData("network")] - [InlineData("metadata")] - [InlineData("xbmcmetadata")] - [InlineData("livetv")] - public async Task UpdateConfiguration_EmptyRequest_UsesInternalDefaults(string key) - { - await AssertMatchesLegacyDeserialization(key, "{}"); - } + private readonly JellyfinApplicationFactory _factory; + private static string? _accessToken; - [Theory] - [InlineData("", """ - {"ActivityLogRetentionDays":null,"QuickConnectAvailable":false,"MinResumePct":0, - "SortRemoveWords":[],"TrickplayOptions":{"EnableHwAcceleration":true}} - """)] - [InlineData("encoding", """ - {"EnableHardwareEncoding":false,"EncodingThreadCount":0,"HardwareDecodingCodecs":[], - "H264Crf":"19","EncoderPreset":"slow","VaapiDevice":null,"UnknownOption":true} - """)] - [InlineData("network", """ - {"CertificatePath":null,"CertificatePassword":null,"InternalHttpPort":"8097", - "EnableRemoteAccess":false,"BaseUrl":"jellyfin/","KnownProxies":[]} - """)] - [InlineData("network", """ - {"BaseUrl":"","LocalNetworkSubnets":[],"LocalNetworkAddresses":[],"KnownProxies":[], - "VirtualInterfaceNames":[],"PublishedServerUriBySubnet":[],"RemoteIPFilter":[]} - """)] - [InlineData("metadata", """{"UseFileCreationTimeForDateAdded":false}""")] - [InlineData("xbmcmetadata", """{"UserId":"","ReleaseDateFormat":"yyyy-MM-dd","SaveImagePathsInNfo":false}""")] - [InlineData("livetv", """ - {"GuideDays":"","RecordingPath":null,"SaveRecordingNFO":false, - "TunerHosts":[{"Url":"http://tuner"}],"ListingProviders":[{"Type":"XmlTv"}]} - """)] - [InlineData("livetv", """{"TunerHosts":[],"ListingProviders":[],"MediaLocationsCreated":[]}""")] - public async Task UpdateConfiguration_SuppliedProperties_MatchLegacyDeserialization(string key, string json) + public ConfigurationControllerTests(JellyfinApplicationFactory factory) { - await AssertMatchesLegacyDeserialization(key, json); + _factory = factory; } - [Theory] - [InlineData("", "ServerName", "\"My server\"")] - [InlineData("encoding", "H264Crf", "18")] - [InlineData("network", "InternalHttpPort", "8097")] - [InlineData("metadata", "UseFileCreationTimeForDateAdded", "false")] - [InlineData("xbmcmetadata", "SaveImagePathsInNfo", "false")] - [InlineData("livetv", "GuideDays", "7")] - public async Task GetAndUpdateConfiguration_LowercaseRoute_RoundTripsConfiguration(string key, string property, string value) + [Fact] + public async Task GetConfiguration_Unauthorized_Unauthorized() { - using var response = await _client.GetAsync(GetUrl(key), TestContext.Current.CancellationToken); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType); + var client = _factory.CreateClient(); - var body = JsonNode.Parse(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken))!.AsObject(); - var expected = JsonSerializer.SerializeToNode(_configurations[key], JsonDefaults.Options)!.AsObject(); - Assert.True(JsonNode.DeepEquals(expected, body)); - - body[property] = JsonNode.Parse(value); - expected[property] = JsonNode.Parse(value); - await PostConfiguration(key, body.ToJsonString()); - - Assert.True(JsonNode.DeepEquals(expected, JsonSerializer.SerializeToNode(_configurations[key], JsonDefaults.Options))); + using var request = new HttpRequestMessage(HttpMethod.Get, GetUrl("encoding")); + using var response = await client.SendAsync(request, TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); } [Fact] public async Task UpdateConfiguration_CamelCaseProperties_AreApplied() { - await PostConfiguration("encoding", """{"h264Crf":18,"enableHardwareEncoding":false}"""); + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); - var configuration = Assert.IsType<EncodingOptions>(_configurations["encoding"]); - Assert.Equal(18, configuration.H264Crf); - Assert.False(configuration.EnableHardwareEncoding); - Assert.Equal(new EncodingOptions().H265Crf, configuration.H265Crf); + await PostConfiguration(client, "encoding", """{"h264Crf":18,"enableHardwareEncoding":false}"""); using var request = new HttpRequestMessage(HttpMethod.Get, GetUrl("encoding")); request.Headers.Accept.ParseAdd(JsonDefaults.CamelCaseMediaType); - using var response = await _client.SendAsync(request, TestContext.Current.CancellationToken); + using var response = await client.SendAsync(request, TestContext.Current.CancellationToken); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var body = JsonNode.Parse(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)); - Assert.Equal(18, body!["h264Crf"]!.GetValue<int>()); - } - - [Fact] - public async Task UpdateConfiguration_OmittedProperties_ResetToDefaultsRatherThanCurrentValues() - { - await PostConfiguration("encoding", """{"H264Crf":18,"EnableHardwareEncoding":false}"""); - await AssertMatchesLegacyDeserialization("encoding", "{}"); - } - - [Fact] - public async Task UpdateConfiguration_DefaultObjects_AreNotSharedBetweenRequests() - { - await PostConfiguration(string.Empty, "{}"); - var first = Assert.IsType<ServerConfiguration>(_configurations[string.Empty]); - first.MetadataOptions[0].ItemType = "Changed"; - first.TrickplayOptions.WidthResolutions[0] = 123; - - await AssertMatchesLegacyDeserialization(string.Empty, "{}"); - var second = Assert.IsType<ServerConfiguration>(_configurations[string.Empty]); - Assert.NotSame(first.MetadataOptions, second.MetadataOptions); - Assert.NotSame(first.TrickplayOptions, second.TrickplayOptions); + Assert.NotNull(body); + Assert.NotNull(body["h264Crf"]); + Assert.Equal(18, body["h264Crf"]!.GetValue<int>()); } [Theory] @@ -203,18 +83,13 @@ public sealed class ConfigurationControllerTests : IDisposable [InlineData("", """{"ServerName":null}""")] public async Task UpdateConfiguration_InvalidRequest_DoesNotSave(string key, string json) { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); + using var content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json); - using var response = await _client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken); + using var response = await client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); - _configurationManager.Verify(manager => manager.SaveConfiguration(It.IsAny<string>(), It.IsAny<object>()), Times.Never); - _configurationManager.Verify(manager => manager.ReplaceConfiguration(It.IsAny<BaseApplicationConfiguration>()), Times.Never); - } - - public void Dispose() - { - _client.Dispose(); - _host.Dispose(); } private static string GetUrl(string key) @@ -222,25 +97,10 @@ public sealed class ConfigurationControllerTests : IDisposable return string.IsNullOrEmpty(key) ? "/System/Configuration" : "/System/Configuration/" + key; } - private async Task AssertMatchesLegacyDeserialization(string key, string json) - { - // Compare with how the old endpoint read the same JSON. - var type = _configurations[key].GetType(); - var expected = JsonSerializer.Deserialize(json, type, JsonDefaults.Options); - - await PostConfiguration(key, json); - - var actual = _configurations[key]; - Assert.IsType(type, actual); - Assert.True(JsonNode.DeepEquals( - JsonSerializer.SerializeToNode(expected, type, JsonDefaults.Options), - JsonSerializer.SerializeToNode(actual, type, JsonDefaults.Options))); - } - - private async Task PostConfiguration(string key, string json) + private async Task PostConfiguration(HttpClient client, string key, string json) { using var content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json); - using var response = await _client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken); + using var response = await client.PostAsync(GetUrl(key), content, TestContext.Current.CancellationToken); Assert.True( response.StatusCode == HttpStatusCode.NoContent, $"{response.StatusCode}: {await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)}"); |
