Bifrost/tests/Bifrost.Web.Tests/Support/TestWebApplicationFactory.cs
2026-04-21 20:08:49 +01:00

57 lines
2.1 KiB
C#

using Bifrost.Web.Data;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Bifrost.Web.Tests.Support;
public sealed class TestWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.ConfigureAppConfiguration((_, configurationBuilder) =>
{
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string?>
{
["Bootstrap:AdminGitHubLogins:0"] = "test-admin",
["GitHubOAuth:ClientId"] = "configured-for-tests",
["GitHubOAuth:ClientSecret"] = "configured-for-tests"
});
});
builder.ConfigureServices(services =>
{
services.RemoveAll(typeof(DbContextOptions<BifrostDbContext>));
services.RemoveAll<BifrostDbContext>();
services.AddDbContext<BifrostDbContext>(options =>
{
options.UseInMemoryDatabase($"bifrost-tests-{Guid.NewGuid():N}");
});
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = TestAuthHandler.SchemeName;
options.DefaultScheme = TestAuthHandler.SchemeName;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
TestAuthHandler.SchemeName,
_ => { });
services.PostConfigure<CookieAuthenticationOptions>(
CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = "/auth/sign-in";
options.AccessDeniedPath = "/Membership/Status";
});
});
}
}