Path: redb-identity/tests/redb.Identity.Tests/Cleanup/TokenCleanupTests.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/Cleanup/TokenCleanupTests.cs ---- using redb.Identity.Contracts.Tokens; using AwesomeAssertions; using Microsoft.Extensions.Options; using NSubstitute; using redb.Core; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Services; using redb.Identity.Core.Configuration; 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.Cleanup; public class TokenCleanupTests { private readonly IRedbService _redb = Substitute.For(); private readonly IBackgroundDeletionService _bgDeletion = Substitute.For(); private readonly IRedbSecurityContext _secCtx = Substitute.For(); private readonly TokenCleanupProcessor _processor; public TokenCleanupTests() { var options = Options.Create(new RedbIdentityOptions { TokenRetentionDays = 30 }); var systemUser = new RedbUser { Id = 0, Login = "sys", Name = "System" }; _secCtx.GetEffectiveUser().Returns(systemUser); _redb.SecurityContext.Returns(_secCtx); _bgDeletion.DeleteAsync(Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(ci => new DeletionMark(999, ci.Arg>().Count())); var context = MockRouteContext.Create(_redb); _processor = new TokenCleanupProcessor(context, options, backgroundDeletion: _bgDeletion); } private static RedbObject CreateToken(long id, string status, int daysOld, DateTimeOffset? dateComplete = null) { var token = MockRedbQuery.CreateObject(id, $"token-{id}", new TokenProps { Status = status, Type = "access_token" }); token.DateCreate = DateTimeOffset.UtcNow.AddDays(-daysOld); if (dateComplete.HasValue) token.DateComplete = dateComplete; return token; } [Fact] public async Task Prune_DeletesRevokedTokensOlderThanRetention() { var tokens = new List> { CreateToken(1, Statuses.Revoked, daysOld: 60), CreateToken(2, Statuses.Revoked, daysOld: 31), CreateToken(3, Statuses.Valid, daysOld: 60) // valid → not pruned by step 1 }; MockRedbQuery.Setup(_redb, tokens); MockRedbQuery.Setup(_redb, new List>()); var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedTokens.Should().Be(2); exchange.Properties["identity-event-type"].Should().Be("TokensPruned"); // Marked through the CALLER's service so the mark joins the caller's transaction. // IBackgroundDeletionService.DeleteAsync would mark on a second connection of its // own; it is left to purge, which it does by polling for the trash container. await _redb.Received(1).SoftDeleteAsync( Arg.Is>(ids => ids.Count() == 2), Arg.Any(), Arg.Any(), Arg.Any()); await _bgDeletion.DidNotReceive().DeleteAsync( Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Prune_SkipsTokensWithinRetention() { var tokens = new List> { CreateToken(1, Statuses.Revoked, daysOld: 10), // within 30 days CreateToken(2, Statuses.Revoked, daysOld: 5) // within 30 days }; MockRedbQuery.Setup(_redb, tokens); MockRedbQuery.Setup(_redb, new List>()); var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedTokens.Should().Be(0); // Assert on the path that actually marks. The background service is no longer called // at all, so an assertion only on it would hold even if a token HAD been deleted. await _redb.DidNotReceive().SoftDeleteAsync( Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Prune_DeletesExpiredValidTokens() { var expired = DateTimeOffset.UtcNow.AddDays(-1); // expired yesterday var tokens = new List> { CreateToken(1, Statuses.Valid, daysOld: 60, dateComplete: expired) }; MockRedbQuery.Setup(_redb, tokens); MockRedbQuery.Setup(_redb, new List>()); var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedTokens.Should().Be(1); } [Fact] public async Task Prune_EmptyDB_ReturnsZero() { MockRedbQuery.Setup(_redb, new List>()); MockRedbQuery.Setup(_redb, new List>()); var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedTokens.Should().Be(0); body.PrunedAuthorizations.Should().Be(0); } [Fact] public async Task Prune_PrunesOrphanedAuthorizations() { MockRedbQuery.Setup(_redb, new List>()); var auths = new List> { MockRedbQuery.CreateObject(100, "auth-1", new AuthorizationProps { Status = Statuses.Revoked }) }; auths[0].DateCreate = DateTimeOffset.UtcNow.AddDays(-60); MockRedbQuery.Setup(_redb, auths); // No tokens reference this authorization → orphaned var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedAuthorizations.Should().Be(1); await _redb.Received().SoftDeleteAsync( Arg.Is>(ids => ids.Contains(100L)), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Prune_KeepsAuthorizationsWithTokens() { // Token referencing authorization 100 var tokens = new List> { CreateToken(1, Statuses.Valid, daysOld: 5) }; tokens[0].Props.AuthorizationObjectId = 100; MockRedbQuery.Setup(_redb, tokens); var auths = new List> { MockRedbQuery.CreateObject(100, "auth-1", new AuthorizationProps { Status = Statuses.Revoked }) }; auths[0].DateCreate = DateTimeOffset.UtcNow.AddDays(-60); MockRedbQuery.Setup(_redb, auths); var exchange = new TestExchange(); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.PrunedAuthorizations.Should().Be(0); } }