-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64d74d4
commit 30bf460
Showing
9 changed files
with
248 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 100 additions & 100 deletions
200
tests/Serilog.Ui.Web.Tests/Endpoints/SerilogUiDecoratorsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,104 @@ | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute; | ||
using Serilog.Ui.Web; | ||
using Serilog.Ui.Web.Authorization; | ||
using Serilog.Ui.Web.Endpoints; | ||
using Serilog.Ui.Web.Tests.Authorization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Ui.Web.Tests.Endpoints | ||
{ | ||
[Trait("Ui-Api-Decorators", "Web")] | ||
public class SerilogUiDecoratorsTest | ||
{ | ||
private readonly AuthorizationFilterService authMock; | ||
private readonly ISerilogUiAppRoutes appRoutesMock; | ||
private readonly ISerilogUiEndpoints endpointMock; | ||
private readonly SerilogUiAppRoutesDecorator sutRoutesDecorator; | ||
private readonly SerilogUiEndpointsDecorator sutEndpointsDecorator; | ||
|
||
public SerilogUiDecoratorsTest() | ||
{ | ||
authMock = new AuthorizationFilterService(); | ||
appRoutesMock = Substitute.For<ISerilogUiAppRoutes>(); | ||
endpointMock = Substitute.For<ISerilogUiEndpoints>(); | ||
appRoutesMock.GetHome(Arg.Any<HttpContext>()); | ||
appRoutesMock.RedirectHome(Arg.Any<HttpContext>()); | ||
endpointMock.GetLogs(Arg.Any<HttpContext>()); | ||
endpointMock.GetApiKeys(Arg.Any<HttpContext>()); | ||
|
||
sutRoutesDecorator = new SerilogUiAppRoutesDecorator(appRoutesMock, authMock); | ||
sutEndpointsDecorator = new SerilogUiEndpointsDecorator(endpointMock, authMock); | ||
} | ||
|
||
[Fact] | ||
public async Task It_forwards_the_call_to_app_endpoints_on_success_authentication() | ||
{ | ||
sutEndpointsDecorator.SetOptions(new()); | ||
|
||
await sutEndpointsDecorator.GetLogs(new DefaultHttpContext()); | ||
await sutEndpointsDecorator.GetApiKeys(new DefaultHttpContext()); | ||
|
||
await endpointMock.Received().GetLogs(Arg.Any<HttpContext>()); | ||
await endpointMock.Received().GetApiKeys(Arg.Any<HttpContext>()); | ||
} | ||
|
||
[Fact] | ||
public async Task It_forwards_the_call_to_app_routes_when_unauth_page_access_is_enabled() | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using NSubstitute; | ||
using Serilog.Ui.Web; | ||
using Serilog.Ui.Web.Authorization; | ||
using Serilog.Ui.Web.Endpoints; | ||
using Serilog.Ui.Web.Tests.Authorization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Ui.Web.Tests.Endpoints | ||
{ | ||
[Trait("Ui-Api-Decorators", "Web")] | ||
public class SerilogUiDecoratorsTest | ||
{ | ||
private readonly AuthorizationFilterService _authMock; | ||
private readonly ISerilogUiAppRoutes _appRoutesMock; | ||
private readonly ISerilogUiEndpoints _endpointMock; | ||
private readonly SerilogUiAppRoutesDecorator _sutRoutesDecorator; | ||
private readonly SerilogUiEndpointsDecorator _sutEndpointsDecorator; | ||
|
||
public SerilogUiDecoratorsTest() | ||
{ | ||
_authMock = new AuthorizationFilterService(); | ||
_appRoutesMock = Substitute.For<ISerilogUiAppRoutes>(); | ||
_endpointMock = Substitute.For<ISerilogUiEndpoints>(); | ||
_appRoutesMock.GetHome(Arg.Any<HttpContext>()); | ||
_appRoutesMock.RedirectHome(Arg.Any<HttpContext>()); | ||
_endpointMock.GetLogs(Arg.Any<HttpContext>()); | ||
_endpointMock.GetApiKeys(Arg.Any<HttpContext>()); | ||
|
||
_sutRoutesDecorator = new SerilogUiAppRoutesDecorator(_appRoutesMock, _authMock); | ||
_sutEndpointsDecorator = new SerilogUiEndpointsDecorator(_endpointMock, _authMock); | ||
} | ||
|
||
[Fact] | ||
public async Task It_forwards_the_call_to_app_endpoints_on_success_authentication() | ||
{ | ||
_sutEndpointsDecorator.SetOptions(new()); | ||
|
||
await _sutEndpointsDecorator.GetLogs(new DefaultHttpContext()); | ||
await _sutEndpointsDecorator.GetApiKeys(new DefaultHttpContext()); | ||
|
||
await _endpointMock.Received().GetLogs(Arg.Any<HttpContext>()); | ||
await _endpointMock.Received().GetApiKeys(Arg.Any<HttpContext>()); | ||
} | ||
|
||
[Fact] | ||
public async Task It_forwards_the_call_to_app_routes_when_unauth_page_access_is_enabled() | ||
{ | ||
sutRoutesDecorator.SetOptions(new() { }); | ||
await sutRoutesDecorator.GetHome(new DefaultHttpContext()); | ||
await sutRoutesDecorator.RedirectHome(new DefaultHttpContext()); | ||
_sutRoutesDecorator.SetOptions(new() { }); | ||
await _sutRoutesDecorator.GetHome(new DefaultHttpContext()); | ||
await _sutRoutesDecorator.RedirectHome(new DefaultHttpContext()); | ||
|
||
await appRoutesMock.Received().GetHome(Arg.Any<HttpContext>()); | ||
await appRoutesMock.Received().RedirectHome(Arg.Any<HttpContext>()); | ||
await _appRoutesMock.Received().GetHome(Arg.Any<HttpContext>()); | ||
await _appRoutesMock.Received().RedirectHome(Arg.Any<HttpContext>()); | ||
} | ||
|
||
[Fact] | ||
public async Task It_blocks_the_call_on_failed_authentication() | ||
{ | ||
var uiOpts = new UiOptions() { Authorization = new() { RunAuthorizationFilterOnAppRoutes = true } }; | ||
uiOpts.Authorization.Filters = new IUiAuthorizationFilter[] { new ForbidLocalRequestFilter() }; | ||
sutRoutesDecorator.SetOptions(uiOpts); | ||
sutEndpointsDecorator.SetOptions(uiOpts); | ||
|
||
var defaultHttp = new DefaultHttpContext(); | ||
await sutRoutesDecorator.RedirectHome(defaultHttp); | ||
defaultHttp.Response.StatusCode.Should().Be(403); | ||
await appRoutesMock.DidNotReceive().RedirectHome(Arg.Any<HttpContext>()); | ||
|
||
var defaultHttp2 = new DefaultHttpContext(); | ||
await sutEndpointsDecorator.GetLogs(defaultHttp2); | ||
defaultHttp2.Response.StatusCode.Should().Be(403); | ||
await endpointMock.DidNotReceive().GetLogs(Arg.Any<HttpContext>()); | ||
|
||
var defaultHttp3 = new DefaultHttpContext(); | ||
await sutEndpointsDecorator.GetApiKeys(defaultHttp3); | ||
defaultHttp3.Response.StatusCode.Should().Be(403); | ||
await endpointMock.DidNotReceive().GetApiKeys(Arg.Any<HttpContext>()); | ||
} | ||
|
||
[Fact] | ||
public async Task It_blocks_the_GetHome_on_failed_authentication_with_custom_delegate() | ||
{ | ||
var uiOpts = new UiOptions() { Authorization = new() { RunAuthorizationFilterOnAppRoutes = true } }; | ||
uiOpts.Authorization.Filters = new IUiAuthorizationFilter[] { new ForbidLocalRequestFilter() }; | ||
sutRoutesDecorator.SetOptions(uiOpts); | ||
sutEndpointsDecorator.SetOptions(uiOpts); | ||
|
||
var defaultHttp = new DefaultHttpContext(); | ||
defaultHttp.Response.Body = new MemoryStream(); | ||
await sutRoutesDecorator.GetHome(defaultHttp); | ||
|
||
defaultHttp.Response.StatusCode.Should().Be(403); | ||
await appRoutesMock.DidNotReceive().GetHome(Arg.Any<HttpContext>()); | ||
|
||
defaultHttp.Response.Body.Seek(0, SeekOrigin.Begin); | ||
var bodyWrite = await new StreamReader(defaultHttp.Response.Body).ReadToEndAsync(); | ||
bodyWrite.Should().Be("<p>You don't have enough permission to access this page!</p>"); | ||
} | ||
} | ||
} | ||
[Fact] | ||
public async Task It_blocks_the_call_on_failed_authentication() | ||
{ | ||
var uiOpts = new UiOptions() { Authorization = new() { RunAuthorizationFilterOnAppRoutes = true } }; | ||
uiOpts.Authorization.Filters = new IUiAuthorizationFilter[] { new ForbidLocalRequestFilter() }; | ||
_sutRoutesDecorator.SetOptions(uiOpts); | ||
_sutEndpointsDecorator.SetOptions(uiOpts); | ||
|
||
var defaultHttp = new DefaultHttpContext(); | ||
await _sutRoutesDecorator.RedirectHome(defaultHttp); | ||
defaultHttp.Response.StatusCode.Should().Be(403); | ||
await _appRoutesMock.DidNotReceive().RedirectHome(Arg.Any<HttpContext>()); | ||
|
||
var defaultHttp2 = new DefaultHttpContext(); | ||
await _sutEndpointsDecorator.GetLogs(defaultHttp2); | ||
defaultHttp2.Response.StatusCode.Should().Be(403); | ||
await _endpointMock.DidNotReceive().GetLogs(Arg.Any<HttpContext>()); | ||
|
||
var defaultHttp3 = new DefaultHttpContext(); | ||
await _sutEndpointsDecorator.GetApiKeys(defaultHttp3); | ||
defaultHttp3.Response.StatusCode.Should().Be(403); | ||
await _endpointMock.DidNotReceive().GetApiKeys(Arg.Any<HttpContext>()); | ||
} | ||
|
||
[Fact] | ||
public async Task It_blocks_the_GetHome_on_failed_authentication_with_custom_delegate() | ||
{ | ||
var uiOpts = new UiOptions() { Authorization = new() { RunAuthorizationFilterOnAppRoutes = true } }; | ||
uiOpts.Authorization.Filters = new IUiAuthorizationFilter[] { new ForbidLocalRequestFilter() }; | ||
_sutRoutesDecorator.SetOptions(uiOpts); | ||
_sutEndpointsDecorator.SetOptions(uiOpts); | ||
|
||
var defaultHttp = new DefaultHttpContext(); | ||
defaultHttp.Response.Body = new MemoryStream(); | ||
await _sutRoutesDecorator.GetHome(defaultHttp); | ||
|
||
defaultHttp.Response.StatusCode.Should().Be(403); | ||
await _appRoutesMock.DidNotReceive().GetHome(Arg.Any<HttpContext>()); | ||
|
||
defaultHttp.Response.Body.Seek(0, SeekOrigin.Begin); | ||
var bodyWrite = await new StreamReader(defaultHttp.Response.Body).ReadToEndAsync(); | ||
bodyWrite.Should().Be("<p>You don't have enough permission to access this page!</p>"); | ||
} | ||
} | ||
} |
Oops, something went wrong.