Skip to content

Commit

Permalink
fix(decorators-test): refactor setup to consider new variable assigna…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
followynne authored Sep 11, 2024
1 parent f397dd5 commit c30ee9a
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions tests/Serilog.Ui.Web.Tests/Endpoints/SerilogUiDecoratorsTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using NSubstitute.ClearExtensions;
using Serilog.Ui.Core.Interfaces;
using Serilog.Ui.Core.Models.Options;
using Serilog.Ui.Web.Authorization;
Expand All @@ -17,31 +16,23 @@ namespace Serilog.Ui.Web.Tests.Endpoints;
[Trait("Ui-Api-Decorators", "Web")]
public class SerilogUiDecoratorsTest
{
private readonly IHttpContextAccessor _contextAccessor;
private readonly IHttpContextAccessor _contextAccessor = Substitute.For<IHttpContextAccessor>();

private readonly List<IUiAuthorizationFilter> _syncFilters = [];

private readonly ISerilogUiAppRoutes _appRoutesMock;
private readonly ISerilogUiAppRoutes _appRoutesMock = Substitute.For<ISerilogUiAppRoutes>();

private readonly ISerilogUiEndpoints _endpointMock;
private readonly ISerilogUiEndpoints _endpointMock = Substitute.For<ISerilogUiEndpoints>();

private readonly SerilogUiAppRoutesDecorator _sutRoutesDecorator;

private readonly SerilogUiEndpointsDecorator _sutEndpointsDecorator;

public SerilogUiDecoratorsTest()
{
var context = new DefaultHttpContext();
_contextAccessor = Substitute.For<IHttpContextAccessor>();
_contextAccessor.HttpContext.Returns(context);

var authMock = new AuthorizationFilterService(_contextAccessor, _syncFilters, []);

_appRoutesMock = Substitute.For<ISerilogUiAppRoutes>();
_endpointMock = Substitute.For<ISerilogUiEndpoints>();

_sutRoutesDecorator = new SerilogUiAppRoutesDecorator(_contextAccessor, _appRoutesMock, authMock);
_sutEndpointsDecorator = new SerilogUiEndpointsDecorator(_endpointMock, authMock);
var (serilogUiAppRoutesDecorator, serilogUiEndpointsDecorator) = CreateTarget();
_sutRoutesDecorator = serilogUiAppRoutesDecorator;
_sutEndpointsDecorator = serilogUiEndpointsDecorator;
}

[Fact]
Expand Down Expand Up @@ -103,33 +94,28 @@ public async Task It_forwards_the_call_to_RedirectHome_always()
[Fact]
public async Task It_blocks_the_call_on_failed_authentication()
{
// Arrange
// Arrange: reset target to register the new HttpContext variable
var uiOpts = new UiOptions(new ProvidersOptions()).EnableAuthorizationOnAppRoutes();

_syncFilters.Add(new ForbidLocalRequestFilter(_contextAccessor));
_sutRoutesDecorator.SetOptions(uiOpts);
_sutEndpointsDecorator.SetOptions(uiOpts);

// Arrange
_contextAccessor.ClearSubstitute();
var cleanContext = new DefaultHttpContext();
_contextAccessor.HttpContext.Returns(cleanContext);
var (_, logsEndpointDecorator) = CreateTarget();
logsEndpointDecorator.SetOptions(uiOpts);

// Act
await _sutEndpointsDecorator.GetLogsAsync();
await logsEndpointDecorator.GetLogsAsync();

// Assert
cleanContext.Response.StatusCode.Should().Be(403);
_contextAccessor.HttpContext!.Response.StatusCode.Should().Be(403);
await _endpointMock.DidNotReceive().GetLogsAsync();

// Arrange
_contextAccessor.ClearSubstitute();
var cleanContext2 = new DefaultHttpContext();
_contextAccessor.HttpContext.Returns(cleanContext2);
// Arrange: reset target to register the new HttpContext variable
var (_, apiKeysEndpointDecorator) = CreateTarget();
apiKeysEndpointDecorator.SetOptions(uiOpts);

// Act
await _sutEndpointsDecorator.GetApiKeysAsync();
await apiKeysEndpointDecorator.GetApiKeysAsync();
// Assert
cleanContext2.Response.StatusCode.Should().Be(403);
_contextAccessor.HttpContext.Response.StatusCode.Should().Be(403);
await _endpointMock.DidNotReceive().GetApiKeysAsync();
}

Expand Down Expand Up @@ -159,4 +145,16 @@ public async Task It_return_GetHome_on_failed_authentication_with_custom_ui_opti
}

private static UiOptions GetOptions() => new(new ProvidersOptions());
}

private (SerilogUiAppRoutesDecorator, SerilogUiEndpointsDecorator) CreateTarget()
{
var context = new DefaultHttpContext();
_contextAccessor.HttpContext.Returns(context);

var authMock = new AuthorizationFilterService(_contextAccessor, _syncFilters, []);

return (
new SerilogUiAppRoutesDecorator(_contextAccessor, _appRoutesMock, authMock),
new SerilogUiEndpointsDecorator(_endpointMock, authMock));
}
}

0 comments on commit c30ee9a

Please sign in to comment.