Path: redb-identity/tests/redb.Identity.Tests/Infrastructure/IdentityRouteFixture.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Infrastructure/IdentityRouteFixture.cs
----
using System.Security.Claims;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSubstitute;
using OpenIddict.Abstractions;
using OpenIddict.Server;
using redb.Core;
using redb.Core.Providers;
using redb.Identity.Core;
using redb.Identity.Core.Configuration;
using redb.Identity.Core.OpenIddict;
using redb.Identity.Contracts.Routes;
using redb.Route.Abstractions;
using redb.Route.Components;
using redb.Route.Core;
using Xunit;
using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Server.OpenIddictServerEvents;
using redb.Identity.Core.Routes;
namespace redb.Identity.Tests.Infrastructure;
///
/// Shared fixture that boots a full RouteContext with all 13 Identity routes.
/// Uses OpenIddict degraded mode + mocked IRedbService.
/// Tests send messages via ProducerTemplate through direct-vm:// — exercises the complete pipeline:
/// error handling → throttle → processor → WireTap → event dispatch.
///
public sealed class IdentityRouteFixture : IAsyncLifetime
{
private ServiceProvider _sp = null!;
private RouteContext _ctx = null!;
public ProducerTemplate Producer { get; private set; } = null!;
public IRedbService Redb { get; private set; } = null!;
public IServiceProvider ServiceProvider => _sp;
/// The From URIs of every route the fixture registered — lets a test check coverage instead of guessing.
public IEnumerable RegisteredFromUris => _ctx.Routes.Select(r => r.FromUri);
public async Task InitializeAsync()
{
var services = new ServiceCollection();
// Logging
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
// Mocked IRedbService
Redb = Substitute.For();
var userProvider = Substitute.For();
Redb.UserProvider.Returns(userProvider);
services.AddSingleton(Redb);
// SharedVmRegistry (required for direct-vm)
services.AddSingleton();
// DataProtection + MFA protectors — required by IdentityCoreRouteBuilder.Configure()
// even when no MFA tests run.
services.AddDataProtection().UseEphemeralDataProtectionProvider();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// Identity options
services.AddSingleton(Options.Create(new RedbIdentityOptions
{
TokenRetentionDays = 30,
TokenThrottleMaxPerPeriod = 100, // relaxed for tests
TokenThrottlePeriod = TimeSpan.FromSeconds(1)
}));
// OpenIddict in degraded mode
services.AddOpenIddict()
.AddCore(core => core.UseRedbStores())
.AddServer(options =>
{
options.EnableDegradedMode();
options.SetIssuer(new Uri("https://identity.test.local/"));
options.SetTokenEndpointUris("/connect/token");
options.SetAuthorizationEndpointUris("/connect/authorize");
options.SetIntrospectionEndpointUris("/connect/introspect");
options.SetRevocationEndpointUris("/connect/revoke");
options.SetConfigurationEndpointUris("/.well-known/openid-configuration");
options.AllowClientCredentialsFlow();
options.AllowAuthorizationCodeFlow();
options.AllowRefreshTokenFlow();
options.AddEphemeralEncryptionKey();
options.AddEphemeralSigningKey();
options.DisableAccessTokenEncryption();
options.UseRedbRoute();
// Degraded mode: skip validation for all enabled endpoints
options.AddEventHandler(builder =>
builder
.UseInlineHandler(context => default)
.SetOrder(int.MaxValue - 100_000)
.Build());
options.AddEventHandler(builder =>
builder
.UseInlineHandler(context => default)
.SetOrder(int.MaxValue - 100_000)
.Build());
options.AddEventHandler(builder =>
builder
.UseInlineHandler(context => default)
.SetOrder(int.MaxValue - 100_000)
.Build());
options.AddEventHandler(builder =>
builder
.UseInlineHandler(context => default)
.SetOrder(int.MaxValue - 100_000)
.Build());
// Provide ClaimsPrincipal for client_credentials
options.AddEventHandler(builder =>
builder
.UseInlineHandler(context =>
{
if (!context.Request.IsClientCredentialsGrantType())
return default;
var identity = new ClaimsIdentity(
authenticationType: "OpenIddict.Server",
nameType: Claims.Name,
roleType: Claims.Role);
identity.SetClaim(Claims.Subject, context.Request.ClientId ?? "test-sub");
identity.SetScopes(context.Request.GetScopes());
context.Principal = new ClaimsPrincipal(identity);
return default;
})
.SetOrder(OpenIddictServerHandlers.Exchange.AttachPrincipal
.Descriptor.Order + 100)
.Build());
});
_sp = services.BuildServiceProvider();
// Create RouteContext with full DI
_ctx = new RouteContext(_sp, "identity-integration");
// Register all 13 identity routes
_ctx.AddRoutes(new IdentityCoreRouteBuilder(_sp, Options.Create(new RedbIdentityOptions
{
TokenThrottleMaxPerPeriod = 100,
TokenThrottlePeriod = TimeSpan.FromSeconds(1),
TokenRetentionDays = 30
})));
await _ctx.Start();
Producer = new ProducerTemplate(_ctx);
Producer.Start();
}
public async Task DisposeAsync()
{
Producer.Stop();
Producer.Dispose();
await _ctx.DisposeAsync();
await _sp.DisposeAsync();
}
///
/// Sends a request through the route pipeline with InOut pattern (returns Out.Body).
///
public async Task