Skip to content

Commit

Permalink
chore: rename private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
followynne committed Sep 17, 2023
1 parent 64d74d4 commit 30bf460
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 312 deletions.
60 changes: 30 additions & 30 deletions tests/Serilog.Ui.Web.Tests/Endpoints/SerilogUiAppRoutesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ namespace Ui.Web.Tests.Endpoints
[Trait("Ui-Api-Routes", "Web")]
public class SerilogUiAppRoutesTest
{
private readonly IAppStreamLoader streamLoaderMock;
private readonly SerilogUiAppRoutes sut;
private readonly DefaultHttpContext testContext;
private readonly IAppStreamLoader _streamLoaderMock;
private readonly SerilogUiAppRoutes _sut;
private readonly DefaultHttpContext _testContext;

public SerilogUiAppRoutesTest()
{
testContext = new DefaultHttpContext();
testContext.Request.Host = new HostString("test.dev");
testContext.Request.Scheme = "https";
streamLoaderMock = Substitute.For<IAppStreamLoader>();
sut = new SerilogUiAppRoutes(streamLoaderMock);
_testContext = new DefaultHttpContext();
_testContext.Request.Host = new HostString("test.dev");
_testContext.Request.Scheme = "https";
_streamLoaderMock = Substitute.For<IAppStreamLoader>();
_sut = new SerilogUiAppRoutes(_streamLoaderMock);
}

[Fact]
public async Task It_gets_app_home()
{
sut.SetOptions(new()
_sut.SetOptions(new()
{
BodyContent = "<div>body-test</div>",
HeadContent = "<div>head-test</div>",
Authorization = new() { AuthenticationType = AuthenticationType.Jwt },
RoutePrefix = "test",
HomeUrl = "home-url"
});
testContext.Request.Path = "/serilog-ui-url/index.html";
testContext.Response.Body = new MemoryStream();
_testContext.Request.Path = "/serilog-ui-url/index.html";
_testContext.Response.Body = new MemoryStream();

using var stream = new MemoryStream(Encoding.UTF8.GetBytes(
"<!DOCTYPE html><html lang=\"en\"><head><meta name=\"dummy\" content=\"%(HeadContent)\"></head>" +
"<body><div id=\"serilog-ui-app\"></div><script>const config = '%(Configs)';</script>" +
"<meta name=\"dummy\" content=\"%(BodyContent)\"></body></html>"));
streamLoaderMock.GetIndex().Returns(stream);
_streamLoaderMock.GetIndex().Returns(stream);

await sut.GetHome(testContext);
await _sut.GetHome(_testContext);

testContext.Response.StatusCode.Should().Be(200);
testContext.Response.ContentType.Should().Be("text/html;charset=utf-8");
_testContext.Response.StatusCode.Should().Be(200);
_testContext.Response.ContentType.Should().Be("text/html;charset=utf-8");

testContext.Response.Body.Seek(0, SeekOrigin.Begin);
var bodyWrite = await new StreamReader(testContext.Response.Body).ReadToEndAsync();
_testContext.Response.Body.Seek(0, SeekOrigin.Begin);
var bodyWrite = await new StreamReader(_testContext.Response.Body).ReadToEndAsync();
bodyWrite.Should().Be(
"<!DOCTYPE html><html lang=\"en\"><head><div>head-test</div></head>" +
"<body><div id=\"serilog-ui-app\"></div>" +
Expand All @@ -66,34 +66,34 @@ public async Task It_gets_app_home()
[Fact]
public async Task It_returns_page_error_when_stream_cannot_load_app_home()
{
sut.SetOptions(new());
testContext.Request.Path = "/serilog-ui-url/index.html";
testContext.Response.Body = new MemoryStream();
streamLoaderMock.GetIndex().Returns((Stream)null!);
_sut.SetOptions(new());
_testContext.Request.Path = "/serilog-ui-url/index.html";
_testContext.Response.Body = new MemoryStream();
_streamLoaderMock.GetIndex().Returns((Stream)null!);

await sut.GetHome(testContext);
await _sut.GetHome(_testContext);

testContext.Response.StatusCode.Should().Be(500);
_testContext.Response.StatusCode.Should().Be(500);

testContext.Response.Body.Seek(0, SeekOrigin.Begin);
var bodyWrite = await new StreamReader(testContext.Response.Body).ReadToEndAsync();
_testContext.Response.Body.Seek(0, SeekOrigin.Begin);
var bodyWrite = await new StreamReader(_testContext.Response.Body).ReadToEndAsync();
bodyWrite.Should().Be("<div>Server error while loading assets. Please contact administration.</div>");
}

[Fact]
public async Task It_redirects_app_home()
{
testContext.Request.Path = "/serilog-ui-url/";
await sut.RedirectHome(testContext);
_testContext.Request.Path = "/serilog-ui-url/";
await _sut.RedirectHome(_testContext);

testContext.Response.StatusCode.Should().Be(301);
testContext.Response.Headers.Location[0].Should().Be("https://test.dev/serilog-ui-url/index.html");
_testContext.Response.StatusCode.Should().Be(301);
_testContext.Response.Headers.Location[0].Should().Be("https://test.dev/serilog-ui-url/index.html");
}

[Fact]
public Task It_throws_on_app_home_if_ui_options_were_not_set()
{
var result = () => sut.GetHome(new DefaultHttpContext());
var result = () => _sut.GetHome(new DefaultHttpContext());

return result.Should().ThrowAsync<ArgumentNullException>();
}
Expand Down
200 changes: 100 additions & 100 deletions tests/Serilog.Ui.Web.Tests/Endpoints/SerilogUiDecoratorsTest.cs
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>");
}
}
}
Loading

0 comments on commit 30bf460

Please sign in to comment.