1616using Xero . Demo . Api . Domain . Models ;
1717using Xero . Demo . Api . Tests . EndpointTests . UnitTests . V1 . TestData ;
1818using Xero . Demo . Api . Tests . Setup ;
19+ using Xero . Demo . Api . Xero . Demo . Domain . Models ;
1920using Xunit ;
21+ using static Xero . Demo . Api . Domain . Models . CONSTANTS ;
2022
2123namespace Xero . Demo . Api . Tests . EndpointTests . IntegrationTests
2224{
@@ -50,21 +52,14 @@ public ProductControllerTest()
5052
5153 [ Theory ]
5254 [ InlineData ( "en-US" , "1" ) ]
53- //[InlineData("en-US", "2")]
55+ //[InlineData("en-US", "2")] :: TO-DO
5456 public async Task GetAsync_Returns_200 ( string culture , string version )
5557 {
5658 // Given
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" ) ;
59+ var client = factory . CreateClient ( ) ;
60+ var authResponse = await client . PostAsync ( string . Format ( SampleDataV1 . readerLoginEndpoint , culture , version , Roles . Reader ) , null ) ;
61+ var authDetails = JsonConvert . DeserializeObject < AuthenticateResponse > ( await authResponse . Content . ReadAsStringAsync ( ) ) ;
62+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authDetails . Token ) ;
6863
6964 // When
7065 var response = await client . GetAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) ) ;
@@ -75,11 +70,13 @@ public async Task GetAsync_Returns_200(string culture, string version)
7570
7671 [ Theory ]
7772 [ InlineData ( "en-US" , "1" ) ]
78- [ InlineData ( "en-US" , "2" ) ]
7973 public async Task GetByIdAsync_Returns_200 ( string culture , string version )
8074 {
8175 // Given
8276 var client = factory . CreateClient ( ) ;
77+ var authResponse = await client . PostAsync ( string . Format ( SampleDataV1 . readerLoginEndpoint , culture , version , Roles . Reader ) , null ) ;
78+ var authDetails = JsonConvert . DeserializeObject < AuthenticateResponse > ( await authResponse . Content . ReadAsStringAsync ( ) ) ;
79+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , authDetails . Token ) ;
8380 var addProductResponse = await client . PostAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) , SampleDataV1 . Product ) ;
8481 var addedProduct = JsonConvert . DeserializeObject < Product > ( await addProductResponse . Content . ReadAsStringAsync ( ) ) ;
8582 var id = addedProduct . Id ;
@@ -93,11 +90,13 @@ public async Task GetByIdAsync_Returns_200(string culture, string version)
9390
9491 [ Theory ]
9592 [ InlineData ( "en-US" , "1" ) ]
96- [ InlineData ( "en-US" , "2" ) ]
9793 public async Task PostAsync_Returns_201 ( string culture , string version )
9894 {
9995 // Given
10096 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 ) ;
101100
102101 // When
103102 var response = await client . PostAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) , SampleDataV1 . Product ) ;
@@ -108,12 +107,13 @@ public async Task PostAsync_Returns_201(string culture, string version)
108107
109108 [ Theory ]
110109 [ InlineData ( "en-US" , "1" ) ]
111- [ InlineData ( "en-US" , "2" ) ]
112110 public async Task PutAsync_Returns_204 ( string culture , string version )
113111 {
114112 // Given
115113 var client = factory . CreateClient ( ) ;
116- var addResponse = await client . PostAsJsonAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) , SampleDataV1 . Product ) ;
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 ) ;
117117
118118 var productResponse = await client . GetAsync ( string . Format ( SampleDataV1 . productEndpoint , culture , version ) ) ;
119119 var products = JsonConvert . DeserializeObject < List < ProductDTO > > ( await productResponse . Content . ReadAsStringAsync ( ) ) ;
0 commit comments