Path: redb-identity/tests/redb.Identity.Client.Tests/Backchannel/BackchannelTokenProviderTests.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.Client.Tests/Backchannel/BackchannelTokenProviderTests.cs
----
using System.Net;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using redb.Identity.Client.Backchannel;
using redb.Identity.Client.Tests.TestKit;
using Xunit;
namespace redb.Identity.Client.Tests.Backchannel;
///
/// The backchannel client takes its token from the token endpoint that the discovery document of
/// names, never from the API base URL: the management
/// API can listen on a port of its own, where there is no token endpoint.
///
public sealed class BackchannelTokenProviderTests
{
private const string Discovery = "https://id.test/.well-known/openid-configuration";
private const string TokenEndpoint = "https://id.test/connect/token";
private static BackchannelIdentityClientOptions Options(Uri? authority = null) => new()
{
BaseUrl = new Uri("http://localhost:5003/"),
Authority = authority ?? new Uri("https://id.test/"),
ClientId = "identity-backchannel",
ClientSecret = "secret",
};
private static FakeHttpMessageHandler Server(int expiresIn = 3600, Func? discoveryUp = null) => new(req =>
{
var url = req.RequestUri!.ToString();
if (req.Method == HttpMethod.Get && url == Discovery)
{
return Task.FromResult(discoveryUp is null || discoveryUp()
? FakeHttpMessageHandler.BuildResponse(HttpStatusCode.OK, $$"""{"issuer":"https://id.test/","token_endpoint":"{{TokenEndpoint}}"}""")
: FakeHttpMessageHandler.BuildResponse(HttpStatusCode.ServiceUnavailable, null));
}
if (req.Method == HttpMethod.Post && url == TokenEndpoint)
{
return Task.FromResult(FakeHttpMessageHandler.BuildResponse(HttpStatusCode.OK, $$"""{"access_token":"tok","expires_in":{{expiresIn}}}"""));
}
return Task.FromResult(FakeHttpMessageHandler.BuildResponse(HttpStatusCode.NotFound, null));
});
[Fact]
public async Task The_token_comes_from_the_token_endpoint_of_the_authority_not_from_the_api_base_url()
{
var server = Server();
using var provider = new BackchannelTokenProvider(new HttpClient(server), Microsoft.Extensions.Options.Options.Create(Options()));
var token = await provider.GetAccessTokenAsync();
token.Should().Be("tok");
server.Requests.Select(r => $"{r.Method} {r.RequestUri}").Should().Equal(
$"GET {Discovery}",
$"POST {TokenEndpoint}");
}
[Fact]
public async Task An_authority_with_a_path_keeps_it_in_the_discovery_address()
{
var server = new FakeHttpMessageHandler(HttpStatusCode.NotFound, null);
using var provider = new BackchannelTokenProvider(new HttpClient(server),
Microsoft.Extensions.Options.Options.Create(Options(new Uri("https://id.test/tenant-a"))));
await FluentActions.Awaiting(() => provider.GetAccessTokenAsync()).Should().ThrowAsync();
server.Requests[0].RequestUri!.ToString().Should().Be("https://id.test/tenant-a/.well-known/openid-configuration");
}
[Fact]
public async Task The_discovery_document_is_read_once_and_the_token_endpoint_kept()
{
var server = Server(expiresIn: 0);
using var provider = new BackchannelTokenProvider(new HttpClient(server), Microsoft.Extensions.Options.Options.Create(Options()));
await provider.GetAccessTokenAsync();
await provider.GetAccessTokenAsync();
server.Requests.Count(r => r.RequestUri!.ToString() == Discovery).Should().Be(1);
server.Requests.Count(r => r.RequestUri!.ToString() == TokenEndpoint).Should().Be(2);
}
[Fact]
public async Task An_unreachable_authority_fails_the_request_and_the_next_request_reads_discovery_again()
{
var up = false;
var server = Server(discoveryUp: () => up);
using var provider = new BackchannelTokenProvider(new HttpClient(server), Microsoft.Extensions.Options.Options.Create(Options()));
await FluentActions.Awaiting(() => provider.GetAccessTokenAsync()).Should().ThrowAsync();
up = true;
(await provider.GetAccessTokenAsync()).Should().Be("tok");
server.Requests.Count(r => r.RequestUri!.ToString() == Discovery).Should().Be(2);
}
[Fact]
public void Without_an_authority_the_options_are_refused_and_the_message_names_the_key()
{
var services = new ServiceCollection();
services.AddBackchannelIdentityClient(o =>
{
o.BaseUrl = new Uri("http://localhost:5003/");
o.ClientId = "identity-backchannel";
o.ClientSecret = "secret";
});
using var sp = services.BuildServiceProvider();
var act = () => sp.GetRequiredService>().Value;
act.Should().Throw()
.WithMessage("*BackchannelIdentityClientOptions.Authority*");
}
}