Path: redb-identity/tests/redb.Identity.Client.Tests/Endpoints/AuthorityRuleTests.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/Endpoints/AuthorityRuleTests.cs ---- using System.Net; using AwesomeAssertions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using redb.Identity.Client.Auth; using redb.Identity.Client.Tests.TestKit; using redb.Identity.Contracts.Tokens; using Xunit; namespace redb.Identity.Client.Tests.Endpoints; /// /// One rule for the whole client: is the management API, and every /// protocol address (token, introspection, revocation, userinfo, discovery, JWKS) comes from the discovery document /// of . Here the API sits on a management port of its own, where none /// of those endpoints exist. /// public sealed class AuthorityRuleTests { private const string Discovery = "https://issuer.test/.well-known/openid-configuration"; private static readonly IdentityClientOptions Opts = new() { BaseUrl = new Uri("http://localhost:5003/"), Authority = new Uri("https://issuer.test/"), ClientId = "svc", ClientSecret = "secret", Scopes = ["identity:manage"], }; private static FakeHttpMessageHandler Server() => new(req => { var url = req.RequestUri!.ToString(); var body = url switch { Discovery => """ {"issuer":"https://issuer.test/", "token_endpoint":"https://issuer.test/connect/token", "introspection_endpoint":"https://issuer.test/connect/introspect", "revocation_endpoint":"https://issuer.test/connect/revocation", "userinfo_endpoint":"https://issuer.test/connect/userinfo", "jwks_uri":"https://issuer.test/.well-known/jwks"} """, "https://issuer.test/connect/token" => """{"access_token":"tok","token_type":"Bearer","expires_in":3600}""", "https://issuer.test/connect/introspect" => """{"active":true}""", "https://issuer.test/connect/revocation" => "{}", "https://issuer.test/connect/userinfo" => """{"sub":"alice"}""", "https://issuer.test/.well-known/jwks" => """{"keys":[]}""", _ => null, }; return Task.FromResult(body is null ? FakeHttpMessageHandler.BuildResponse(HttpStatusCode.NotFound, null) : FakeHttpMessageHandler.BuildResponse(HttpStatusCode.OK, body)); }); private static IdentityClient Client(FakeHttpMessageHandler server) => new(new HttpClient(server) { BaseAddress = Opts.BaseUrl }, Options.Create(Opts)); private static string[] Calls(FakeHttpMessageHandler server) => server.Requests.Select(r => $"{r.Method} {r.RequestUri}").ToArray(); [Fact] public async Task Client_credentials_token_comes_from_the_token_endpoint_of_the_authority() { var server = Server(); using var provider = new ClientCredentialsAccessTokenProvider(new HttpClient(server), Options.Create(Opts)); (await provider.GetAccessTokenAsync()).Should().Be("tok"); Calls(server).Should().Equal($"GET {Discovery}", "POST https://issuer.test/connect/token"); } [Fact] public async Task Discovery_document_is_read_at_the_authority() { var server = Server(); var doc = await Client(server).GetDiscoveryDocumentAsync(); doc.GetProperty("issuer").GetString().Should().Be("https://issuer.test/"); Calls(server).Should().Equal($"GET {Discovery}"); } [Fact] public async Task Jwks_is_read_at_the_jwks_uri_the_discovery_names_and_discovery_is_read_once() { var server = Server(); var client = Client(server); await client.GetDiscoveryDocumentAsync(); var jwks = await client.GetJwksAsync(); jwks.GetProperty("keys").GetArrayLength().Should().Be(0); Calls(server).Should().Equal($"GET {Discovery}", "GET https://issuer.test/.well-known/jwks"); } [Fact] public async Task Token_introspection_revocation_and_userinfo_go_to_the_endpoints_the_discovery_names() { var server = Server(); var client = Client(server); await client.RequestTokenAsync(new TokenRequest { GrantType = "client_credentials", ClientId = "svc", ClientSecret = "secret" }); await client.IntrospectTokenAsync("t"); await client.RevokeOAuthTokenAsync("t"); await client.GetUserInfoAsync(); Calls(server).Should().Equal( $"GET {Discovery}", "POST https://issuer.test/connect/token", "POST https://issuer.test/connect/introspect", "POST https://issuer.test/connect/revocation", "GET https://issuer.test/connect/userinfo"); } [Fact] public async Task A_failed_discovery_read_is_not_kept() { var up = false; var inner = Server(); var server = new FakeHttpMessageHandler(req => up || req.RequestUri!.ToString() != Discovery ? inner.SendForTest(req) : Task.FromResult(FakeHttpMessageHandler.BuildResponse(HttpStatusCode.ServiceUnavailable, null))); var client = Client(server); await FluentActions.Awaiting(() => client.GetJwksAsync()).Should().ThrowAsync(); up = true; (await client.GetJwksAsync()).GetProperty("keys").GetArrayLength().Should().Be(0); } [Fact] public void Without_an_authority_the_options_are_refused_and_the_message_names_the_key() { var services = new ServiceCollection(); services.AddIdentityClient(o => o.BaseUrl = new Uri("http://localhost:5003/")); using var sp = services.BuildServiceProvider(); var act = () => sp.GetRequiredService>().Value; act.Should().Throw().WithMessage("IdentityClientOptions.Authority is required*"); } }