1- using Newtonsoft . Json ;
1+ using Microsoft . AspNetCore . Authentication ;
2+ using Microsoft . AspNetCore . TestHost ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using Microsoft . Extensions . Logging ;
5+ using Microsoft . Extensions . Options ;
6+ using Newtonsoft . Json ;
27using System ;
38using System . Collections . Generic ;
49using System . Linq ;
10+ using System . Net . Http . Headers ;
511using System . Net . Http . Json ;
12+ using System . Security . Claims ;
13+ using System . Text . Encodings . Web ;
614using System . Threading . Tasks ;
715using Xero . Demo . Api . Domain ;
816using Xero . Demo . Api . Domain . Models ;
1220
1321namespace Xero . Demo . Api . Tests . EndpointTests . IntegrationTests
1422{
23+ public class TestAuthHandler : AuthenticationHandler < AuthenticationSchemeOptions >
24+ {
25+ public TestAuthHandler ( IOptionsMonitor < AuthenticationSchemeOptions > options , ILoggerFactory logger , UrlEncoder encoder , ISystemClock clock )
26+ : base ( options , logger , encoder , clock ) { }
27+
28+ protected override Task < AuthenticateResult > HandleAuthenticateAsync ( )
29+ {
30+ var claims = new [ ] { new Claim ( ClaimTypes . Role , CONSTANTS . Roles . Reader ) } ;
31+ var identity = new ClaimsIdentity ( claims , "Test" ) ;
32+ var principal = new ClaimsPrincipal ( identity ) ;
33+ var ticket = new AuthenticationTicket ( principal , "Test" ) ;
34+
35+ var result = AuthenticateResult . Success ( ticket ) ;
36+
37+ return Task . FromResult ( result ) ;
38+ }
39+ }
40+
1541 [ Trait ( "Category" , "Integration" ) ]
1642 public class ProductControllerTest : IDisposable
1743 {
@@ -28,7 +54,17 @@ public ProductControllerTest()
2854 public async Task GetAsync_Returns_200 ( string culture , string version )
2955 {
3056 // Given
31- var client = factory . CreateClient ( ) ;
57+ var client = factory . WithWebHostBuilder ( builder =>
58+ {
59+ builder . ConfigureTestServices ( services =>
60+ {
61+ services . AddAuthentication ( "Test" )
62+ . AddScheme < AuthenticationSchemeOptions , TestAuthHandler > (
63+ "Test" , options => { } ) ;
64+ } ) ;
65+ } )
66+ . CreateClient ( ) ;
67+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Test" ) ;
3268
3369 // When
3470 var response = await client . GetAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) ) ;
0 commit comments