Path: redb-identity/tests/redb.Identity.Tests/Architecture/ReferenceTokenGuardTests.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/Architecture/ReferenceTokenGuardTests.cs
----
using System.Text.RegularExpressions;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.Architecture;
///
/// The resource side's entry check (ValidateTokenEntryStatusHandler) reads only the token's object row and does not
/// refuse an entry that carries a ReferenceId, as OpenIddict's built-in RestoreTokenEntryProperties does (a leaked
/// reference payload presented as a plain token). That is sound only while the server issues no reference access or
/// refresh tokens. These tests fail the day either is turned on.
///
[Collection("ProductionHttp")]
public class ReferenceTokenGuardTests
{
private const string Reason =
"reference access or refresh tokens are on: ValidateTokenEntryStatusHandler must then also refuse a token " +
"entry that carries a ReferenceId when the token was not validated as a reference token, as OpenIddict's " +
"RestoreTokenEntryProperties does";
private readonly ProductionHttpFixture _fx;
public ReferenceTokenGuardTests(ProductionHttpFixture fx) => _fx = fx;
[Fact]
public void The_server_issues_no_reference_access_or_refresh_tokens()
{
var options = _fx.ServiceProvider.GetRequiredService>().CurrentValue;
options.UseReferenceAccessTokens.Should().BeFalse(Reason);
options.UseReferenceRefreshTokens.Should().BeFalse(Reason);
}
[Fact]
public void No_source_turns_reference_tokens_on()
{
// The options check above sees the test host's configuration only; a switch behind a setting the tests do
// not set is caught here.
var root = LocateSourceRoot();
var sep = Path.DirectorySeparatorChar;
var pattern = new Regex(@"\b(UseReferenceAccessTokens|UseReferenceRefreshTokens)\b", RegexOptions.Compiled);
var offenders = Directory.EnumerateFiles(root, "*.cs", SearchOption.AllDirectories)
.Where(f => !f.Contains($"{sep}obj{sep}", StringComparison.Ordinal) && !f.Contains($"{sep}bin{sep}", StringComparison.Ordinal))
.Where(f => pattern.IsMatch(Regex.Replace(File.ReadAllText(f), @"//[^\n]*", string.Empty)))
.Select(f => Path.GetRelativePath(root, f))
.ToList();
offenders.Should().BeEmpty(Reason);
}
private static string LocateSourceRoot()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null)
{
var candidate = Path.Combine(dir.FullName, "redb.Identity", "src");
if (Directory.Exists(Path.Combine(candidate, "redb.Identity.Core")))
return candidate;
candidate = Path.Combine(dir.FullName, "src");
if (Directory.Exists(Path.Combine(candidate, "redb.Identity.Core")))
return candidate;
dir = dir.Parent;
}
throw new InvalidOperationException("Could not locate redb.Identity/src from the test base directory.");
}
}