Path: redb-identity/tests/redb.Identity.Tests/Session/SessionIntegrationTests.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/Session/SessionIntegrationTests.cs ---- using AwesomeAssertions; using redb.Identity.Contracts.Common; using redb.Identity.Contracts.Routes; using redb.Identity.Tests.Infrastructure; using Xunit; using Xunit.Abstractions; namespace redb.Identity.Tests.Session; /// /// Integration tests for session management via direct-vm routes. /// Uses with full OpenIddict pipeline. /// [Collection("ProductionBootstrap")] public class SessionIntegrationTests { private readonly ProductionBootstrapFixture _fx; private readonly ITestOutputHelper _output; public SessionIntegrationTests(ProductionBootstrapFixture fx, ITestOutputHelper output) { _fx = fx; _output = output; } [Fact] public async Task AuthCode_CreatesSession() { // Authenticate to create a session var authorizeResult = await Authorize(); authorizeResult.Should().ContainKey("code", "Auth should succeed and create a session"); // List sessions for the test user var userId = await GetTestUserId(); var listExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "list" }); var sessions = listExchange.Out?.Body.Should().BeAssignableTo>().Subject; sessions.Should().NotBeEmpty("Authentication should have created a session"); _output.WriteLine($"Found {sessions!.Count()} sessions for user {userId}"); } [Fact] public async Task ManageSessions_ListReturnsSessionInfo() { // Authenticate first await Authorize(); var userId = await GetTestUserId(); var listExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "list" }); var sessions = listExchange.Out?.Body as IEnumerable; sessions.Should().NotBeNull(); foreach (var session in sessions!) { if (session is IDictionary dict) { dict.Should().ContainKey("SessionId"); dict.Should().ContainKey("UserId"); dict.Should().ContainKey("Status"); _output.WriteLine($"Session: id={dict["SessionId"]}, status={dict["Status"]}"); } } } [Fact] public async Task ManageSessions_RevokeAll_RevokesAllSessions() { // Authenticate multiple times await Authorize(); await Authorize(); var userId = await GetTestUserId(); // Revoke all sessions var revokeExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "revoke-all" }); var revokeResponse = revokeExchange.Out!.Body.Should().BeOfType().Subject; revokeResponse.Success.Should().BeTrue(); var revoked = revokeResponse.Revoked!.Value; revoked.Should().BeGreaterThanOrEqualTo(1); _output.WriteLine($"Revoked {revoked} sessions"); // List should be empty var listExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "list" }); var sessions = listExchange.Out?.Body as IEnumerable; sessions.Should().NotBeNull(); sessions!.Count().Should().Be(0); } [Fact] public async Task Logout_RevokesSessionsAndInvalidatesTokens() { // Authenticate and get tokens var authorizeResult = await Authorize(); authorizeResult.Should().ContainKey("code"); var userId = await GetTestUserId(); // Perform logout var logoutExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "logout" }); var logoutResponse = logoutExchange.Out!.Body.Should().BeOfType().Subject; logoutResponse.Success.Should().BeTrue(); _output.WriteLine($"Logged out. Sessions revoked: {logoutResponse.SessionsRevoked}"); // Sessions should be empty var listExchange = await _fx.RequestWithHeaders(IdentityEndpoints.ManageSessions, new Dictionary { ["userId"] = userId }, new Dictionary { ["operation"] = "list" }); var sessions = listExchange.Out?.Body as IEnumerable; sessions!.Count().Should().Be(0); } [Fact] public async Task LogoutEndpoint_DirectVm_Works() { // Authenticate await Authorize(); var userId = await GetTestUserId(); // Call the logout endpoint directly var logoutResult = await _fx.Request(IdentityEndpoints.Logout, new Dictionary { ["userId"] = userId }); var response = (Dictionary)logoutResult!; response.Should().ContainKey("success"); ((bool)response["success"]!).Should().BeTrue(); _output.WriteLine($"Logout via direct endpoint. Sessions revoked: {response["sessions_revoked"]}"); } [Fact] public async Task Discovery_ContainsEndSessionEndpoint() { var discoveryResult = await _fx.Request(IdentityEndpoints.Discovery, new Dictionary()); var response = (Dictionary)discoveryResult!; response.Should().ContainKey("end_session_endpoint"); response["end_session_endpoint"]!.ToString() .Should().Contain("/connect/logout"); _output.WriteLine($"end_session_endpoint: {response["end_session_endpoint"]}"); } private async Task> Authorize() { var body = new Dictionary { ["response_type"] = "code", ["client_id"] = ProductionBootstrapFixture.TestClientIdPublic, ["redirect_uri"] = ProductionBootstrapFixture.TestRedirectUri, ["scope"] = "openid profile email", ["code_challenge"] = GenerateCodeChallenge(), ["code_challenge_method"] = "S256" }; var result = await _fx.RequestWithSession(IdentityEndpoints.Authorize, body); return (Dictionary)result!; } private Task GetTestUserId() => // Goes through the fixture's per-call scope helper because this test calls run // in parallel with the Worker's WireTap audit-pipeline INSERTs; using // `_fx.Redb` directly would share an NpgsqlConnection with the audit writer // and surface as "A command is already in progress: INSERT INTO identity_audit_log". _fx.WithRedb(async redb => { var coreUser = await redb.UserProvider.GetUserByLoginAsync(ProductionBootstrapFixture.TestUsername) .ConfigureAwait(false) ?? throw new Exception("Test user not found"); return coreUser.Id; }); private static string GenerateCodeChallenge() { var bytes = System.Security.Cryptography.RandomNumberGenerator.GetBytes(32); var verifier = Convert.ToBase64String(bytes) .TrimEnd('=').Replace('+', '-').Replace('/', '_'); var hash = System.Security.Cryptography.SHA256.HashData( System.Text.Encoding.ASCII.GetBytes(verifier)); return Convert.ToBase64String(hash) .TrimEnd('=').Replace('+', '-').Replace('/', '_'); } }