22// Licensed under the MIT License.
33
44using System . Net . Http . Headers ;
5+ using Microsoft . AspNetCore . Hosting ;
56using Microsoft . AspNetCore . Mvc . Testing ;
7+ using Microsoft . Extensions . Configuration ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using Microsoft . Identity . Abstractions ;
10+ using Microsoft . Identity . Web ;
611using Microsoft . Identity . Web . Sidecar ;
12+ using Microsoft . Identity . Web . TokenCacheProviders . InMemory ;
713using Xunit ;
814
915namespace Sidecar . Tests ;
1016
1117public 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}
0 commit comments