using IdentityServer8.EntityFramework.DbContexts; using IdentityServer8.EntityFramework.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Yavsc.Org.Tests.Controllers; /// /// Regression sentinel: two /// instances must not see each other's clients. /// /// EF Core's UseInMemoryDatabase(name) returns the same /// backing store to every DbContext that asks for it under /// the same name, in the same process. Before the per-fixture GUID /// fix, both and /// used the bare "InMemory" /// connection string, so every fixture shared one store and tests /// were silently order-dependent. /// /// We assert against directly /// rather than via IClientStore: the validating wrapper around /// IClientStore raises events through IEventService, /// which is not registered in the test host and crashes with a /// NullReferenceException before it can return a result. Going /// straight to the DbContext is the same code path the production /// code uses, so it is the right surface to assert against. /// public class TestWebApplicationFactoryIsolationTests { [Fact] public async Task Second_factory_does_not_see_clients_seeded_into_first() { var marker = $"marker-A-{Guid.NewGuid():N}"; // First factory: seed a distinctive client. using (var first = new TestWebApplicationFactory()) { await using var scope = first.Services.CreateAsyncScope(); var configDb = scope.ServiceProvider.GetRequiredService(); var firstCs = scope.ServiceProvider.GetRequiredService() .GetConnectionString("YavscConnection"); configDb.Clients.Add(new Client { ClientId = marker, ClientName = "marker-A" }); await configDb.SaveChangesAsync(TestContext.Current.CancellationToken); // Sanity: the first factory can see its own seed. var seenByFirst = await configDb.Clients .AsNoTracking() .AnyAsync(c => c.ClientId == marker, TestContext.Current.CancellationToken); Assert.True(seenByFirst); } // Second factory: must start from a clean slate. If the // in-memory store leaked from the first factory, this // assertion fails. using var second = new TestWebApplicationFactory(); await using var secondScope = second.Services.CreateAsyncScope(); var secondCs = secondScope.ServiceProvider.GetRequiredService() .GetConnectionString("YavscConnection"); Assert.StartsWith("InMemory-", secondCs); var secondDb = secondScope.ServiceProvider.GetRequiredService(); var seenBySecond = await secondDb.Clients .AsNoTracking() .AnyAsync(c => c.ClientId == marker, TestContext.Current.CancellationToken); Assert.False(seenBySecond); } }