@@ -12,54 +12,54 @@ namespace AdvancedTodoList.IntegrationTests.Fixtures;
1212/// </summary>
1313public abstract class EndpointsFixture
1414{
15- protected IConfiguration Configuration { get ; private set ; }
16- protected EndpointsWebApplicationFactory WebApplicationFactory { get ; private set ; }
17- protected IServiceScope ServiceScope { get ; private set ; }
15+ protected IConfiguration Configuration { get ; private set ; }
16+ protected EndpointsWebApplicationFactory WebApplicationFactory { get ; private set ; }
17+ protected IServiceScope ServiceScope { get ; private set ; }
1818
19- public const string TestUserId = "TestUserId" ;
19+ public const string TestUserId = "TestUserId" ;
2020
21- protected HttpClient CreateAuthorizedHttpClient ( string userId = TestUserId )
22- {
23- // Create a valid JWT
24- string strKey = Configuration [ "Auth:AccessToken:SecretKey" ] ! ;
25- SymmetricSecurityKey key = new ( Encoding . UTF8 . GetBytes ( strKey ) ) ;
21+ protected HttpClient CreateAuthorizedHttpClient ( string userId = TestUserId )
22+ {
23+ // Create a valid JWT
24+ string strKey = Configuration [ "Auth:AccessToken:SecretKey" ] ! ;
25+ SymmetricSecurityKey key = new ( Encoding . UTF8 . GetBytes ( strKey ) ) ;
2626
27- List < Claim > authClaims =
28- [
29- new ( JwtRegisteredClaimNames . Sub , userId )
30- ] ;
31- JwtSecurityToken token = new (
32- issuer : Configuration [ "Auth:AccessToken:ValidIssuer" ] ,
33- audience : Configuration [ "Auth:AccessToken:ValidAudience" ] ,
34- expires : DateTime . UtcNow . AddMinutes ( 30 ) ,
35- claims : authClaims ,
36- signingCredentials : new SigningCredentials ( key , SecurityAlgorithms . HmacSha256 )
37- ) ;
27+ List < Claim > authClaims =
28+ [
29+ new ( JwtRegisteredClaimNames . Sub , userId )
30+ ] ;
31+ JwtSecurityToken token = new (
32+ issuer : Configuration [ "Auth:AccessToken:ValidIssuer" ] ,
33+ audience : Configuration [ "Auth:AccessToken:ValidAudience" ] ,
34+ expires : DateTime . UtcNow . AddMinutes ( 30 ) ,
35+ claims : authClaims ,
36+ signingCredentials : new SigningCredentials ( key , SecurityAlgorithms . HmacSha256 )
37+ ) ;
3838
39- string bearerToken = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
39+ string bearerToken = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
4040
41- var client = WebApplicationFactory . CreateClient ( ) ;
42- client . DefaultRequestHeaders . Add ( "Authorization" , $ "Bearer { bearerToken } ") ;
41+ var client = WebApplicationFactory . CreateClient ( ) ;
42+ client . DefaultRequestHeaders . Add ( "Authorization" , $ "Bearer { bearerToken } ") ;
4343
44- return client ;
45- }
44+ return client ;
45+ }
4646
47- [ SetUp ]
48- public void SetUpTest ( )
49- {
50- // Configure web application factory
51- WebApplicationFactory = new EndpointsWebApplicationFactory ( ) ;
52- WebApplicationFactory . Server . PreserveExecutionContext = true ;
47+ [ SetUp ]
48+ public void SetUpTest ( )
49+ {
50+ // Configure web application factory
51+ WebApplicationFactory = new EndpointsWebApplicationFactory ( ) ;
52+ WebApplicationFactory . Server . PreserveExecutionContext = true ;
5353
54- var scopeFactory = WebApplicationFactory . Services . GetService < IServiceScopeFactory > ( ) ! ;
55- ServiceScope = scopeFactory . CreateScope ( ) ;
56- Configuration = ServiceScope . ServiceProvider . GetService < IConfiguration > ( ) ! ;
57- }
54+ var scopeFactory = WebApplicationFactory . Services . GetService < IServiceScopeFactory > ( ) ! ;
55+ ServiceScope = scopeFactory . CreateScope ( ) ;
56+ Configuration = ServiceScope . ServiceProvider . GetService < IConfiguration > ( ) ! ;
57+ }
5858
59- [ TearDown ]
60- public Task TearDownTestAsync ( )
61- {
62- ServiceScope . Dispose ( ) ;
63- return WebApplicationFactory . DisposeAsync ( ) . AsTask ( ) ;
64- }
59+ [ TearDown ]
60+ public async Task TearDownTestAsync ( )
61+ {
62+ ServiceScope . Dispose ( ) ;
63+ await WebApplicationFactory . DisposeAsync ( ) ;
64+ }
6565}
0 commit comments