fixe tests

This commit is contained in:
Paul Schneider 2026-09-12 16:49:36 +01:00
commit 128735a237
6 changed files with 95 additions and 36 deletions

View file

@ -10,10 +10,10 @@ namespace Yavsc.Org.Tests
{
[Collection("Yavsc Server")]
[Trait("regression", "oui")]
public class BaseTestContext : IClassFixture<WebServerFixture>, IDisposable
public abstract class BaseTestContext : IClassFixture<WebServerFixture>, IDisposable
{
public readonly WebServerFixture _serverFixture;
private readonly ITestOutputHelper _output;
protected readonly WebServerFixture _serverFixture;
protected readonly ITestOutputHelper _output;
public BaseTestContext(ITestOutputHelper output, WebServerFixture fixture)
{

View file

@ -84,9 +84,11 @@ namespace Yavsc.Org.Tests
[Fact]
public async Task GetOpenIdConfiguration_returns_ok()
{
using var client = _serverFixture.CreateHttpClient();
using var client = CreateHttpClient();
var response = await GetRaw(client, "/.well-known/openid-configuration");
var payload = await response.Content.ReadAsStringAsync();
var payload = await response.Content.ReadAsStringAsync(
TestContext.Current.CancellationToken
);
Assert.True(
response.IsSuccessStatusCode,

View file

@ -1,14 +1,8 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Yavsc.Models;
namespace Yavsc.Org.Tests.Mandatory
{[Collection("Database")]
[Trait("regression", "II")]
[Trait("dev", "wip")]
public class Database: IClassFixture<WebServerFixture>, IDisposable
public class Database : IClassFixture<WebServerFixture>
{
readonly ITestOutputHelper output;
readonly WebServerFixture _serverFixture;
@ -24,15 +18,6 @@ namespace Yavsc.Org.Tests.Mandatory
/// Install all our migrations in a fresh new database.
/// </summary>
public void Dispose()
{
if (_serverFixture!=null)
{
_serverFixture.Dispose();
}
}
[Fact]
public void TestDatabaseMigration()
{

View file

@ -14,11 +14,13 @@ namespace Yavsc.Org.Tests.Smoke;
/// <c>doc/architecture/decoupage-organisation.md</c>. The smoke
/// here asserts the front-end side of the BC.
/// </summary>
public class BlogSmokeTests : SmokeTestBase, IClassFixture<TestWebApplicationFactory>
public class BlogSmokeTests : BaseTestContext, IClassFixture<TestWebApplicationFactory>
{
private readonly TestWebApplicationFactory _factory;
public BlogSmokeTests(TestWebApplicationFactory factory)
public BlogSmokeTests(TestWebApplicationFactory factory, ITestOutputHelper output,
WebServerFixture webServerFixture)
: base(output, webServerFixture)
{
_factory = factory;
}

View file

@ -63,6 +63,7 @@ public sealed class WebServerFixture : WebHostFixture
private static string? _sharedTestingUserName;
private static string? _sharedTestingUserPassword;
private static string? _sharedTestingUserEmail;
private static string? _sharedHttpsAuthority;
private static RecordingSmtpClientFactory? _sharedSmtpClientFactory;
public IConfiguration? Configuration { get; private set; }
@ -78,10 +79,19 @@ public sealed class WebServerFixture : WebHostFixture
public RecordingSmtpClientFactory? SmtpClientFactory { get; private set; }
public ILogger? Logger { get; internal set; }
public string? HttpsAuthority => Addresses.FirstOrDefault(u => u.StartsWith("https:"));
public string? HttpsAuthority { get; private set; }
protected override WebApplicationOptions CreateBuilderOptions()
{
return new WebApplicationOptions
{
ApplicationName = typeof(Yavsc.Program).Assembly.GetName().Name
};
}
protected override WebApplication BuildApp(WebApplicationBuilder builder)
{
var authority = $"https://localhost:{_httpsPort}";
HttpsAuthority = $"https://localhost:{HttpsPort}";
// WebApplication.CreateBuilder defaults WebRootPath to
// {ContentRoot}/wwwroot. The test assembly runs from
@ -100,7 +110,7 @@ public sealed class WebServerFixture : WebHostFixture
["Smtp:Port"] = "465",
["Smtp:UserName"] = "test-user",
["Smtp:Password"] = "test-pass",
["Site:Authority"] = authority
["Site:Authority"] = HttpsAuthority
});
Configuration = builder.Configuration;
@ -183,6 +193,7 @@ public sealed class WebServerFixture : WebHostFixture
_sharedTestingUserName = TestingUserName;
_sharedTestingUserPassword = TestingUserPassword;
_sharedTestingUserEmail = TestingUserEmail;
_sharedHttpsAuthority = HttpsAuthority;
_sharedLogger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger<WebServerFixture>();
Logger = _sharedLogger;
SmtpClientFactory = smtpFactory;
@ -195,13 +206,42 @@ public sealed class WebServerFixture : WebHostFixture
using var scope = Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
if (db.Database.IsRelational())
{
db.Database.Migrate();
ReseedAuthTestData(scope);
return;
}
ReseedAuthTestData(scope);
}
db.Database.EnsureCreated();
private void ReseedAuthTestData(IServiceScope scope)
{
TestingUserName ??= "Tester";
TestingUserPassword ??= "Test123!";
TestingUserEmail ??= "test@no-reply.com";
TestClientId ??= "testClientId";
TestClientSecret ??= Guid.CreateVersion7().ToString();
TestingUser = null;
EnsureUser(TestingUserName, TestingUserPassword, TestingUserEmail, scope);
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
TestingUser = db.Users.FirstOrDefault(u => u.UserName == TestingUserName);
var configDb = scope.ServiceProvider.GetRequiredService<IdentityServer8.EntityFramework.DbContexts.ConfigurationDbContext>();
var hasClient = configDb.Set<Client>().Any(c => c.ClientId == TestClientId);
if (!hasClient)
{
AddAuthorizedClient(scope, TestClientId, TestClientSecret);
}
_sharedTestClientId = TestClientId;
_sharedTestClientSecret = TestClientSecret;
_sharedTestingUserName = TestingUserName;
_sharedTestingUserPassword = TestingUserPassword;
_sharedTestingUserEmail = TestingUserEmail;
}
protected override async Task<WebApplication> ConfigurePipelineAsync(WebApplication app)
@ -226,6 +266,7 @@ public sealed class WebServerFixture : WebHostFixture
TestingUserName = _sharedTestingUserName;
TestingUserPassword = _sharedTestingUserPassword;
TestingUserEmail = _sharedTestingUserEmail;
HttpsAuthority = _sharedHttpsAuthority;
SmtpClientFactory = _sharedSmtpClientFactory;
Configuration = _sharedConfiguration;
SiteSettings = _sharedSiteSettings;