Path: redb-identity/tests/redb.Identity.Tests/Management/TokenManagementTests.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/TokenManagementTests.cs ---- using AwesomeAssertions; using NSubstitute; using redb.Core; using redb.Core.Models.Entities; using redb.Identity.Contracts.Common; using redb.Identity.Contracts.Tokens; using redb.Identity.Core.Models; using redb.Identity.Core.Routes.Processors; using redb.Identity.Tests.Infrastructure; using redb.Route.Abstractions; using Xunit; using static OpenIddict.Abstractions.OpenIddictConstants; namespace redb.Identity.Tests.Management; public class TokenManagementTests { private readonly IRedbService _redb = Substitute.For(); private readonly TokenManagementProcessor _processor; public TokenManagementTests() { var context = MockRouteContext.Create(_redb); _processor = new TokenManagementProcessor(context); } private static TestExchange CreateExchange(string operation, object? body = null) { var exchange = new TestExchange(); exchange.In.Headers["operation"] = operation; if (body != null) exchange.In.Body = body; return exchange; } // ── List ── [Fact] public async Task List_NoFilters_ReturnsAllTokens() { var items = new List> { MockRedbQuery.CreateObject(1, "token1", new TokenProps { ApplicationObjectId = 10, Status = Statuses.Valid, Type = "access_token" }), MockRedbQuery.CreateObject(2, "token2", new TokenProps { ApplicationObjectId = 10, Status = Statuses.Revoked, Type = "refresh_token" }) }; MockRedbQuery.Setup(_redb, items); var exchange = CreateExchange("list"); await _processor.Process(exchange); var result = exchange.Out!.Body.Should().BeOfType>().Subject; result.Total.Should().Be(2); result.Items.Should().HaveCount(2); } [Fact] public async Task List_FilterByStatus_ReturnsFiltered() { var items = new List> { MockRedbQuery.CreateObject(1, "t1", new TokenProps { Status = Statuses.Valid, Type = "access_token" }), MockRedbQuery.CreateObject(2, "t2", new TokenProps { Status = Statuses.Revoked, Type = "access_token" }), MockRedbQuery.CreateObject(3, "t3", new TokenProps { Status = Statuses.Valid, Type = "refresh_token" }) }; MockRedbQuery.Setup(_redb, items); var exchange = CreateExchange("list", new Dictionary { ["status"] = Statuses.Valid }); await _processor.Process(exchange); var result = exchange.Out!.Body.Should().BeOfType>().Subject; result.Total.Should().Be(2); result.Items.Should().OnlyContain(t => t.Status == Statuses.Valid); } // ── Revoke ── [Fact] public async Task Revoke_ValidToken_SetsRevokedStatus() { var token = MockRedbQuery.CreateObject(42, "tok", new TokenProps { Status = Statuses.Valid, Type = "access_token" }); _redb.LoadAsync(42).Returns(token); _redb.SaveAsync(Arg.Any>()).Returns(42L); var exchange = CreateExchange("revoke", new Dictionary { ["tokenId"] = 42L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Success.Should().BeTrue(); token.Props.Status.Should().Be(Statuses.Revoked); exchange.Properties["identity-event-type"].Should().Be("TokenRevoked"); } [Fact] public async Task Revoke_UserToken_EventNamesTheUser() { // The token's subject column is the public sub; the event carries the user's internal id, // the key of their UserProps, as TokensRevokedByUser does. var subject = Guid.NewGuid(); var token = MockRedbQuery.CreateObject(43, "tok", new TokenProps { Status = Statuses.Valid, Type = "access_token" }); token.value_guid = subject; _redb.LoadAsync(43).Returns(token); _redb.SaveAsync(Arg.Any>()).Returns(43L); var user = MockRedbQuery.CreateObject(900, "user", new UserProps()); user.value_guid = subject; user.key = 7; MockRedbQuery.Setup(_redb, new List> { user }); var exchange = CreateExchange("revoke", new Dictionary { ["tokenId"] = 43L }); await _processor.Process(exchange); exchange.Properties["identity-event-type"].Should().Be("TokenRevoked"); var data = exchange.Properties["identity-event-data"]!; data.GetType().GetProperty("UserId")!.GetValue(data).Should().Be(7L); } [Fact] public async Task Revoke_MissingTokenId_ReturnsValidationError() { var exchange = CreateExchange("revoke", new Dictionary { ["tokenId"] = 0L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } [Fact] public async Task Revoke_TokenNotFound_ReturnsNotFoundError() { _redb.LoadAsync(999).Returns((RedbObject?)null); var exchange = CreateExchange("revoke", new Dictionary { ["tokenId"] = 999L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("not_found"); } // ── Revoke by user ── [Fact] public async Task RevokeByUser_RevokesAllValidTokens() { // The processor first reads UserProps to resolve the public sub (value_guid), // then queries TokenProps by ValueGuid. Both queryables must be seeded. var userGuid = Guid.NewGuid(); var userObj = MockRedbQuery.CreateObject(100, "u100", new UserProps()); userObj.Key = 100; userObj.value_guid = userGuid; MockRedbQuery.Setup(_redb, new List> { userObj }); var items = new List> { MockRedbQuery.CreateObject(1, "t1", new TokenProps { Status = Statuses.Valid, Type = "access_token" }), MockRedbQuery.CreateObject(2, "t2", new TokenProps { Status = Statuses.Valid, Type = "refresh_token" }) }; // TokenProps subject linkage moved from Key→value_guid; mirror that on the seed. foreach (var t in items) t.value_guid = userGuid; MockRedbQuery.Setup(_redb, items); _redb.SaveAsync(Arg.Any>()).Returns(ci => ci.Arg>().Id); var exchange = CreateExchange("revoke-by-user", new Dictionary { ["userId"] = 100L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.RevokedCount.Should().Be(2); items.Should().OnlyContain(t => t.Props.Status == Statuses.Revoked); exchange.Properties["identity-event-type"].Should().Be("TokensRevokedByUser"); } [Fact] public async Task RevokeByUser_MissingUserId_ReturnsValidationError() { var exchange = CreateExchange("revoke-by-user", new Dictionary { ["userId"] = 0L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } // ── Unknown operation ── [Fact] public async Task UnknownOperation_ReturnsInvalidOperation() { var exchange = CreateExchange("destroy"); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("invalid_operation"); } }