Path: redb-identity/tests/redb.Identity.Tests/Module/IdentityModuleConfigBinderTests.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/Module/IdentityModuleConfigBinderTests.cs ---- using System.Net; using AwesomeAssertions; using NSubstitute; using redb.Identity.Contracts.Configuration; using redb.Identity.Core.Configuration; using redb.Identity.Core.Module; using redb.Route.Abstractions; using redb.Route.Core; using Xunit; namespace redb.Identity.Tests.Module; /// /// Verifies that correctly hydrates /// from the nested IDictionary<string, object?> /// structure that Tsak deposits on the route context via SetProperty. /// public sealed class IdentityModuleConfigBinderTests { private static IRouteContext MakeContext(IDictionary? identitySection) { var sp = Substitute.For(); var ctx = new RouteContext(sp, "_test"); if (identitySection is not null) ctx.SetProperty(IdentityModuleConfigBinder.SectionName, identitySection); return ctx; } [Fact] public void Bind_ReturnsDefaults_WhenNoIdentitySection() { var opts = IdentityModuleConfigBinder.Bind(MakeContext(null)); opts.Should().NotBeNull(); opts.Issuer.Should().Be(new Uri("https://localhost/")); opts.RateLimit.Enabled.Should().BeFalse(); } [Fact] public void Bind_HydratesScalarsAndTimespans() { var section = new Dictionary { ["RedbInstanceName"] = "identity-pg", ["Issuer"] = "https://identity.example.com/", ["AllowEphemeralKeys"] = "true", ["AccessTokenLifetime"] = "00:30:00", ["RecoveryCodePbkdf2Iterations"] = "750000", ["Features"] = new Dictionary { ["EnableDeviceCodeFlow"] = "true", }, }; var opts = IdentityModuleConfigBinder.Bind(MakeContext(section)); opts.RedbInstanceName.Should().Be("identity-pg"); opts.Issuer.Should().Be(new Uri("https://identity.example.com/")); opts.AllowEphemeralKeys.Should().BeTrue(); opts.AccessTokenLifetime.Should().Be(TimeSpan.FromMinutes(30)); opts.RecoveryCodePbkdf2Iterations.Should().Be(750_000); opts.Features.EnableDeviceCodeFlow.Should().BeTrue(); } [Fact] public void Bind_HydratesNestedRateLimitOptions() { var section = new Dictionary { ["RateLimit"] = new Dictionary { ["Enabled"] = "true", ["PerIpPerMinute"] = "120", ["PerIpUsernameFailures"] = "3", ["PerIpUsernameWindow"] = "00:30:00", ["Backend"] = "redis", ["RedisConnectionString"] = "localhost:6379", ["RedisKeyPrefix"] = "myapp:rl:" } }; var opts = IdentityModuleConfigBinder.Bind(MakeContext(section)); opts.RateLimit.Enabled.Should().BeTrue(); opts.RateLimit.PerIpPerMinute.Should().Be(120); opts.RateLimit.PerIpUsernameFailures.Should().Be(3); opts.RateLimit.PerIpUsernameWindow.Should().Be(TimeSpan.FromMinutes(30)); opts.RateLimit.Backend.Should().Be("redis"); opts.RateLimit.RedisConnectionString.Should().Be("localhost:6379"); opts.RateLimit.RedisKeyPrefix.Should().Be("myapp:rl:"); } [Fact] public void Bind_HydratesStringArrays() { var section = new Dictionary { ["DynamicRegistrationAllowedScopes"] = new List { "openid", "profile", "custom_scope" } }; var opts = IdentityModuleConfigBinder.Bind(MakeContext(section)); opts.DynamicRegistrationAllowedScopes.Should().Equal("openid", "profile", "custom_scope"); } [Fact] public void Bind_HydratesFederationProvidersList() { var section = new Dictionary { ["Features"] = new Dictionary { ["EnableFederation"] = "true", }, ["FederationProviders"] = new List { new Dictionary { ["ProviderId"] = "google", ["DisplayName"] = "Google", ["Authority"] = "https://accounts.google.com", ["ClientId"] = "client-1", ["ClientSecret"] = "secret-1", ["Priority"] = "10" } } }; var opts = IdentityModuleConfigBinder.Bind(MakeContext(section)); opts.Features.EnableFederation.Should().BeTrue(); opts.FederationProviders.Should().HaveCount(1); opts.FederationProviders[0].ProviderId.Should().Be("google"); opts.FederationProviders[0].Authority.Should().Be("https://accounts.google.com"); opts.FederationProviders[0].Priority.Should().Be(10); } [Fact] public void Bind_ManuallyParsesIpAddressesAndNetworks() { var section = new Dictionary { ["ReverseProxies"] = new Dictionary { ["TrustForwardedFor"] = "true", ["KnownProxies"] = new List { "10.0.0.1", "203.0.113.7" }, ["KnownNetworks"] = new List { "10.0.0.0/8", "192.168.0.0/16" } } }; var opts = IdentityModuleConfigBinder.Bind(MakeContext(section)); opts.ReverseProxies.TrustForwardedFor.Should().BeTrue(); opts.ReverseProxies.KnownProxies.Should().HaveCount(2); opts.ReverseProxies.KnownProxies.Should().Contain(IPAddress.Parse("10.0.0.1")); opts.ReverseProxies.KnownNetworks.Should().HaveCount(2); opts.ReverseProxies.KnownNetworks[0].BaseAddress.Should().Be(IPAddress.Parse("10.0.0.0")); opts.ReverseProxies.KnownNetworks[0].PrefixLength.Should().Be(8); } // Identity:Audit in a Tsak worker: the module used to hand the route builder no audit options at // all, so configured targets were ignored and events reached only the relational sink. [Fact] public void BindAudit_ReturnsDefaults_WhenNoAuditSection() { var audit = IdentityModuleConfigBinder.BindAudit(MakeContext(new Dictionary { ["Issuer"] = "https://id.example/" })); audit.Enabled.Should().BeTrue(); audit.Filter.Should().Be("*"); audit.Targets.Should().BeEmpty(); } [Fact] public void BindAudit_HydratesTargetsAndSwitches() { var section = new Dictionary { ["Audit"] = new Dictionary { ["Filter"] = "TokenRevoked,UserDeleted", ["PersistToProps"] = "false", ["Targets"] = new List { new Dictionary { ["Name"] = "app", ["Enabled"] = "true", ["Uri"] = "direct-vm://app-identity-events" }, new Dictionary { ["Name"] = "log", ["Enabled"] = "false", ["Uri"] = "log:identity-audit" }, }, }, }; var audit = IdentityModuleConfigBinder.BindAudit(MakeContext(section)); audit.Filter.Should().Be("TokenRevoked,UserDeleted"); audit.PersistToProps.Should().BeFalse(); audit.Targets.Should().HaveCount(2); audit.Targets[0].Name.Should().Be("app"); audit.Targets[0].Enabled.Should().BeTrue(); audit.Targets[0].Uri.Should().Be("direct-vm://app-identity-events"); audit.Targets[1].Enabled.Should().BeFalse(); } [Fact] public void BindAudit_ReadsTargetsGivenAsIndexedKeys() { // An environment override (Identity__Audit__Targets__0__Uri) arrives as a dictionary keyed "0". var section = new Dictionary { ["Audit"] = new Dictionary { ["Targets"] = new Dictionary { ["0"] = new Dictionary { ["Name"] = "app", ["Enabled"] = "true", ["Uri"] = "direct-vm://app-identity-events" }, }, }, }; var audit = IdentityModuleConfigBinder.BindAudit(MakeContext(section)); audit.Targets.Should().ContainSingle() .Which.Uri.Should().Be("direct-vm://app-identity-events"); } }