Path: redb-identity/tests/redb.Identity.Tests/Management/ResultContractsJsonTests.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/Management/ResultContractsJsonTests.cs
----
using System.Text.Encodings.Web;
using System.Text.Json;
using AwesomeAssertions;
using redb.Identity.Contracts.Common;
using redb.Identity.Contracts.Sessions;
using redb.Identity.Contracts.Tokens;
using Xunit;
namespace redb.Identity.Tests.Management;
///
/// The result types of the management and cleanup routes write the JSON those routes wrote when their bodies
/// were anonymous objects, under every option set a controller dispatcher serializes with: the HTTP dispatcher
/// names members camelCase, the gRPC one writes names as declared, so the names are pinned on the types and an
/// absent optional member stays absent. The wire tests (ManagementResultWireTests, GrpcManagementResultWireTests)
/// prove the routes a caller reaches; these cover every type, including the session cleanup that only a timer runs.
///
public class ResultContractsJsonTests
{
// The option sets of redb.Route.Controllers: HttpControllerDispatcher's default and GrpcControllerDispatcher's.
private static readonly JsonSerializerOptions HttpDispatcher = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
private static readonly JsonSerializerOptions GrpcDispatcher = new() { PropertyNameCaseInsensitive = true };
public static TheoryData OptionSets => new() { "http", "grpc", "default" };
private static string Write(object value, string options) => options switch
{
"http" => JsonSerializer.Serialize(value, HttpDispatcher),
"grpc" => JsonSerializer.Serialize(value, GrpcDispatcher),
_ => JsonSerializer.Serialize(value),
};
[Theory, MemberData(nameof(OptionSets))]
public void The_error_document(string options) =>
Write(new ErrorResponse { Error = "invalid_operation", ErrorDescription = "Unknown operation: nuke" }, options)
.Should().Be("""{"error":"invalid_operation","error_description":"Unknown operation: nuke"}""");
[Theory, MemberData(nameof(OptionSets))]
public void Success_alone(string options) =>
Write(new SuccessResponse(), options).Should().Be("""{"success":true}""");
[Theory, MemberData(nameof(OptionSets))]
public void Success_with_the_sessions_revoked(string options) =>
Write(new SuccessResponse { SessionsRevoked = 2 }, options).Should().Be("""{"success":true,"sessionsRevoked":2}""");
[Theory, MemberData(nameof(OptionSets))]
public void Success_with_the_consents_revoked(string options) =>
Write(new SuccessResponse { Revoked = 1 }, options).Should().Be("""{"success":true,"revoked":1}""");
[Theory, MemberData(nameof(OptionSets))]
public void The_revoked_count(string options) =>
Write(new RevokedCountResponse { RevokedCount = 3 }, options).Should().Be("""{"revokedCount":3}""");
[Theory, MemberData(nameof(OptionSets))]
public void The_token_prune(string options) =>
Write(new TokenPruneResult { PrunedTokens = 5, PrunedAuthorizations = 2 }, options)
.Should().Be("""{"prunedTokens":5,"prunedAuthorizations":2}""");
[Theory, MemberData(nameof(OptionSets))]
public void The_token_prune_preview(string options) =>
Write(new TokenPrunePreview
{
WouldPruneTokens = 5,
WouldPruneAuthorizations = 2,
Threshold = new DateTimeOffset(2026, 11, 10, 8, 30, 0, TimeSpan.Zero),
}, options).Should().Be(
"""{"dryRun":true,"wouldPruneTokens":5,"wouldPruneAuthorizations":2,"threshold":"2026-11-10T08:30:00+00:00"}""");
[Theory, MemberData(nameof(OptionSets))]
public void The_session_prune(string options) =>
Write(new SessionPruneResult { PrunedSessions = 4, ExpiredActiveSessions = 1 }, options)
.Should().Be("""{"prunedSessions":4,"expiredActiveSessions":1}""");
}