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 ;
1+ using Newtonsoft . Json ;
72using System ;
83using System . Collections . Generic ;
94using System . Linq ;
5+ using System . Net . Http ;
106using System . Net . Http . Headers ;
117using System . Net . Http . Json ;
12- using System . Security . Claims ;
13- using System . Text . Encodings . Web ;
148using System . Threading . Tasks ;
159using Xero . Demo . Api . Domain ;
1610using Xero . Demo . Api . Domain . Models ;
2216
2317namespace Xero . Demo . Api . Tests . EndpointTests . IntegrationTests
2418{
25- public class TestAuthHandler : AuthenticationHandler < AuthenticationSchemeOptions >
26- {
27- public TestAuthHandler ( IOptionsMonitor < AuthenticationSchemeOptions > options , ILoggerFactory logger , UrlEncoder encoder , ISystemClock clock )
28- : base ( options , logger , encoder , clock ) { }
29-
30- protected override Task < AuthenticateResult > HandleAuthenticateAsync ( )
31- {
32- var claims = new [ ] { new Claim ( ClaimTypes . Role , CONSTANTS . Roles . Reader ) } ;
33- var identity = new ClaimsIdentity ( claims , "Test" ) ;
34- var principal = new ClaimsPrincipal ( identity ) ;
35- var ticket = new AuthenticationTicket ( principal , "Test" ) ;
36-
37- var result = AuthenticateResult . Success ( ticket ) ;
38-
39- return Task . FromResult ( result ) ;
40- }
41- }
42-
4319 [ Trait ( "Category" , "Integration" ) ]
4420 public class ProductControllerTest : IDisposable
4521 {
@@ -52,7 +28,6 @@ public ProductControllerTest()
5228
5329 [ Theory ]
5430 [ InlineData ( "en-US" , "1" ) ]
55- //[InlineData("en-US", "2")] :: TO-DO
5631 public async Task GetAsync_Returns_200 ( string culture , string version )
5732 {
5833 // Given
@@ -93,10 +68,7 @@ public async Task GetByIdAsync_Returns_200(string culture, string version)
9368 public async Task PostAsync_Returns_201 ( string culture , string version )
9469 {
9570 // Given
96- var client = factory . CreateClient ( ) ;
97- var authResponse = await client . PostAsync ( string . Format ( SampleDataV1 . readerLoginEndpoint , culture , version , Roles . Admin ) , null ) ;
98- var authDetails = JsonConvert . DeserializeObject < AuthenticateResponse > ( await authResponse . Content . ReadAsStringAsync ( ) ) ;
99- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authDetails . Token ) ;
71+ var client = await SetupHttpClient ( Roles . Admin , culture , version ) ;
10072
10173 // When
10274 var response = await client . PostAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) , SampleDataV1 . Product ) ;
@@ -110,13 +82,12 @@ public async Task PostAsync_Returns_201(string culture, string version)
11082 public async Task PutAsync_Returns_204 ( string culture , string version )
11183 {
11284 // Given
113- var client = factory . CreateClient ( ) ;
114- var authResponse = await client . PostAsync ( string . Format ( SampleDataV1 . readerLoginEndpoint , culture , version , Roles . Editor ) , null ) ;
115- var authDetails = JsonConvert . DeserializeObject < AuthenticateResponse > ( await authResponse . Content . ReadAsStringAsync ( ) ) ;
116- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authDetails . Token ) ;
85+ var client = await SetupHttpClient ( Roles . Admin , culture , version ) ;
86+ await client . PostAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) , SampleDataV1 . Product ) ;
11787
11888 var productResponse = await client . GetAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) ) ;
11989 var products = JsonConvert . DeserializeObject < List < ProductDTO > > ( await productResponse . Content . ReadAsStringAsync ( ) ) ;
90+
12091 var id = products . FirstOrDefault ( ) . Id ;
12192 var putRequestPayload = new Product
12293 {
@@ -127,13 +98,24 @@ public async Task PutAsync_Returns_204(string culture, string version)
12798 Description = products . FirstOrDefault ( ) . Description
12899 } ;
129100
101+ client = await SetupHttpClient ( Roles . Reader , culture , version ) ;
102+
130103 // When
131104 var response = await client . PutAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) + $ "/{ id } ", putRequestPayload ) ;
132105
133106 // Then
134107 Assert . Equal ( System . Net . HttpStatusCode . NoContent , response . StatusCode ) ;
135108 }
136109
110+ private async Task < HttpClient > SetupHttpClient ( string role , string culture , string version )
111+ {
112+ var client = factory . CreateClient ( ) ;
113+ var authResponse = await client . PostAsync ( string . Format ( SampleDataV1 . readerLoginEndpoint , culture , version , Roles . Reader ) , null ) ;
114+ var authDetails = JsonConvert . DeserializeObject < AuthenticateResponse > ( await authResponse . Content . ReadAsStringAsync ( ) ) ;
115+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authDetails . Token ) ;
116+ return client ;
117+ }
118+
137119 public void Dispose ( )
138120 {
139121 factory = null ;
0 commit comments