Path: redb-identity/tests/redb.Identity.Tests/FullStack/ManagementTokenStatusTests.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/FullStack/ManagementTokenStatusTests.cs
----
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using AwesomeAssertions;
using OpenIddict.Abstractions;
using redb.Core.Models.Entities;
using redb.Identity.Core.Models;
using redb.Identity.Core.Services;
using redb.Identity.Core.Stores;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.FullStack;
///
/// The management API accepts an access token only while its stored entry is valid: a token revoked at the
/// revocation endpoint, or one whose client was deleted, is answered 401 from then on.
///
/// The check reads only the token's object row: value_bool is true while the token is valid and false once it
/// is not. A row written before that mirror existed (NULL) is checked by the full read of the entry.
///
///
[Collection("ProductionHttp")]
public class ManagementTokenStatusTests
{
private const string Secret = "status-secret-value-1234";
private readonly ProductionHttpFixture _fx;
public ManagementTokenStatusTests(ProductionHttpFixture fx) => _fx = fx;
private async Task<(string ClientId, string Id)> CreateClientAsync()
{
var clientId = "status-" + Guid.NewGuid().ToString("N")[..12];
using var create = new HttpRequestMessage(HttpMethod.Post, "/api/v1/identity/applications")
{
Content = JsonContent.Create(new
{
clientId,
clientSecret = Secret,
clientType = "confidential",
displayName = "Token status",
permissions = new[] { "ept:token", "ept:revocation", "gt:client_credentials", "scp:identity:read" },
}),
};
create.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken);
using var created = await _fx.Http.SendAsync(create);
var body = await created.Content.ReadAsStringAsync();
((int)created.StatusCode).Should().BeInRange(200, 299, body);
return (clientId, JsonDocument.Parse(body).RootElement.GetProperty("id").GetString()!);
}
private async Task TokenAsync(string clientId)
{
using var response = await _fx.Http.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = clientId,
["client_secret"] = Secret,
["scope"] = "identity:read",
}));
var body = await response.Content.ReadAsStringAsync();
response.StatusCode.Should().Be(HttpStatusCode.OK, body);
return JsonDocument.Parse(body).RootElement.GetProperty("access_token").GetString()!;
}
private async Task ListApplicationsAsync(string token)
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/identity/applications?offset=0&count=1");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
using var response = await _fx.Http.SendAsync(request);
return response.StatusCode;
}
[Fact]
public async Task A_revoked_access_token_is_refused_by_the_management_api()
{
var (clientId, id) = await CreateClientAsync();
var token = await TokenAsync(clientId);
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.OK);
(await TokenRowAsync(id)).value_bool.Should().BeTrue("a valid token's row carries the mirror");
using var revoke = await _fx.Http.PostAsync("/connect/revocation", new FormUrlEncodedContent(new Dictionary
{
["token"] = token,
["token_type_hint"] = "access_token",
["client_id"] = clientId,
["client_secret"] = Secret,
}));
revoke.StatusCode.Should().Be(HttpStatusCode.OK, await revoke.Content.ReadAsStringAsync());
(await TokenRowAsync(id)).value_bool.Should().BeFalse();
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.Unauthorized);
}
[Fact]
public async Task A_token_of_a_deleted_client_is_refused_by_the_management_api()
{
var (clientId, id) = await CreateClientAsync();
var token = await TokenAsync(clientId);
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.OK);
using var delete = new HttpRequestMessage(HttpMethod.Delete, $"/api/v1/identity/applications/{id}");
delete.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken);
using var deleted = await _fx.Http.SendAsync(delete);
((int)deleted.StatusCode).Should().BeInRange(200, 299, await deleted.Content.ReadAsStringAsync());
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.Unauthorized);
}
// The one access token the client got (client_credentials, one token request per test).
private async Task> TokenRowAsync(string applicationId)
{
var appId = long.Parse(applicationId);
var rows = await _fx.Redb.Query().WhereRedb(o => o.ValueLong == appId).ToListAsync();
rows.Should().ContainSingle();
return rows[0];
}
[Fact]
public async Task A_redeemed_token_is_refused_by_the_management_api()
{
var (clientId, id) = await CreateClientAsync();
var token = await TokenAsync(clientId);
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.OK);
var row = await TokenRowAsync(id);
TokenStatus.Set(row, OpenIddictConstants.Statuses.Redeemed);
await _fx.Redb.SaveAsync(row);
(await TokenRowAsync(id)).value_bool.Should().BeFalse();
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.Unauthorized);
}
[Fact]
public async Task A_deleted_token_entry_is_refused_by_the_management_api()
{
var (clientId, id) = await CreateClientAsync();
var token = await TokenAsync(clientId);
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.OK);
var row = await TokenRowAsync(id);
await IdentityDeletionHelper.DeleteAsync(_fx.Redb, null, row.id);
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.Unauthorized);
}
[Fact]
public async Task A_row_written_before_the_mirror_is_checked_by_its_status()
{
var (clientId, id) = await CreateClientAsync();
var token = await TokenAsync(clientId);
// As written by a build before the mirror: value_bool NULL, status valid in the values.
var row = await TokenRowAsync(id);
row.value_bool = null;
await _fx.Redb.SaveAsync(row);
(await TokenRowAsync(id)).value_bool.Should().BeNull();
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.OK);
// Revoked by such a build: the status moved, value_bool stayed NULL. The test writes past the helper here on
// purpose (the guard scans the product sources only).
row = await TokenRowAsync(id);
row.Props.Status = OpenIddictConstants.Statuses.Revoked;
row.value_bool = null;
await _fx.Redb.SaveAsync(row);
(await TokenRowAsync(id)).value_bool.Should().BeNull();
(await ListApplicationsAsync(token)).Should().Be(HttpStatusCode.Unauthorized);
}
}