Path: redb-identity/tests/redb.Identity.Tests/DynamicRegistration/ClientRegistrationManagementProcessorTests.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/DynamicRegistration/ClientRegistrationManagementProcessorTests.cs ---- using System.Security.Cryptography; using System.Text; using AwesomeAssertions; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using OpenIddict.Abstractions; using redb.Core; using redb.Core.Models.Entities; using redb.Identity.Contracts.Registration; using redb.Identity.Core.Models; using redb.Identity.Contracts.Routes; using redb.Identity.Core.Routes.Processors; using redb.Identity.Tests.Infrastructure; using redb.Route.Abstractions; using Xunit; namespace redb.Identity.Tests.DynamicRegistration; /// /// Z2 (RFC 7592): Unit tests for . /// Exercises read/update/delete via operation header plus RFC 7592 §3 security: /// missing/wrong/non-DCR RAT → 401; unknown client → 404; RAT verified in fixed time; /// client_secret NEVER echoed back on read. /// public class ClientRegistrationManagementProcessorTests { private const string Token = "test-registration-access-token-abcdef1234567890"; private readonly IOpenIddictApplicationManager _manager = Substitute.For(); private readonly IRedbService _redb = Substitute.For(); private static string Sha256Hex(string s) { var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(s)); var sb = new StringBuilder(bytes.Length * 2); foreach (var b in bytes) sb.Append(b.ToString("x2")); return sb.ToString(); } private ClientRegistrationManagementProcessor CreateProcessor() { var services = new ServiceCollection(); services.AddSingleton(_manager); services.AddSingleton(_redb); var sp = services.BuildServiceProvider(); var ctx = Substitute.For(); ctx.GetServiceProvider().Returns(sp); // NSubstitute auto-mocks interface return types — pin GetFromRegistry to null so // GetIdentityService falls through to the host SP instead of an empty auto-mocked scope. ctx.GetFromRegistry(Arg.Any()).Returns((IServiceScopeFactory?)null); ctx.GetService().Returns(_redb); return new ClientRegistrationManagementProcessor(ctx); } private static TestExchange MakeExchange(string operation, string? clientId, string? bearer = Token) { var ex = new TestExchange(); ex.In.Headers["operation"] = operation; if (clientId is not null) ex.In.Headers["client_id"] = clientId; if (bearer is not null) ex.In.Headers["access_token"] = bearer; return ex; } private RedbObject ApplicationWithStoredHash( string clientId = "client-123", string? redirectUri = "https://app.example.com/cb", bool hasDcrHash = true) { var app = new RedbObject { name = "My App", Props = new ApplicationProps { ClientId = clientId, ClientType = "public", RedirectUris = redirectUri is null ? null : [redirectUri], ApplicationType = "web", RegistrationAccessTokenHash = hasDcrHash ? Sha256Hex(Token) : null } }; _manager.FindByClientIdAsync(clientId, Arg.Any()) .Returns(new ValueTask(app)); return app; } // ── Auth failures ── [Fact] public async Task Missing_ClientId_Returns_400() { var proc = CreateProcessor(); var ex = MakeExchange("read", clientId: null); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400); } [Fact] public async Task Missing_Bearer_Returns_401() { var proc = CreateProcessor(); var ex = MakeExchange("read", "client-123", bearer: null); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_token"); } [Fact] public async Task Unknown_Client_Returns_404() { _manager.FindByClientIdAsync(Arg.Any(), Arg.Any()) .Returns(new ValueTask((object?)null)); var proc = CreateProcessor(); var ex = MakeExchange("read", "does-not-exist"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(404); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_client_id"); } [Fact] public async Task Client_Without_Dcr_Hash_Returns_401() { ApplicationWithStoredHash(hasDcrHash: false); var proc = CreateProcessor(); var ex = MakeExchange("read", "client-123"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_token"); body["error_description"]!.ToString().Should().Contain("not eligible"); } [Fact] public async Task Wrong_Bearer_Returns_401() { ApplicationWithStoredHash(); var proc = CreateProcessor(); var ex = MakeExchange("read", "client-123", bearer: "not-the-real-token"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_token"); } [Fact] public async Task Authorization_Header_Accepted_As_Fallback() { ApplicationWithStoredHash(); var proc = CreateProcessor(); var ex = MakeExchange("read", "client-123", bearer: null); ex.In.Headers["Authorization"] = $"Bearer {Token}"; await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(200); } // ── Read ── [Fact] public async Task Read_Returns_Metadata_Without_Secrets() { var app = ApplicationWithStoredHash(); app.Props.ClientSecret = "super-secret-should-not-leak"; var proc = CreateProcessor(); var ex = MakeExchange("read", "client-123"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(200); var body = (IDictionary)ex.Out.Body!; body["client_id"]!.ToString().Should().Be("client-123"); body["client_name"]!.ToString().Should().Be("My App"); body.ContainsKey("client_secret").Should().BeFalse("RFC 7592 §2.3 — client_secret MUST NOT be returned on GET"); body.ContainsKey("registration_access_token").Should().BeFalse("RAT is one-shot at creation — never echoed"); var redirs = (System.Collections.Generic.IEnumerable)((System.Text.Json.JsonElement)body["redirect_uris"]!).EnumerateArray().Cast(); redirs.Select(o => o!.ToString()).Should().ContainSingle().Which.Should().Be("https://app.example.com/cb"); } [Theory] [InlineData("public", null, null, "none")] [InlineData("confidential", "hashed-secret", null, "client_secret_basic")] [InlineData("confidential", null, "https://keys.example.com/jwks", "private_key_jwt")] public async Task Read_Answers_The_Auth_Method_And_Keys_As_Stored(string clientType, string? secret, string? jwksUri, string method) { var app = ApplicationWithStoredHash(); app.Props.ClientType = clientType; app.Props.ClientSecret = secret; app.Props.JwksUri = jwksUri; app.Props.RequestObjectSigningAlg = "PS256"; var ex = MakeExchange("read", "client-123"); await CreateProcessor().Process(ex); var body = (IDictionary)ex.Out!.Body!; body["token_endpoint_auth_method"]!.ToString().Should().Be(method); body["request_object_signing_alg"]!.ToString().Should().Be("PS256"); if (jwksUri is null) body.ContainsKey("jwks_uri").Should().BeFalse(); else body["jwks_uri"]!.ToString().Should().Be(jwksUri); } // ── Update ── [Fact] public async Task Update_Persists_Changes_And_Returns_Updated_Metadata() { var app = ApplicationWithStoredHash(); RedbObject? savedApp = null; _redb.SaveAsync(Arg.Do>(r => savedApp = r)) .Returns(Task.FromResult(1L)); var proc = CreateProcessor(); var ex = MakeExchange("update", "client-123"); ex.In.Body = new DynamicRegistrationRequest { ClientName = "Renamed App", RedirectUris = ["https://new.example.com/cb"] }; await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(200); savedApp.Should().BeSameAs(app); savedApp!.name.Should().Be("Renamed App"); savedApp.Props.RedirectUris.Should().BeEquivalentTo(["https://new.example.com/cb"]); var body = (IDictionary)ex.Out.Body!; body["client_name"]!.ToString().Should().Be("Renamed App"); ((System.Text.Json.JsonElement)body["redirect_uris"]!).EnumerateArray() .Select(e => e.GetString()).Should().BeEquivalentTo(["https://new.example.com/cb"]); ex.Properties["identity-event-type"].Should().Be(IdentityAuditEventIds.ClientUpdated); } [Fact] public async Task Update_Accepts_Json_String_Body() { ApplicationWithStoredHash(); _redb.SaveAsync(Arg.Any>()).Returns(Task.FromResult(1L)); var proc = CreateProcessor(); var ex = MakeExchange("update", "client-123"); ex.In.Body = """{"client_name":"From Json","redirect_uris":["https://json.example.com/cb"]}"""; await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(200); var body = (IDictionary)ex.Out.Body!; body["client_name"]!.ToString().Should().Be("From Json"); } [Fact] public async Task Update_With_Invalid_Body_Returns_400() { ApplicationWithStoredHash(); var proc = CreateProcessor(); var ex = MakeExchange("update", "client-123"); ex.In.Body = 42; // unsupported type await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_client_metadata"); } // ── Delete ── [Fact] public async Task Delete_Calls_Manager_DeleteAsync_And_Returns_204() { var app = ApplicationWithStoredHash(); var proc = CreateProcessor(); var ex = MakeExchange("delete", "client-123"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(204); await _manager.Received(1).DeleteAsync(app, Arg.Any()); ex.Properties["identity-event-type"].Should().Be(IdentityAuditEventIds.ClientDeleted); } // ── Unknown operation ── [Fact] public async Task Unknown_Operation_Returns_400() { ApplicationWithStoredHash(); var proc = CreateProcessor(); var ex = MakeExchange("explode", "client-123"); await proc.Process(ex); ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400); var body = (IDictionary)ex.Out.Body!; body["error"]!.ToString().Should().Be("invalid_request"); } }