Skip to content

Commit 1034c0e

Browse files
jmprieurkeegan-caruso
authored andcommitted
more end to end tests for sidecar (#3477)
* Adding E2E test * Improving the tests * Fix name * Apply suggestion from @keegan-caruso Co-authored-by: Keegan <[email protected]> --------- Co-authored-by: Keegan <[email protected]>
1 parent 2a53819 commit 1034c0e

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed

src/Microsoft.Identity.Web.AgentIdentities/Microsoft.Identity.Web.AgentIdentities.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<Title>Microsoft Identity Web Agentic Identity support</Title>
55
<Product>Microsoft Identity Web for Agent Identities</Product>
6-
<Description>Helper methods for Agent applications to act as the agent identities.</Description>
6+
<Description>Helper methods for Agent identity blueprint to act as the agent identities.</Description>
77
<PackageReadmeFile>README.md</PackageReadmeFile>
88

99
<!-- The package is new in 3.10.0.-->

src/Microsoft.Identity.Web.Sidecar/Endpoints/ValidateRequestEndpoints.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ public static void AddValidateRequestEndpoints(this WebApplication app)
2323
private static Results<Ok<ValidateAuthorizationHeaderResult>, ProblemHttpResult> ValidateEndpoint(HttpContext httpContext, IConfiguration configuration)
2424
{
2525
string scopeRequiredByApi = configuration["AzureAd:Scopes"] ?? string.Empty;
26-
httpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
27-
26+
if (!string.IsNullOrWhiteSpace(scopeRequiredByApi))
27+
{
28+
httpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
29+
}
2830
var claimsPrincipal = httpContext.User;
2931
var token = claimsPrincipal.GetBootstrapToken() as JsonWebToken;
3032

src/Microsoft.Identity.Web.Sidecar/appsettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
2020
}
2121
],
2222

23-
"EnablePiiLogging": false
23+
"EnablePiiLogging": false,
24+
"AllowWebApiToBeAuthorizedByACL": true,
2425
},
2526

2627
"DownstreamApi": {
@@ -37,7 +38,7 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
3738
"Microsoft.AspNetCore": "Warning"
3839
}
3940
},
40-
"AllowedHosts": "*"
41+
"AllowedHosts": "*",
4142
}
4243

4344

tests/E2E Tests/Sidecar.Tests/Sidecar.Tests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
</PropertyGroup>
1212
<ItemGroup>
13+
<None Remove="appsettings.agentids.json" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<Content Include="appsettings.agentids.json">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</Content>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.9" NoWarn="NU1605" />
1322
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
1423
<PackageReference Include="xunit" Version="2.9.0" />
1524
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />

tests/E2E Tests/Sidecar.Tests/ValidateEndpointTests.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
// Licensed under the MIT License.
33

44
using System.Net.Http.Headers;
5+
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.AspNetCore.Mvc.Testing;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Identity.Abstractions;
10+
using Microsoft.Identity.Web;
611
using Microsoft.Identity.Web.Sidecar;
12+
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
713
using Xunit;
814

915
namespace Sidecar.Tests;
1016

1117
public class SidecarApiFactory : WebApplicationFactory<Program>
1218
{
13-
protected override void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder)
19+
protected override void ConfigureWebHost(IWebHostBuilder builder)
1420
{
21+
builder.ConfigureAppConfiguration(builder =>
22+
{
23+
builder.AddJsonFile(
24+
path: Path.Combine(Directory.GetCurrentDirectory().ToString(), "appsettings.agentids.json"),
25+
optional: false,
26+
reloadOnChange: true);
27+
});
1528
builder.ConfigureServices(services =>
1629
{
1730
});
@@ -34,4 +47,50 @@ public async Task Validate_WhenBadTokenAsync()
3447
var content = await response.Content.ReadAsStringAsync();
3548
Assert.Contains("invalid_token", response.Headers.WwwAuthenticate.ToString(), StringComparison.CurrentCultureIgnoreCase);
3649
}
50+
51+
[Fact]
52+
public async Task Validate_WhenGoodTokenAsync()
53+
{
54+
// Getting a token to call the API.
55+
string authorizationHeader = await GetAuthorizationHeaderToCallTheSideCarAsync();
56+
57+
// Calling the API
58+
var client = _factory.CreateClient();
59+
60+
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);
61+
var response = await client.GetAsync("/Validate");
62+
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
63+
var content = await response.Content.ReadAsStringAsync();
64+
65+
Assert.NotEmpty(content);
66+
}
67+
68+
private static async Task<string> GetAuthorizationHeaderToCallTheSideCarAsync()
69+
{
70+
ServiceCollection services = new();
71+
IConfiguration configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
72+
services.AddSingleton<IConfiguration>(configuration);
73+
configuration["Instance"] = "https://login.microsoftonline.com/";
74+
configuration["TenantId"] = "31a58c3b-ae9c-4448-9e8f-e9e143e800df";
75+
configuration["ClientId"] = "5cbcd9ff-c994-49ac-87e7-08a93a9c0794";
76+
configuration["SendX5C"] = "true";
77+
configuration["ClientCredentials:0:SourceType"] = "StoreWithDistinguishedName";
78+
configuration["ClientCredentials:0:CertificateStorePath"] = "LocalMachine/My";
79+
configuration["ClientCredentials:0:CertificateDistinguishedName"] = "CN=LabAuth.MSIDLab.com";
80+
81+
services.AddTokenAcquisition().AddHttpClient().AddInMemoryTokenCaches();
82+
services.Configure<MicrosoftIdentityApplicationOptions>(configuration);
83+
IServiceProvider serviceProvider = services.BuildServiceProvider();
84+
85+
IAuthorizationHeaderProvider authorizationHeaderProvider = serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
86+
string authorizationHeader = await authorizationHeaderProvider.CreateAuthorizationHeaderForAppAsync("api://d15884b6-a447-4dd5-a5a5-a668c49f6300/.default",
87+
new AuthorizationHeaderProviderOptions()
88+
{
89+
AcquireTokenOptions = new AcquireTokenOptions()
90+
{
91+
AuthenticationOptionsName = ""
92+
}
93+
});
94+
return authorizationHeader;
95+
}
3796
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/AzureAD/microsoft-identity-web/refs/heads/master/JsonSchemas/microsoft-identity-web.json",
3+
"AzureAd": {
4+
"Instance": "https://login.microsoftonline.com/",
5+
"TenantId": "31a58c3b-ae9c-4448-9e8f-e9e143e800df",
6+
"ClientId": "d15884b6-a447-4dd5-a5a5-a668c49f6300", // Agent application ClientId
7+
"ClientCredentials": [
8+
{
9+
"SourceType": "StoreWithDistinguishedName",
10+
"CertificateStorePath": "LocalMachine/My",
11+
"CertificateDistinguishedName": "CN=LabAuth.MSIDLab.com"
12+
}
13+
],
14+
"Scopes": "",
15+
16+
"Audience": "d15884b6-a447-4dd5-a5a5-a668c49f6300"
17+
},
18+
19+
"DownstreamApis": {
20+
21+
}
22+
}

0 commit comments

Comments
 (0)