Path: redb-identity/tests/redb.Identity.Tests/Consent/ConsentIntegrationTests.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/Consent/ConsentIntegrationTests.cs
----
using AwesomeAssertions;
using redb.Identity.Core.Models;
using redb.Identity.Contracts.Routes;
using redb.Identity.Core.Services;
using redb.Identity.Tests.Infrastructure;
using redb.Route.Core;
using Xunit;
namespace redb.Identity.Tests.Consent;
///
/// Production integration tests for consent tracking through the full OpenIddict pipeline.
/// Uses real PostgreSQL + real OpenIddict pipeline + direct-vm:// endpoints.
///
[Collection("ProductionBootstrap")]
public class ConsentIntegrationTests
{
private readonly ProductionBootstrapFixture _fx;
public ConsentIntegrationTests(ProductionBootstrapFixture fx) => _fx = fx;
[Fact]
public async Task AuthCode_ExplicitConsent_RequiresConsentThenSucceeds()
{
// Switch test client to explicit consent. Direct DB reads/writes are wrapped in
// _fx.WithRedb(...) because this test calls _fx.Request(...) which fires Worker-side
// WireTap audit pipeline INSERTs — sharing _fx.Redb's root-scoped NpgsqlConnection
// with that concurrent writer surfaces as
// "A command is already in progress: INSERT INTO identity_audit_log".
var app = await _fx.WithRedb(redb => redb.Query()
.Where(p => p.ClientId == ProductionBootstrapFixture.TestClientId)
.FirstOrDefaultAsync());
app.Should().NotBeNull();
app!.Props.ConsentType = "explicit";
await _fx.WithRedb(redb => redb.SaveAsync(app));
try
{
// First authorize — should get consent_required error (no existing consent)
var firstResult = await AuthorizeAsync(ProductionBootstrapFixture.TestClientId,
"openid profile", ProductionBootstrapFixture.TestClientSecret);
firstResult.Should().BeNull("explicit consent without prior grant should not return a code");
// Grant consent via ConsentGrant endpoint
var coreUser = await _fx.WithRedb(async redb =>
await redb.UserProvider.GetUserByLoginAsync(ProductionBootstrapFixture.TestUsername)
?? throw new Exception("Test user not found"));
var grantBody = new Dictionary
{
["userId"] = coreUser.Id,
["clientId"] = ProductionBootstrapFixture.TestClientId,
["scopes"] = "openid profile"
};
var grantResult = await _fx.Request(IdentityEndpoints.ConsentGrant, grantBody);
var grantResponse = grantResult.Should().BeOfType>().Subject;
grantResponse.Should().ContainKey("success");
grantResponse["success"].Should().Be(true);
// Retry authorize — should now succeed with consent recorded
var code = await AuthorizeAsync(ProductionBootstrapFixture.TestClientId,
"openid profile", ProductionBootstrapFixture.TestClientSecret);
code.Should().NotBeNullOrEmpty("authorize should succeed after consent granted");
// Verify consent was recorded — own scope, owns its NpgsqlConnection for the query.
var consent = await _fx.WithRedb(async redb =>
{
var svc = new ConsentService(redb);
return await svc.CheckAsync(coreUser.Id, app.id, ["openid", "profile"]);
});
consent.Should().NotBeNull("explicit consent should have been recorded via ConsentGrant");
consent!.Props.Scopes.Should().Contain("openid");
consent.Props.Scopes.Should().Contain("profile");
}
finally
{
// Restore to implicit consent
app.Props.ConsentType = "implicit";
await _fx.WithRedb(redb => redb.SaveAsync(app));
}
}
[Fact]
public async Task AuthCode_ImplicitConsent_NoConsentRecord()
{
// Direct DB reads via _fx.WithRedb to avoid sharing NpgsqlConnection with the
// Worker's WireTap audit pipeline (see comment on AuthCode_ExplicitConsent_*).
var app = await _fx.WithRedb(redb => redb.Query()
.Where(p => p.ClientId == ProductionBootstrapFixture.TestClientIdPublic)
.FirstOrDefaultAsync());
app.Should().NotBeNull();
app!.Props.ConsentType.Should().BeOneOf("implicit", null);
// Authorize
var code = await AuthorizeAsync(ProductionBootstrapFixture.TestClientIdPublic, "openid profile");
code.Should().NotBeNullOrEmpty();
// For implicit consent, no permanent authorization should exist for this flow execution.
await _fx.WithRedb(async redb =>
{
var consentService = new ConsentService(redb);
var coreUser = await redb.UserProvider.GetUserByLoginAsync(ProductionBootstrapFixture.TestUsername)
?? throw new Exception("Test user not found");
// (OpenIddict may create ad-hoc ones, but our ConsentService only returns permanent ones)
var consent = await consentService.CheckAsync(coreUser.Id, app.id, ["openid", "profile"]);
// This might or might not be null depending on whether OpenIddict creates permanent auths
// The key assertion is that our ConsentService.GrantAsync was NOT called for implicit
// We verify this by checking that no permanent auth was created AFTER our specific flow
});
}
[Fact]
public async Task ConsentManagement_ListAndRevoke_ViaDirectVm()
{
// Bootstrap reads via per-call scope (see AuthCode_ExplicitConsent comment).
var (app, coreUser) = await _fx.WithRedb(async redb =>
{
var a = await redb.Query()
.Where(p => p.ClientId == ProductionBootstrapFixture.TestClientId)
.FirstOrDefaultAsync();
var u = await redb.UserProvider.GetUserByLoginAsync(ProductionBootstrapFixture.TestUsername)
?? throw new Exception("Test user not found");
return (a, u);
});
await _fx.WithRedb(redb =>
new ConsentService(redb).GrantAsync(coreUser.Id, app!.id, ["openid", "profile"]));
// List via direct-vm
var listExchange = await _fx.RequestWithHeaders(
IdentityEndpoints.ManageConsents,
new Dictionary { ["userId"] = coreUser.Id },
new Dictionary { ["operation"] = "list" });
var listBody = listExchange.Out?.Body;
listBody.Should().NotBeNull();
// Revoke via direct-vm
var revokeExchange = await _fx.RequestWithHeaders(
IdentityEndpoints.ManageConsents,
new Dictionary
{
["userId"] = coreUser.Id,
["applicationId"] = app.id
},
new Dictionary { ["operation"] = "revoke" });
// Verify consent is revoked
var afterRevoke = await _fx.WithRedb(redb =>
new ConsentService(redb).CheckAsync(coreUser.Id, app.id, ["openid"]));
afterRevoke.Should().BeNull("consent should be revoked");
}
[Fact]
public async Task ConsentManagement_RevokeAll_ViaDirectVm()
{
// Bootstrap: read apps + user via WithRedb (see explicit-consent comment for rationale).
var (app1, app2, coreUser) = await _fx.WithRedb(async redb =>
{
var a1 = await redb.Query()
.Where(p => p.ClientId == ProductionBootstrapFixture.TestClientId)
.FirstOrDefaultAsync();
var a2 = await redb.Query()
.Where(p => p.ClientId == ProductionBootstrapFixture.TestClientIdPublic)
.FirstOrDefaultAsync();
var u = await redb.UserProvider.GetUserByLoginAsync(ProductionBootstrapFixture.TestUsername)
?? throw new Exception("Test user not found");
return (a1, a2, u);
});
// GrantAsync writes via the same scoped redb — own scope per call to avoid sharing
// the captive Redb's NpgsqlConnection with the Worker's WireTap audit writer.
await _fx.WithRedb(redb =>
new ConsentService(redb).GrantAsync(coreUser.Id, app1!.id, ["openid"]));
await _fx.WithRedb(redb =>
new ConsentService(redb).GrantAsync(coreUser.Id, app2!.id, ["openid", "profile"]));
// Revoke all
var exchange = await _fx.RequestWithHeaders(
IdentityEndpoints.ManageConsents,
new Dictionary { ["userId"] = coreUser.Id },
new Dictionary { ["operation"] = "revoke-all" });
// Verify all revoked
var list = await _fx.WithRedb(redb => new ConsentService(redb).ListAsync(coreUser.Id));
list.Should().BeEmpty("all consents should be revoked");
}
private async Task AuthorizeAsync(string clientId, string scopes, string? clientSecret = null)
{
var (verifier, challenge) = GeneratePkce();
var body = new Dictionary
{
["response_type"] = "code",
["client_id"] = clientId,
["redirect_uri"] = ProductionBootstrapFixture.TestRedirectUri,
["scope"] = scopes,
["code_challenge"] = challenge,
["code_challenge_method"] = "S256"
};
if (clientSecret is not null)
body["client_secret"] = clientSecret;
var result = await _fx.RequestWithSession(IdentityEndpoints.Authorize, body);
var response = result as Dictionary;
return response?.TryGetValue("code", out var code) == true ? code?.ToString() : null;
}
private static (string verifier, string challenge) GeneratePkce()
{
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));
var challenge = Convert.ToBase64String(hash)
.TrimEnd('=').Replace('+', '-').Replace('/', '_');
return (verifier, challenge);
}
}