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 { protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Testing"); builder.ConfigureAppConfiguration((_, configurationBuilder) => { configurationBuilder.AddInMemoryCollection(new Dictionary { ["Bootstrap:AdminGitHubLogins:0"] = "test-admin", ["GitHubOAuth:ClientId"] = "configured-for-tests", ["GitHubOAuth:ClientSecret"] = "configured-for-tests" }); }); builder.ConfigureServices(services => { services.RemoveAll(typeof(DbContextOptions)); services.RemoveAll(); services.AddDbContext(options => { options.UseInMemoryDatabase($"bifrost-tests-{Guid.NewGuid():N}"); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = TestAuthHandler.SchemeName; options.DefaultScheme = TestAuthHandler.SchemeName; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; }).AddScheme( TestAuthHandler.SchemeName, _ => { }); services.PostConfigure( CookieAuthenticationDefaults.AuthenticationScheme, options => { options.LoginPath = "/auth/sign-in"; options.AccessDeniedPath = "/Membership/Status"; }); }); } }