Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui-web): refactor the web backend, create tests #88

Merged

Conversation

followynne
Copy link
Member

@followynne followynne commented Sep 1, 2023

Hello @mo-esmp

this PR aims to refactor the backend part of Web.Ui, to improve maintanability and to be able to test it in depth with a simpler approach.

(I did this part while working on the react-ui; since I'm going longer than expected with the react part (missing the css parts and a couple feat related to the auth token), I'm making this PR with the pieces that are independent-completed)

It includes

  • refactoring of Ui.Web
  • new options parameter to block access to index.html from the start (default to false, to preserve compatibility)
  • coverage of UI.Web project backend
  • some sonarqube smell fixed
  • bumped nuget packages (safe ones, I left out any "risky" package for another time)
  • replaced Moq with Nsubstitute due to the issues raised last month with Moq

@followynne followynne marked this pull request as ready for review September 1, 2023 16:44
@followynne followynne changed the title feat(ui.web): refactor the web backend, create tests feat(ui-web): refactor the web backend, create tests Sep 1, 2023
@followynne followynne enabled auto-merge (squash) September 1, 2023 17:22
@mo-esmp
Copy link
Member

mo-esmp commented Sep 1, 2023

Hey @followynne,
Thanks for the PR. I will review it soon and since the PR is big, it takes some time.

@followynne
Copy link
Member Author

@mo-esmp sure, no worries - take all the time you need! 👍

Since I didn't proceed in the last weeks on the React part, I wanted to at least submit the completed piece as a separate PR, to get something rollin' 😄 thus 0 hurry :)

@@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="NEST" Version="7.11.1" />
<PackageReference Include="NEST.JsonNetSerializer" Version="7.11.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move Newtonsoft.Json to Serilog.Ui.Core project since it is referenced in most projects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied


namespace Serilog.Ui.Web.Authorization
{
internal interface IAuthorizationFilterService
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the interface to a separate file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

Func<HttpResponse, Task> onFailure = null)
{
var accessCheck = await CanAccess(httpContext, options);
if (!accessCheck)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert if condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted


namespace Serilog.Ui.Web.Endpoints
{
internal interface IAppStreamLoader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to a separate file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

internal class AppStreamLoader : IAppStreamLoader
{
private const string AppManifest = "Serilog.Ui.Web.wwwroot.dist.index.html";
public Stream GetIndex() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a break line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

internal class SerilogUiEndpoints : ISerilogUiEndpoints
{
private readonly ILogger<SerilogUiEndpoints> _logger;
private readonly JsonSerializerSettings _jsonSerializerOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it static.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied


var provider = httpContext.RequestServices.GetService<AggregateDataProvider>();

string key = dbKey;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the temp variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public static bool IsLocal(this HttpRequest request)
{
var ipAddress = request.Headers["X-forwarded-for"].FirstOrDefault();
if (!string.IsNullOrWhiteSpace(ipAddress))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add {} ->

if(..) 
{
    ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

@@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the below from csproj.

  <ItemGroup>
    <Folder Include="Models\" />
  </ItemGroup>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

var httpMethod = httpContext.Request.Method;
var isGet = httpMethod == "GET";

if (isGet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert if.

if (!isGet)
{
    return _staticFileMiddleware.Invoke(httpContext);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied



[Trait("Ui-Authorization", "Web")]
public class AuthorizationDefaultTest : IClassFixture<WebAppFactory.Default>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep all authorization tests in one class or move test classes to separate files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved filters utils to the Utilities folder, separated test classes in files

[Fact]
public async Task It_gets_app_home()
{
sut.SetOptions(new()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if you add // Arrange, // Act and // Assert comments to new tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be added now on entire ui.web :)

[Trait("Ui-Api-Routes", "Web")]
public class SerilogUiAppRoutesTest
{
private readonly IAppStreamLoader streamLoaderMock;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use _ for all private fields in test classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

[Fact]
public void It_is_local_when_remote_ip_address_equals_local_ip_address()
{
var mock = Substitute.For<HttpRequest>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: mockHttpRequest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

public void It_is_local_when_remote_ip_address_equals_local_ip_address()
{
var mock = Substitute.For<HttpRequest>();
var context = Substitute.For<HttpContext>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: mockHttpContext (and for the rest of test methods)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied

@followynne
Copy link
Member Author

Hello @mo-esmp

thanks for the deep review, I updated the code on all comments except one (where you'll find a question).

I'll leave to you each comment resolution, to be sure I didn't miss anything while doing them 😄

@mo-esmp
Copy link
Member

mo-esmp commented Sep 20, 2023

@followynne, as always, thanks for the contribution and improving stuff.

@followynne followynne merged commit 44fc5c7 into serilog-contrib:master Sep 20, 2023
3 checks passed
@followynne followynne deleted the feat/refactor-ui-web-backend branch September 20, 2023 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants