Skip to content

Commit

Permalink
perf: add user detail tests on OAuth2Introspection sub claim
Browse files Browse the repository at this point in the history
  • Loading branch information
Azaferany committed Sep 3, 2022
1 parent 1f1c2d7 commit 18c62ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
<PackageReference Include="Verify.Xunit" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
75 changes: 71 additions & 4 deletions test/QuickstartTemplate.WebApi.IntegrationTests/UserDetailTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Net.Http.Json;
using System.Security.Claims;
using FluentAssertions;
using IdentityModel.AspNetCore.OAuth2Introspection;
using IdentityModel.Client;
using IdentityModel.Jwk;
using Microsoft.Extensions.DependencyInjection;
using QuickstartTemplate.WebApi.IntegrationTests.Helpers;
using RichardSzalay.MockHttp;

namespace QuickstartTemplate.WebApi.IntegrationTests;

Expand Down Expand Up @@ -40,8 +43,72 @@ public async Task Test_HttpContext_User_Identity_Name_fill_correctly_with_SUB_fr
}

// some mocking like MockJwtTokens needed but i have no idea how do do that.
// [Fact]
// public async Task Test_HttpContext_User_Identity_Name_fill_correctly_with_SUB_from_OAuth2_Introspection_token()
// {
// }
[Fact]
public async Task Test_HttpContext_User_Identity_Name_fill_correctly_with_SUB_from_OAuth2_Introspection_token()
{
var userId = "12345";

var factory = _customWebApplicationFactory.WithWebHostBuilder(builder =>
builder.ConfigureServices(services =>
{
services.AddControllers()
.AddApplicationPart(GetType().Assembly);

services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
.ConfigurePrimaryHttpMessageHandler(_ =>
{
var mockHttp = new MockHttpMessageHandler();

mockHttp.When($"*/introspect")
.Respond(JsonContent.Create(new
{
iss = "https://demo.duendesoftware.com",
client_id = "PayPingWebApp",
sub = userId,
name = "TestName",
active = true,
scope =
"QuickstartTemplate:admin QuickstartTemplate:read QuickstartTemplate:write"
}));

mockHttp.When($"*/openid-configuration")
.Respond(JsonContent.Create(new
{
issuer = "https://demo.duendesoftware.com",
jwks_uri =
"https://demo.duendesoftware.com/.well-known/openid-configuration/jwks",
authorization_endpoint =
"https://demo.duendesoftware.com/connect/authorize",
token_endpoint = "https://demo.duendesoftware.com/connect/token",
userinfo_endpoint = "https://demo.duendesoftware.com/connect/userinfo",
introspection_endpoint =
"https://demo.duendesoftware.com/connect/introspect",
scopes_supported = new List<string>()
{
"QuickstartTemplate:admin",
"QuickstartTemplate:read",
"QuickstartTemplate:write"
},
claims_supported = new List<string>() { "sub", "name" }
}));
mockHttp.When("*/jwks").Respond(JsonContent.Create(new JsonWebKeySet
{
Keys = new List<JsonWebKey>() { new JsonWebKey() { Alg = "test" } }
}));
return mockHttp;
});
}
));

var client = factory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "v1/Test/UserDetail");

request.SetBearerToken("testtoken");

var response = await client.SendAsync(request);

var readFromString = await response.Content.ReadFromJsonAsync<string>();

readFromString.Should().BeEquivalentTo(userId);
}
}

0 comments on commit 18c62ce

Please sign in to comment.