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

chore(auth-tests): create application factory util, rename files #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Ui.Web.Tests.Utilities;
using Xunit;

namespace Serilog.Ui.Web.Tests.Authorization;

public class AuthorizationFilterDefaultTest : IClassFixture<WebApplicationFactory<WebSampleProgram>>
{
private readonly HttpClient _client;

public AuthorizationFilterDefaultTest(WebApplicationFactory<WebSampleProgram> factory)
{
_client = factory.CreateClient();
}

[Fact]
public async Task Local_Requests_Are_Allowed_By_Default()
{
// Act
var response = await _client.GetAsync("/serilog-ui/index.html");

// Assert
response.EnsureSuccessStatusCode();
}
}

public class AuthorizationFilterWithUserImplementationTest : IClassFixture<WebSampleProgramWithForbiddenLocalRequest>
{
private readonly HttpClient _client;

public AuthorizationFilterWithUserImplementationTest(WebSampleProgramWithForbiddenLocalRequest factory)
{
_client = factory.CreateClient();
}

[Fact]
public async Task Local_Requests_Are_Not_Allowed()
{
// Act
var response = await _client.GetAsync("/serilog-ui/index.html");

// Assert
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}
}

This file was deleted.

29 changes: 0 additions & 29 deletions tests/Serilog.Ui.Web.Tests/Authorization/Program1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class SerilogUiOptionBuilderExtensions
/// Configures the SerilogUi to connect to a InMemory sink.
/// </summary>
/// <param name="optionsBuilder">The options builder.</param>
public static void UseImMemory(this SerilogUiOptionsBuilder optionsBuilder)
public static void UseInMemory(this SerilogUiOptionsBuilder optionsBuilder)
{
((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, SerilogInMemoryDataProvider>();
}
Expand Down
54 changes: 54 additions & 0 deletions tests/Serilog.Ui.Web.Tests/Utilities/WebSampleProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Sinks.InMemory;
using Serilog.Ui.Web;
using Serilog.Ui.Web.Tests.Authorization;
using Serilog.Ui.Web.Tests.SerilogInMemoryDataProvider;
using System.IO;

namespace Ui.Web.Tests.Utilities;

public class WebSampleProgram
{
internal static void Main()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
WebRootPath = Directory.GetCurrentDirectory()
});

builder.Services.AddEndpointsApiExplorer();

Log.Logger = new LoggerConfiguration().WriteTo.InMemory().CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddSerilogUi(options => options.UseInMemory());

var app = builder.Build();
app.UseSerilogUi();

app.Run();
}
}

public class WebSampleProgramDefaultFactory : WebApplicationFactory<WebSampleProgram> { }

public class WebSampleProgramWithForbiddenLocalRequest : WebApplicationFactory<WebSampleProgram>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.Configure(appBuilder =>
{
appBuilder.UseSerilogUi(options =>
{
options.Authorization.AuthenticationType = AuthenticationType.Jwt;
options.Authorization.Filters = new[]
{
new ForbidLocalRequestFilter()
};
});
});
}
}