a web server fixture shared state

This commit is contained in:
Paul Schneider 2026-04-19 17:15:10 +01:00
commit 5b6caa72b5
2 changed files with 140 additions and 111 deletions

View file

@ -81,7 +81,7 @@ namespace yavscTests
} }
internal class BypassSslValidationHandler : HttpClientHandler internal class BypassSslValidationHandler : HttpClientHandler
{ {
public BypassSslValidationHandler() public BypassSslValidationHandler()
{ {
// Override validation for this handler only // Override validation for this handler only
@ -97,5 +97,5 @@ namespace yavscTests
// Accept all certificates (bypass validation) // Accept all certificates (bypass validation)
return true; return true;
} }
} }
} }

View file

@ -29,9 +29,20 @@ namespace isnd.tests
public class WebServerFixture : IDisposable public class WebServerFixture : IDisposable
{ {
private static readonly Lazy<X509Certificate2> _selfSignedCertificate = new Lazy<X509Certificate2>(CreateSelfSignedCertificate); private static readonly Lazy<X509Certificate2> _selfSignedCertificate = new Lazy<X509Certificate2>(CreateSelfSignedCertificate);
private static readonly object _sync = new object();
private static WebApplication? _app; private static WebApplication? _app;
private static bool _isInitialized = false; private static bool _isInitialized = false;
private static int _instanceCount = 0; private static int _instanceCount = 0;
private static readonly List<string> _sharedAddresses = new List<string>();
private static string? _sharedTestClientId;
private static string? _sharedTestClientSecret;
private static string? _sharedTestingUserName;
private static string? _sharedTestingUserPassword;
private static string? _sharedTestingUserEmail;
private static IServiceProvider? _sharedServices;
private static IConfiguration? _sharedConfiguration;
private static SiteSettings? _sharedSiteSettings;
private static Microsoft.Extensions.Logging.ILogger? _sharedLogger;
public List<string> Addresses { get; private set; } = new List<string>(); public List<string> Addresses { get; private set; } = new List<string>();
public Microsoft.Extensions.Logging.ILogger? Logger { get; internal set; } public Microsoft.Extensions.Logging.ILogger? Logger { get; internal set; }
@ -40,7 +51,6 @@ namespace isnd.tests
public IConfiguration? Configuration { get; private set; } public IConfiguration? Configuration { get; private set; }
private WebApplication? app;
public string? TestClientId { get; private set; } public string? TestClientId { get; private set; }
public IServiceProvider? Services { get; private set; } public IServiceProvider? Services { get; private set; }
@ -52,10 +62,10 @@ namespace isnd.tests
public bool DbCreated { get; internal set; } public bool DbCreated { get; internal set; }
public SiteSettings? SiteSettings { get => siteSettings; set => siteSettings = value; } public SiteSettings? SiteSettings { get => siteSettings; set => siteSettings = value; }
public string? TestClientSecret { get; set; } public string? TestClientSecret { get; set; }
public string? TestingUserEmail { get; set; }
public WebServerFixture() public WebServerFixture()
{ {
lock (this) lock (_sync)
{ {
_instanceCount++; _instanceCount++;
if (!_isInitialized) if (!_isInitialized)
@ -63,25 +73,14 @@ namespace isnd.tests
SetupHost().Wait(); SetupHost().Wait();
_isInitialized = true; _isInitialized = true;
} }
else
{ CopySharedState();
// Get addresses from existing app
var server = _app!.Services.GetRequiredService<IServer>();
var addressFeatures = server.Features.Get<IServerAddressesFeature>();
if (addressFeatures?.Addresses != null)
{
foreach (var address in addressFeatures.Addresses)
{
Addresses.Add(address);
}
}
}
} }
} }
public void Dispose() public void Dispose()
{ {
lock (this) lock (_sync)
{ {
_instanceCount--; _instanceCount--;
if (_instanceCount == 0 && _app != null) if (_instanceCount == 0 && _app != null)
@ -89,9 +88,33 @@ namespace isnd.tests
_app.StopAsync().Wait(); _app.StopAsync().Wait();
_app = null; _app = null;
_isInitialized = false; _isInitialized = false;
_sharedAddresses.Clear();
_sharedServices = null;
_sharedConfiguration = null;
_sharedSiteSettings = null;
_sharedLogger = null;
_sharedTestClientId = null;
_sharedTestClientSecret = null;
_sharedTestingUserName = null;
_sharedTestingUserPassword = null;
_sharedTestingUserEmail = null;
} }
} }
} }
private void CopySharedState()
{
Addresses = new List<string>(_sharedAddresses);
Logger = _sharedLogger;
Configuration = _sharedConfiguration;
Services = _sharedServices;
SiteSettings = _sharedSiteSettings;
TestClientId = _sharedTestClientId;
TestClientSecret = _sharedTestClientSecret;
TestingUserName = _sharedTestingUserName;
TestingUserPassword = _sharedTestingUserPassword;
TestingUserEmail = _sharedTestingUserEmail;
}
void ConfigureLogger() => Log.Logger = new LoggerConfiguration() void ConfigureLogger() => Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug() .MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
@ -128,10 +151,10 @@ namespace isnd.tests
["Smtp:SenderEmail"] = "test@example.com" ["Smtp:SenderEmail"] = "test@example.com"
}); });
// Configure Kestrel for HTTPS with self-signed certificate // Configure Kestrel for HTTPS with self-signed certificate on a dynamic port
builder.WebHost.ConfigureKestrel(options => builder.WebHost.ConfigureKestrel(options =>
{ {
options.Listen(IPAddress.Loopback, 5001, listenOptions => options.Listen(IPAddress.Loopback, 0, listenOptions =>
{ {
listenOptions.UseHttps(_selfSignedCertificate.Value); listenOptions.UseHttps(_selfSignedCertificate.Value);
}); });
@ -151,37 +174,45 @@ namespace isnd.tests
TestingUserName = "Tester"; TestingUserName = "Tester";
TestingUserPassword = "tesT456+*"; TestingUserPassword = "tesT456+*";
TestClientId = "testClientId"; TestClientId = "testClientId";
TestingUserEmail = "test@no-reply.com";
TestingUser = null;
TestClientSecret = Guid.CreateVersion7().ToString(); TestClientSecret = Guid.CreateVersion7().ToString();
EnsureUser(TestingUserName, TestingUserPassword); EnsureUser(TestingUserName, TestingUserPassword, TestingUserEmail, migrationScope);
AddAuthorizedClient(TestClientId, TestClientSecret); AddAuthorizedClient(migrationScope, TestClientId, TestClientSecret);
TestingUser = await db.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName); TestingUser = await db.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName);
} }
await _app!.ConfigurePipeline(); await _app!.ConfigurePipeline();
_app.UseSession(); _app.UseSession();
await _app.StartAsync(); await _app.StartAsync();
_sharedServices = _app.Services;
_sharedConfiguration = Configuration;
_sharedSiteSettings = SiteSettings;
_sharedTestClientId = TestClientId;
_sharedTestClientSecret = TestClientSecret;
_sharedTestingUserName = TestingUserName;
_sharedTestingUserPassword = TestingUserPassword;
_sharedTestingUserEmail = TestingUserEmail;
_sharedLogger = _app.Services.GetRequiredService<ILoggerFactory>().CreateLogger<WebServerFixture>();
Logger = _sharedLogger;
var server = _app.Services.GetRequiredService<IServer>();
var logFactory = app.Services.GetRequiredService<ILoggerFactory>();
Logger = logFactory.CreateLogger<WebServerFixture>();
var server = app.Services.GetRequiredService<IServer>();
var addressFeatures = server.Features.Get<IServerAddressesFeature>(); var addressFeatures = server.Features.Get<IServerAddressesFeature>();
if (addressFeatures?.Addresses != null) if (addressFeatures?.Addresses != null)
{ {
_sharedAddresses.Clear();
foreach (var address in addressFeatures.Addresses) foreach (var address in addressFeatures.Addresses)
{ {
_sharedAddresses.Add(address);
Addresses.Add(address); Addresses.Add(address);
} }
} }
} }
private void AddAuthorizedClient(string testClientId, string testClientSecret) private void AddAuthorizedClient(IServiceScope scope, string testClientId, string testClientSecret)
{
using (IServiceScope scope = app!.Services.CreateScope())
{ {
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Client testingClient = new Client Client testingClient = new Client
{ {
@ -242,13 +273,11 @@ namespace isnd.tests
db.SaveChanges(); db.SaveChanges();
} }
}
public void EnsureUser(string testingUserName, string password) public void EnsureUser(string testingUserName, string password, string email, IServiceScope scope)
{ {
if (TestingUser == null) if (TestingUser == null)
{ {
using IServiceScope scope = app!.Services.CreateScope();
var userManager = var userManager =
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>(); scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();