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
{
{
public BypassSslValidationHandler()
{
// Override validation for this handler only
@ -97,5 +97,5 @@ namespace yavscTests
// Accept all certificates (bypass validation)
return true;
}
}
}
}

View file

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