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