diff --git a/src/Lumina.Presentation.Web/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpoint.cs b/src/Lumina.Presentation.Web/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpoint.cs index 63968658..91cb0752 100644 --- a/src/Lumina.Presentation.Web/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpoint.cs +++ b/src/Lumina.Presentation.Web/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpoint.cs @@ -54,8 +54,16 @@ public class PluginsIndexViewEndpoint : BaseEndpoint PluginDto[] plugins = await _apiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGINS, cancellationToken).ConfigureAwait(false); Dictionary settingsByPluginId = []; foreach (PluginDto plugin in plugins) + { if (plugin.LoadStatus == Lumina.Presentation.Web.Common.Enums.Plugins.PluginLoadStatus.Loaded) - settingsByPluginId[plugin.Id] = await _apiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGIN_SETTINGS.Replace("{pluginId}", plugin.Id.ToString()), cancellationToken).ConfigureAwait(false); + { + PluginSettingsDto pluginSettings = await _apiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGIN_SETTINGS.Replace("{pluginId}", plugin.Id.ToString()), cancellationToken).ConfigureAwait(false); + // A plugin that exposes an empty settings schema has nothing to configure, so it is left out of the map the view renders + // the settings buttons and panels from, instead of showing a Settings button that opens an empty panel. + if (pluginSettings.Schema.Count > 0) + settingsByPluginId[plugin.Id] = pluginSettings; + } + } Dictionary viewData = new() { ["pluginSettings"] = settingsByPluginId }; return View("/Core/Views/Admin/Plugins.cshtml", plugins, viewData); } diff --git a/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpointTests.cs b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpointTests.cs new file mode 100644 index 00000000..355c8efa --- /dev/null +++ b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Core/Endpoints/Plugins/Index/PluginsIndexViewEndpointTests.cs @@ -0,0 +1,177 @@ +#region ========================================================================= USING ===================================================================================== +using FastEndpoints; +using Lumina.Presentation.Web.Common.Api; +using Lumina.Presentation.Web.Common.DTO.Plugins; +using Lumina.Presentation.Web.Common.Enums.Plugins; +using Lumina.Presentation.Web.Common.Http; +using Lumina.Presentation.Web.Common.Routes; +using Lumina.Presentation.Web.Core.Endpoints.Plugins.Index; +using Lumina.Presentation.Web.Fixtures.Common.DTO.Plugins; +using Lumina.Presentation.Web.Fixtures.Common.TestHelpers; +using Microsoft.AspNetCore.Http; +using NSubstitute; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +#endregion + +namespace Lumina.Presentation.Web.UnitTests.Core.Endpoints.Plugins.Index; + +/// +/// Contains unit tests for the class. +/// +[ExcludeFromCodeCoverage] +public class PluginsIndexViewEndpointTests +{ + private readonly IApiHttpClient _mockApiHttpClient; + private readonly PluginsIndexViewEndpoint _sut; + private readonly PluginDtoFixture _pluginDtoFixture = new(); + private readonly PluginSettingsDtoFixture _pluginSettingsDtoFixture = new(); + + /// + /// Initializes a new instance of the class. + /// + public PluginsIndexViewEndpointTests() + { + _mockApiHttpClient = Substitute.For(); + _sut = Factory.Create(_mockApiHttpClient); + TestHttpContextFactory.ConfigureSession(_sut.HttpContext, TestHttpContextFactory.CreateSession()); + } + + [Fact] + public async Task ExecuteAsync_WhenALoadedPluginHasNoSettings_ShouldLeaveItOutOfThePluginSettingsViewData() + { + // Arrange + PluginDto plugin = _pluginDtoFixture.Create(); + PluginSettingsDto pluginSettings = _pluginSettingsDtoFixture.Create(pluginId: plugin.Id, includeSchema: false); + _mockApiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGINS, Arg.Any()) + .Returns([plugin]); + _mockApiHttpClient.GetAsync(BuildSettingsEndpoint(plugin.Id), Arg.Any()) + .Returns(pluginSettings); + + // Act + IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None); + + // Assert + Dictionary settingsByPluginId = GetPluginSettingsViewData(result); + Assert.False(settingsByPluginId.ContainsKey(plugin.Id)); + } + + [Fact] + public async Task ExecuteAsync_WhenALoadedPluginHasSettings_ShouldExposeItsSettingsInThePluginSettingsViewData() + { + // Arrange + PluginDto plugin = _pluginDtoFixture.Create(); + PluginSettingsDto pluginSettings = _pluginSettingsDtoFixture.Create(pluginId: plugin.Id); + _mockApiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGINS, Arg.Any()) + .Returns([plugin]); + _mockApiHttpClient.GetAsync(BuildSettingsEndpoint(plugin.Id), Arg.Any()) + .Returns(pluginSettings); + + // Act + IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None); + + // Assert + Dictionary settingsByPluginId = GetPluginSettingsViewData(result); + Assert.True(settingsByPluginId.TryGetValue(plugin.Id, out PluginSettingsDto? actualSettings)); + Assert.Same(pluginSettings, actualSettings); + await _mockApiHttpClient.Received(1).GetAsync(BuildSettingsEndpoint(plugin.Id), Arg.Any()); + } + + [Fact] + public async Task ExecuteAsync_WhenAPluginFailedToLoad_ShouldNotRequestItsSettings() + { + // Arrange + PluginDto plugin = _pluginDtoFixture.Create(loadStatus: PluginLoadStatus.FailedToLoad); + _mockApiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGINS, Arg.Any()) + .Returns([plugin]); + + // Act + await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None); + + // Assert + await _mockApiHttpClient.DidNotReceive().GetAsync(Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task ExecuteAsync_WhenCalled_ShouldRenderThePluginsViewWithThePlugins() + { + // Arrange + PluginDto[] plugins = [.. _pluginDtoFixture.CreateMany(2)]; + foreach (PluginDto plugin in plugins) + _mockApiHttpClient.GetAsync(BuildSettingsEndpoint(plugin.Id), Arg.Any()) + .Returns(_pluginSettingsDtoFixture.Create(pluginId: plugin.Id)); + _mockApiHttpClient.GetAsync(ApiRoutes.Plugins.GET_PLUGINS, Arg.Any()) + .Returns(plugins); + + // Act + IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, CancellationToken.None); + + // Assert + Assert.Equal("/Core/Views/Admin/Plugins.cshtml", GetRenderedViewName(result)); + Assert.Same(plugins, GetRenderedModel(result)); + } + + /// + /// Builds the API route of the settings of the plugin identified by the provided identifier. + /// + /// The unique identifier of the plugin. + /// The API route of the plugin settings. + private static string BuildSettingsEndpoint(Guid pluginId) + { + return ApiRoutes.Plugins.GET_PLUGIN_SETTINGS.Replace("{pluginId}", pluginId.ToString()); + } + + /// + /// Reads the plugin settings map exposed to the rendered view. + /// + /// The result returned by the endpoint under test. + /// The plugin settings keyed by plugin identifier. + private static Dictionary GetPluginSettingsViewData(IResult result) + { + IReadOnlyDictionary viewData = GetRenderedViewData(result); + return Assert.IsType>(viewData["pluginSettings"]); + } + + /// + /// Reads the private view name of the rendered , which the result does not expose publicly. + /// + /// The result returned by the endpoint under test. + /// The name or path of the rendered view. + private static string GetRenderedViewName(IResult result) + { + RazorViewResult viewResult = Assert.IsType(result); + FieldInfo? viewNameField = typeof(RazorViewResult).GetField("_viewName", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.NotNull(viewNameField); + return (string)viewNameField.GetValue(viewResult)!; + } + + /// + /// Reads the private model of the rendered , which the result does not expose publicly. + /// + /// The result returned by the endpoint under test. + /// The model passed to the rendered view. + private static object? GetRenderedModel(IResult result) + { + RazorViewResult viewResult = Assert.IsType(result); + FieldInfo? modelField = typeof(RazorViewResult).GetField("_model", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.NotNull(modelField); + return modelField.GetValue(viewResult); + } + + /// + /// Reads the private view data of the rendered , which the result does not expose publicly. + /// + /// The result returned by the endpoint under test. + /// The view data entries passed to the rendered view. + private static IReadOnlyDictionary GetRenderedViewData(IResult result) + { + RazorViewResult viewResult = Assert.IsType(result); + FieldInfo? viewDataField = typeof(RazorViewResult).GetField("_viewData", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.NotNull(viewDataField); + return (IReadOnlyDictionary)viewDataField.GetValue(viewResult)!; + } +}