Path: redb-identity/tests/redb.Identity.Tests/Health/IdentityHealthContributorTests.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/Health/IdentityHealthContributorTests.cs ---- using AwesomeAssertions; using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using NSubstitute; using OpenIddict.Server; using redb.Core; using redb.Identity.Core.Health; using redb.Tsak.Contracts; using Xunit; namespace redb.Identity.Tests.Health; /// /// Unit tests for covering the three probes /// (database, signing-keys, data-protection) and the worst-status aggregation rule. /// public class IdentityHealthContributorTests { [Fact] public async Task AllProbesGreen_ReturnsHealthy() { var sp = BuildProvider(dbVersion: "PostgreSQL 16.2", signingKeys: 1, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Healthy); contributor.ModuleName.Should().Be("identity"); } [Fact] public async Task DatabaseUnreachable_ReturnsUnhealthy() { var redb = Substitute.For(); redb.GetDbVersionAsync(Arg.Any()) .Returns>(_ => throw new InvalidOperationException("DB down")); var sp = BuildProvider(redb: redb, signingKeys: 1, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Unhealthy); } [Fact] public async Task DatabaseEmptyVersion_ReturnsUnhealthy() { var sp = BuildProvider(dbVersion: "", signingKeys: 1, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Unhealthy); } [Fact] public async Task NoSigningKeys_ReturnsUnhealthy() { var sp = BuildProvider(dbVersion: "PostgreSQL 16.2", signingKeys: 0, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Unhealthy); } [Fact] public async Task NoDataProtectionKeys_ReturnsDegraded() { var sp = BuildProvider(dbVersion: "PostgreSQL 16.2", signingKeys: 1, dpKeys: 0); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Degraded); } [Fact] public async Task DatabaseAndSigningBoth_Unhealthy_ReturnsUnhealthy() { // Worst-status wins: even though data-protection is fine, db+signing failure dominates. var sp = BuildProvider(dbVersion: "", signingKeys: 0, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Unhealthy); } [Fact] public async Task SigningOptionsMissing_ReturnsUnhealthy() { // No IOptionsMonitor registered at all. var redb = Substitute.For(); redb.GetDbVersionAsync(Arg.Any()).Returns("v1"); var keyManager = Substitute.For(); keyManager.GetAllKeys().Returns(new List { Substitute.For() }); var services = new ServiceCollection(); services.AddScoped(_ => redb); services.AddSingleton(keyManager); var sp = services.BuildServiceProvider(); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); var status = await contributor.CheckHealthAsync(CancellationToken.None); status.Should().Be(HealthStatus.Unhealthy); } [Fact] public async Task Cancellation_PropagatesFromDbProbe() { var redb = Substitute.For(); redb.GetDbVersionAsync(Arg.Any()) .Returns>(_ => throw new OperationCanceledException()); var sp = BuildProvider(redb: redb, signingKeys: 1, dpKeys: 1); var contributor = new IdentityHealthContributor(sp, NullLogger.Instance); // OperationCanceledException is not caught — bubbles up so the probe respects cancellation. await Assert.ThrowsAsync( () => contributor.CheckHealthAsync(CancellationToken.None)); } // ----------------------------------------------------------------------- private static IServiceProvider BuildProvider( string? dbVersion = null, IRedbService? redb = null, int signingKeys = 0, int dpKeys = 0) { var services = new ServiceCollection(); if (redb is null) { redb = Substitute.For(); redb.GetDbVersionAsync(Arg.Any()).Returns(dbVersion); } services.AddScoped(_ => redb); var serverOptions = new OpenIddictServerOptions(); for (int i = 0; i < signingKeys; i++) { var bytes = new byte[64]; Random.Shared.NextBytes(bytes); serverOptions.SigningCredentials.Add(new SigningCredentials( new SymmetricSecurityKey(bytes), SecurityAlgorithms.HmacSha256)); } services.AddSingleton>( new StaticOptionsMonitor(serverOptions)); var keyManager = Substitute.For(); var keys = new List(); for (int i = 0; i < dpKeys; i++) keys.Add(Substitute.For()); keyManager.GetAllKeys().Returns(keys); services.AddSingleton(keyManager); return services.BuildServiceProvider(); } private sealed class StaticOptionsMonitor : IOptionsMonitor { public StaticOptionsMonitor(T value) => CurrentValue = value; public T CurrentValue { get; } public T Get(string? name) => CurrentValue; public IDisposable? OnChange(Action listener) => null; } }