mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
237 lines
13 KiB
C#
237 lines
13 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Lumina.Presentation.Web.Common.Api;
|
|
using Lumina.Presentation.Web.Common.Authorization;
|
|
using Lumina.Presentation.Web.Common.DTO.Themes;
|
|
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
|
using Lumina.Presentation.Web.Common.Primitives;
|
|
using Lumina.Presentation.Web.Common.Routes;
|
|
using Lumina.Presentation.Web.Common.Services;
|
|
using Lumina.Presentation.Web.Core.Themes;
|
|
using Lumina.Presentation.Web.Fixtures.Common.DTO.Themes;
|
|
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
using Microsoft.Extensions.Localization;
|
|
using NSubstitute;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
#endregion
|
|
|
|
namespace Lumina.Presentation.Web.UnitTests.Core.Themes;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="ThemeLayoutRenderer"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class ThemeLayoutRendererTests
|
|
{
|
|
private readonly IApiHttpClient _mockApiHttpClient;
|
|
private readonly ICompositeViewEngine _mockViewEngine;
|
|
private readonly ITempDataProvider _mockTempDataProvider;
|
|
private readonly IServiceProvider _mockServiceProvider;
|
|
private readonly IHttpContextAccessor _mockHttpContextAccessor;
|
|
private readonly IUrlService _mockUrlService;
|
|
private readonly IStringLocalizerFactory _mockStringLocalizerFactory;
|
|
private readonly IStringLocalizer _mockStringLocalizer;
|
|
private readonly IAuthorizationService _mockAuthorizationService;
|
|
private readonly IView _mockHeadView;
|
|
private readonly IView _mockAudioPlayerView;
|
|
private readonly IView _mockScriptsView;
|
|
private readonly IView _mockNavMenuView;
|
|
private readonly ThemeResponseDtoFixture _themeResponseDtoFixture = new();
|
|
private readonly ThemeTemplateResponseDtoFixture _themeTemplateResponseDtoFixture = new();
|
|
private readonly ThemeLayoutPageDtoFixture _themeLayoutPageDtoFixture = new();
|
|
private readonly ThemeLayoutRenderer _sut;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ThemeLayoutRendererTests"/> class.
|
|
/// </summary>
|
|
public ThemeLayoutRendererTests()
|
|
{
|
|
_mockApiHttpClient = Substitute.For<IApiHttpClient>();
|
|
_mockViewEngine = Substitute.For<ICompositeViewEngine>();
|
|
_mockTempDataProvider = Substitute.For<ITempDataProvider>();
|
|
_mockTempDataProvider.LoadTempData(Arg.Any<HttpContext>()).Returns(new Dictionary<string, object>());
|
|
_mockServiceProvider = Substitute.For<IServiceProvider>();
|
|
_mockServiceProvider.GetService(typeof(IModelMetadataProvider)).Returns(new EmptyModelMetadataProvider());
|
|
_mockHttpContextAccessor = Substitute.For<IHttpContextAccessor>();
|
|
|
|
_mockUrlService = Substitute.For<IUrlService>();
|
|
_mockUrlService.GetAbsoluteUrl(Arg.Any<string>(), Arg.Any<object?>()).Returns("http://localhost/en-us");
|
|
_mockStringLocalizer = CreateLocalizer();
|
|
_mockStringLocalizerFactory = Substitute.For<IStringLocalizerFactory>();
|
|
_mockStringLocalizerFactory.Create(Arg.Any<string>(), Arg.Any<string>()).Returns(_mockStringLocalizer);
|
|
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
|
_mockAuthorizationService.IsInRoleAsync("Admin", Arg.Any<CancellationToken>()).Returns(false);
|
|
_mockAuthorizationService.HasPermissionAsync(Arg.Any<AuthorizationPermission>(), Arg.Any<CancellationToken>()).Returns(false);
|
|
|
|
_mockHeadView = CreateView("<head-fragment>");
|
|
_mockAudioPlayerView = CreateView("<audio-fragment>");
|
|
_mockScriptsView = CreateView("<scripts-fragment>");
|
|
_mockNavMenuView = CreateView("<nav-menu-fragment>");
|
|
_mockViewEngine.FindView(Arg.Any<ActionContext>(), "_ThemeLayoutHead", false).Returns(ViewEngineResult.Found("_ThemeLayoutHead", _mockHeadView));
|
|
_mockViewEngine.FindView(Arg.Any<ActionContext>(), "_AudioPlayer", false).Returns(ViewEngineResult.Found("_AudioPlayer", _mockAudioPlayerView));
|
|
_mockViewEngine.FindView(Arg.Any<ActionContext>(), "_ThemeLayoutScripts", false).Returns(ViewEngineResult.Found("_ThemeLayoutScripts", _mockScriptsView));
|
|
_mockViewEngine.FindView(Arg.Any<ActionContext>(), "_NavMenu", false).Returns(ViewEngineResult.Found("_NavMenu", _mockNavMenuView));
|
|
|
|
RazorViewToStringRenderer viewRenderer = new(_mockViewEngine, _mockTempDataProvider, _mockServiceProvider, _mockHttpContextAccessor);
|
|
ThemeNavBuilder navBuilder = new(_mockUrlService, _mockStringLocalizerFactory, _mockAuthorizationService, _mockHttpContextAccessor);
|
|
ThemeService themeService = new(_mockApiHttpClient);
|
|
_sut = new ThemeLayoutRenderer(themeService, new ThemeTemplateEngine(), viewRenderer, navBuilder, _mockHttpContextAccessor);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenderAsync_WhenUserIsAuthenticated_ShouldRenderLayoutWithAllPlaceholders()
|
|
{
|
|
// Arrange
|
|
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(user: TestHttpContextFactory.CreateAuthenticatedUser()));
|
|
ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial");
|
|
SetupRenderDocuments(theme, layoutTemplate: "{{title}}|{{assetBase}}|{{{appHead}}}|{{{nav}}}|{{{content}}}|{{{audioPlayer}}}|{{{appScripts}}}|{{{scripts}}}|{{mainStyle}}", navTemplate: "nav:{{siteName}}");
|
|
ThemeLayoutPageDto page = _themeLayoutPageDtoFixture.Create(title: "My Page", content: "<main>", script: "<page-script>");
|
|
|
|
// Act
|
|
Result<string> result = await _sut.RenderAsync(page, "editorial", CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("My Page|/theme-assets/editorial/assets|<head-fragment>|nav:Lumina|<main>|<audio-fragment>|<scripts-fragment>|<page-script>|", result.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenderAsync_WhenUserIsAnonymous_ShouldRenderEmptyAudioPlayerAndBottomMainStyle()
|
|
{
|
|
// Arrange
|
|
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(user: null));
|
|
ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial");
|
|
SetupRenderDocuments(theme, layoutTemplate: "{{title}}|{{assetBase}}|{{{appHead}}}|{{{nav}}}|{{{content}}}|{{{audioPlayer}}}|{{{appScripts}}}|{{{scripts}}}|{{mainStyle}}", navTemplate: "nav:{{siteName}}");
|
|
ThemeLayoutPageDto page = _themeLayoutPageDtoFixture.Create(title: "My Page", content: "<main>", script: "<page-script>");
|
|
|
|
// Act
|
|
Result<string> result = await _sut.RenderAsync(page, "editorial", CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("My Page|/theme-assets/editorial/assets|<head-fragment>|nav:Lumina|<main>||<scripts-fragment>|<page-script>|bottom: 0px;", result.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenderAsync_WhenNavTemplateCannotBeRendered_ShouldFallBackToApplicationNavMenu()
|
|
{
|
|
// Arrange
|
|
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(user: null));
|
|
ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial");
|
|
SetupRenderDocuments(theme, layoutTemplate: "{{title}}|{{{nav}}}", navTemplate: "{{#unclosed}}");
|
|
ThemeLayoutPageDto page = _themeLayoutPageDtoFixture.Create(title: "My Page", content: "<main>", script: string.Empty);
|
|
|
|
// Act
|
|
Result<string> result = await _sut.RenderAsync(page, "editorial", CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("My Page|<nav-menu-fragment>", result.Value);
|
|
_mockViewEngine.Received(1).FindView(Arg.Any<ActionContext>(), "_NavMenu", false);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenderAsync_WhenNavDocumentCannotBeLoaded_ShouldFallBackToApplicationNavMenu()
|
|
{
|
|
// Arrange
|
|
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(user: null));
|
|
ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial");
|
|
SetupRenderDocuments(theme, layoutTemplate: "{{title}}|{{{nav}}}", navTemplate: "nav:{{siteName}}");
|
|
string navEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE
|
|
.Replace("{themeId}", "editorial")
|
|
.Replace("{*pageKey}", "shared/nav-menu");
|
|
_mockApiHttpClient.GetAsync<ThemeTemplateResponseDto>(navEndpoint, Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromException<ThemeTemplateResponseDto>(new HttpRequestException("The navigation template is unavailable.")));
|
|
ThemeLayoutPageDto page = _themeLayoutPageDtoFixture.Create(title: "My Page", content: "<main>", script: string.Empty);
|
|
|
|
// Act
|
|
Result<string> result = await _sut.RenderAsync(page, "editorial", CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("My Page|<nav-menu-fragment>", result.Value);
|
|
_mockViewEngine.Received(1).FindView(Arg.Any<ActionContext>(), "_NavMenu", false);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenderAsync_WhenCalled_ShouldResolveLayoutAndNavDocumentsFromTheme()
|
|
{
|
|
// Arrange
|
|
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(user: null));
|
|
ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial");
|
|
SetupRenderDocuments(theme, layoutTemplate: "{{title}}", navTemplate: "nav:{{siteName}}");
|
|
ThemeLayoutPageDto page = _themeLayoutPageDtoFixture.Create(title: "My Page", content: "<main>", script: string.Empty);
|
|
string layoutEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE
|
|
.Replace("{themeId}", "editorial")
|
|
.Replace("{*pageKey}", "shared/layout");
|
|
string navEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE
|
|
.Replace("{themeId}", "editorial")
|
|
.Replace("{*pageKey}", "shared/nav-menu");
|
|
|
|
// Act
|
|
await _sut.RenderAsync(page, "editorial", CancellationToken.None);
|
|
|
|
// Assert
|
|
await _mockApiHttpClient.Received(1).GetAsync<ThemeTemplateResponseDto>(layoutEndpoint, Arg.Any<CancellationToken>());
|
|
await _mockApiHttpClient.Received(1).GetAsync<ThemeTemplateResponseDto>(navEndpoint, Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stubs the API responses that return the layout and navigation menu templates of the given theme.
|
|
/// </summary>
|
|
/// <param name="theme">The theme whose layout and navigation menu templates are stubbed.</param>
|
|
/// <param name="layoutTemplate">The template returned for the layout document.</param>
|
|
/// <param name="navTemplate">The template returned for the navigation menu document.</param>
|
|
private void SetupRenderDocuments(ThemeResponseDto theme, string layoutTemplate, string navTemplate)
|
|
{
|
|
string layoutEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE
|
|
.Replace("{themeId}", theme.ThemeId)
|
|
.Replace("{*pageKey}", "shared/layout");
|
|
string navEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE
|
|
.Replace("{themeId}", theme.ThemeId)
|
|
.Replace("{*pageKey}", "shared/nav-menu");
|
|
_mockApiHttpClient.GetAsync<ThemeTemplateResponseDto>(layoutEndpoint, Arg.Any<CancellationToken>())
|
|
.Returns(_themeTemplateResponseDtoFixture.Create(theme: theme, template: layoutTemplate));
|
|
_mockApiHttpClient.GetAsync<ThemeTemplateResponseDto>(navEndpoint, Arg.Any<CancellationToken>())
|
|
.Returns(_themeTemplateResponseDtoFixture.Create(theme: theme, template: navTemplate));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a substitute view that renders the given markup.
|
|
/// </summary>
|
|
/// <param name="markup">The markup rendered by the view.</param>
|
|
/// <returns>The created substitute view.</returns>
|
|
private static IView CreateView(string markup)
|
|
{
|
|
IView view = Substitute.For<IView>();
|
|
view.RenderAsync(Arg.Any<ViewContext>())
|
|
.Returns(callInfo =>
|
|
{
|
|
callInfo.Arg<ViewContext>().Writer.Write(markup);
|
|
return Task.CompletedTask;
|
|
});
|
|
return view;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a substitute string localizer that returns a string prefixed with "Localized-".
|
|
/// </summary>
|
|
/// <returns>The created substitute string localizer.</returns>
|
|
private static IStringLocalizer CreateLocalizer()
|
|
{
|
|
IStringLocalizer localizer = Substitute.For<IStringLocalizer>();
|
|
localizer[Arg.Any<string>()].Returns(callInfo => new LocalizedString(callInfo.Arg<string>(), $"Localized-{callInfo.Arg<string>()}"));
|
|
return localizer;
|
|
}
|
|
}
|