Path: redb-identity/tests/redb.Identity.Tests/Http/E2E/ClientCredentialsFlowTests.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/Http/E2E/ClientCredentialsFlowTests.cs
----
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using AwesomeAssertions;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.Http.E2E;
///
/// E2E test: OAuth 2.0 Client Credentials flow over real HTTP.
/// Uses with real PostgreSQL + Kestrel.
///
[Collection("HttpIdentity")]
public class ClientCredentialsFlowTests
{
private readonly HttpIdentityFixture _fixture;
public ClientCredentialsFlowTests(HttpIdentityFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task TokenEndpoint_ClientCredentials_ReturnsAccessToken()
{
var content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "e2e-test-client",
["client_secret"] = "e2e-test-secret"
});
var response = await _fixture.Http.PostAsync("/connect/token", content);
response.StatusCode.Should().Be(HttpStatusCode.OK);
var body = await response.Content.ReadAsStringAsync();
var json = JsonDocument.Parse(body).RootElement;
// Verify we got a token response (access_token) or an error response
if (json.TryGetProperty("access_token", out var token))
{
token.GetString().Should().NotBeNullOrEmpty();
json.GetProperty("token_type").GetString().Should().Be("Bearer");
}
else
{
// If error, surface it for diagnostics
json.TryGetProperty("error", out var error).Should().BeTrue(
$"expected either access_token or error in response: {body}");
Assert.Fail($"Token endpoint returned error: {error}, body: {body}");
}
}
[Fact]
public async Task TokenEndpoint_ClientCredentials_WithScopes_ReturnsToken()
{
// In degraded mode, scopes are not validated by the store,
// so any scope string is accepted by the ClaimsPrincipal handler
var content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "scoped-client",
["client_secret"] = "scoped-secret",
["scope"] = "api profile"
});
var response = await _fixture.Http.PostAsync("/connect/token", content);
// Degraded mode may return 200+token or 400+error
response.StatusCode.Should().BeOneOf(HttpStatusCode.OK, HttpStatusCode.BadRequest);
var body = await response.Content.ReadAsStringAsync();
body.Should().NotBeNullOrEmpty("token endpoint should return a JSON response");
var json = JsonDocument.Parse(body).RootElement;
// Degraded mode may or may not issue scoped tokens; verify we got a valid JSON response
var hasToken = json.TryGetProperty("access_token", out _);
var hasError = json.TryGetProperty("error", out _);
(hasToken || hasError).Should().BeTrue(
$"expected either access_token or error in response: {body}");
}
[Fact]
public async Task TokenEndpoint_BasicAuth_ClientCredentials()
{
var credentials = Convert.ToBase64String("basic-client:basic-secret"u8);
var content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials"
});
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/token")
{
Content = content
};
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
var response = await _fixture.Http.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.OK);
var json = await response.Content.ReadFromJsonAsync();
json.GetProperty("access_token").GetString().Should().NotBeNullOrEmpty();
}
[Fact]
public async Task TokenEndpoint_MissingGrantType_ReturnsErrorResponse()
{
var content = new FormUrlEncodedContent(new Dictionary
{
["client_id"] = "bad-client"
});
var response = await _fixture.Http.PostAsync("/connect/token", content);
// In degraded mode, OpenIddict may still process the request;
// verify we get a parseable response (error or success)
var body = await response.Content.ReadAsStringAsync();
body.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task TokenEndpoint_ResponseContainsRequiredFields()
{
var content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "fields-test-client",
["client_secret"] = "fields-test-secret"
});
var response = await _fixture.Http.PostAsync("/connect/token", content);
response.StatusCode.Should().Be(HttpStatusCode.OK);
var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement;
json.GetProperty("access_token").GetString().Should().NotBeNullOrEmpty();
json.GetProperty("token_type").GetString().Should().Be("Bearer");
json.TryGetProperty("expires_in", out var expiresIn).Should().BeTrue();
expiresIn.GetInt32().Should().BeGreaterThan(0);
}
[Fact]
public async Task UserinfoEndpoint_WithAccessToken_ReturnsSubject()
{
// First, get an access token
var tokenContent = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "userinfo-client",
["client_secret"] = "userinfo-secret"
});
var tokenResp = await _fixture.Http.PostAsync("/connect/token", tokenContent);
tokenResp.StatusCode.Should().Be(HttpStatusCode.OK);
var tokenJson = JsonDocument.Parse(await tokenResp.Content.ReadAsStringAsync()).RootElement;
var accessToken = tokenJson.GetProperty("access_token").GetString()!;
// Call userinfo with the Bearer token
var request = new HttpRequestMessage(HttpMethod.Get, "/connect/userinfo");
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
var response = await _fixture.Http.SendAsync(request);
// Degraded mode may return 200 with claims or 401/400
response.StatusCode.Should().BeOneOf(HttpStatusCode.OK, HttpStatusCode.Unauthorized, HttpStatusCode.BadRequest);
if (response.StatusCode == HttpStatusCode.OK)
{
var body = await response.Content.ReadAsStringAsync();
body.Should().NotBeNullOrEmpty("userinfo should return claims data");
}
}
[Fact]
public async Task RevocationEndpoint_AcceptsToken()
{
// Get a token to revoke
var tokenContent = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "revoke-client",
["client_secret"] = "revoke-secret"
});
var tokenResp = await _fixture.Http.PostAsync("/connect/token", tokenContent);
tokenResp.StatusCode.Should().Be(HttpStatusCode.OK);
var tokenJson = JsonDocument.Parse(await tokenResp.Content.ReadAsStringAsync()).RootElement;
var accessToken = tokenJson.GetProperty("access_token").GetString()!;
// Revoke the token
var revokeContent = new FormUrlEncodedContent(new Dictionary
{
["token"] = accessToken,
["client_id"] = "revoke-client",
["client_secret"] = "revoke-secret"
});
var response = await _fixture.Http.PostAsync("/connect/revocation", revokeContent);
// RFC 7009: revocation always returns 200 OK (even for invalid tokens)
response.StatusCode.Should().Be(HttpStatusCode.OK,
"revocation response: {0}", await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task IntrospectionEndpoint_ReturnsActiveFlag()
{
// Get a token to introspect
var tokenContent = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "introspect-client",
["client_secret"] = "introspect-secret"
});
var tokenResp = await _fixture.Http.PostAsync("/connect/token", tokenContent);
tokenResp.StatusCode.Should().Be(HttpStatusCode.OK);
var tokenJson = JsonDocument.Parse(await tokenResp.Content.ReadAsStringAsync()).RootElement;
var accessToken = tokenJson.GetProperty("access_token").GetString()!;
// Introspect the token
var introspectContent = new FormUrlEncodedContent(new Dictionary
{
["token"] = accessToken,
["client_id"] = "introspect-client",
["client_secret"] = "introspect-secret"
});
var response = await _fixture.Http.PostAsync("/connect/introspect", introspectContent);
response.StatusCode.Should().Be(HttpStatusCode.OK,
"introspection response: {0}", await response.Content.ReadAsStringAsync());
var body = await response.Content.ReadAsStringAsync();
body.Should().NotBeNullOrEmpty();
var json = JsonDocument.Parse(body).RootElement;
json.TryGetProperty("active", out var active).Should().BeTrue(
"RFC 7662: introspection must return 'active' field; got: {0}", body);
active.GetBoolean().Should().BeTrue("freshly issued token should be active");
}
}