Path: redb-identity/tests/redb.Identity.Tests/Routes/MeSessionsProcessorTests.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/Routes/MeSessionsProcessorTests.cs
----
using AwesomeAssertions;
using redb.Identity.Contracts.Common;
using redb.Identity.Core.Routes.Processors;
using redb.Route.Core;
using Xunit;
namespace redb.Identity.Tests.Routes;
///
/// H3-SSO unit tests for . Covers the caller-id
/// precondition paths (missing / non-positive) which reject before
/// touching any redb service, so no DB or IRouteContext is required.
/// Full happy-path list/revoke and ownership-mismatch flows are covered by the
/// integration suite where a real service is wired.
///
public sealed class MeSessionsProcessorTests
{
private static Exchange BuildExchange(long? userId, string operation)
{
var ex = new Exchange(new Message { Body = new Dictionary() });
ex.In.Headers["operation"] = operation;
if (userId is not null)
ex.Properties["identity:management-user-id"] = userId.Value;
return ex;
}
[Fact]
public async Task MissingSubject_Returns401()
{
// IRouteContext is not reached — the subject check short-circuits first.
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: null, operation: "list");
await sut.Process(ex);
ex.HasOut.Should().BeTrue();
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401);
ex.IsStopped.Should().BeTrue();
ex.Out.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_token");
}
[Fact]
public async Task NonPositiveSubject_Returns401()
{
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: 0, operation: "list");
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401);
}
[Fact]
public async Task RevokeCurrent_MissingSidClaim_Returns400_SidUnavailable()
{
// Caller is authenticated (subject present) but the token has no sid claim
// (e.g. client_credentials grant). revoke-current is inapplicable → 400.
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: 42, operation: "revoke-current");
await sut.Process(ex);
ex.HasOut.Should().BeTrue();
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
ex.Out.Body.Should().BeOfType().Subject.Error.Should().Be("sid_unavailable");
}
[Fact]
public async Task RevokeCurrent_NonNumericSid_Returns400()
{
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: 42, operation: "revoke-current");
ex.Properties["identity:management-sid"] = "not-a-number";
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
}
[Fact]
public async Task RevokeOthers_MissingSubject_Returns401()
{
// Same subject-precondition as every other operation — no token, no API.
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: null, operation: "revoke-others");
await sut.Process(ex);
ex.HasOut.Should().BeTrue();
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401);
ex.Out.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_token");
}
[Fact]
public async Task RevokeOthers_MissingSidClaim_DoesNotReject400()
{
// CRITICAL contract: unlike revoke-current, revoke-others MUST NOT 400 when sid
// is absent — the processor falls through and revokes ALL sessions of the caller.
//
// This unit test only covers the *precondition gate*: the processor must NOT
// short-circuit with a 400/sid_unavailable response. To prove the gate let through
// we drive the processor with a null IRouteContext and assert two facts:
// 1. Process(...) throws ArgumentNullException from RedbRouteExtensions
// .GetRedbService(...) (ThrowIfNull guard at the entry).
// 2. The exchange was NOT stopped before the throw — i.e. no Reject(400) ran.
// A regression that re-introduces the sid_unavailable check for revoke-others
// would either set Out+IsStopped=true and return (no throw at all), violating #1,
// or reach Reject before throwing, violating #2.
//
// The full happy-path (list → revoke-each → audit event) requires a wired
// SessionService and is covered by the integration suite (see STATUS W-6 backlog).
var sut = new MeSessionsProcessor(context: null!, redbName: null);
var ex = BuildExchange(userId: 42, operation: "revoke-others");
var act = async () => await sut.Process(ex);
await act.Should().ThrowAsync();
ex.IsStopped.Should().BeFalse("revoke-others must not reject 400 for tokens without a sid claim");
}
}