diff --git a/Persistence.API/appsettings.Tests.json b/Persistence.API/appsettings.Tests.json index c1329f9..033464f 100644 --- a/Persistence.API/appsettings.Tests.json +++ b/Persistence.API/appsettings.Tests.json @@ -4,5 +4,12 @@ "Port": 5432, "Username": "postgres", "Password": "q" - } + }, + "KeycloakTestUser": { + "username": "myuser", + "password": 12345, + "clientId": "webapi", + "grantType": "password" + }, + "KeycloakGetTokenUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/token" } diff --git a/Persistence.API/appsettings.json b/Persistence.API/appsettings.json index 2e0033b..3e1eea7 100644 --- a/Persistence.API/appsettings.json +++ b/Persistence.API/appsettings.json @@ -10,9 +10,9 @@ }, "AllowedHosts": "*", "Authentication": { - "MetadataAddress": "http://localhost:8080/realms/TestRealm/.well-known/openid-configuration", + "MetadataAddress": "http://192.168.0.10:8321/realms/Persistence/.well-known/openid-configuration", "Audience": "account", - "ValidIssuer": "http://localhost:8080/realms/TestRealm", - "AuthorizationUrl": "http://localhost:8080/realms/TestRealm/protocol/openid-connect/auth" + "ValidIssuer": "http://192.168.0.10:8321/realms/Persistence", + "AuthorizationUrl": "http://192.168.0.10:8321/realms/Persistence/protocol/openid-connect/auth" } } diff --git a/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs b/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs index 8708960..38d7ccd 100644 --- a/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs @@ -12,7 +12,7 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest +/// настройки credentials для пользователя в KeyCloak +/// +public class KeyCloakUser +{ + /// + /// + /// + public required string Username { get; set; } + + /// + /// + /// + public required string Password { get; set; } + + /// + /// + /// + public required string ClientId { get; set; } + + /// + /// + /// + public required string GrantType { get; set; } +} diff --git a/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj b/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj index 7d0116e..a5cf35f 100644 --- a/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj +++ b/Persistence.IntegrationTests/Persistence.IntegrationTests.csproj @@ -15,6 +15,7 @@ + all diff --git a/Persistence.IntegrationTests/WebAppFactoryFixture.cs b/Persistence.IntegrationTests/WebAppFactoryFixture.cs index f932b83..d04509f 100644 --- a/Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -3,10 +3,12 @@ using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Persistence.API; using Persistence.Database.Model; using Persistence.Database.Postgres; -using Persistence.API; using Refit; +using RestSharp; +using System.Net.Http.Headers; using System.Text.Json; using Persistence.Database.Postgres; using System.Net.Http.Headers; @@ -26,6 +28,8 @@ public class WebAppFactoryFixture : WebApplicationFactory private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); private readonly string connectionString; + private readonly KeyCloakUser keycloakTestUser; + public readonly string KeycloakGetTokenUrl; public WebAppFactoryFixture() { @@ -35,6 +39,10 @@ public class WebAppFactoryFixture : WebApplicationFactory var dbConnection = configuration.GetSection("DbConnection").Get()!; connectionString = dbConnection.GetConnectionString(); + + keycloakTestUser = configuration.GetSection("KeycloakTestUser").Get()!; + + KeycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Value!; } protected override void ConfigureWebHost(IWebHostBuilder builder) @@ -62,8 +70,70 @@ public class WebAppFactoryFixture : WebApplicationFactory var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureCreatedAndMigrated(); - //dbContext.Deposits.AddRange(Data.Defaults.Deposits); dbContext.SaveChanges(); }); } + + public override async ValueTask DisposeAsync() + { + var dbContext = new PersistenceDbContext( + new DbContextOptionsBuilder() + .UseNpgsql(connectionString) + .Options); + + await dbContext.Database.EnsureDeletedAsync(); + } + + public T GetHttpClient(string uriSuffix) + { + var httpClient = CreateClient(); + if (string.IsNullOrEmpty(uriSuffix)) + return RestService.For(httpClient, RefitSettings); + + if (httpClient.BaseAddress is not null) + httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix); + + return RestService.For(httpClient, RefitSettings); + } + + public async Task GetAuthorizedHttpClient(string uriSuffix) + { + var httpClient = await GetAuthorizedHttpClient(); + if (string.IsNullOrEmpty(uriSuffix)) + return RestService.For(httpClient, RefitSettings); + + if (httpClient.BaseAddress is not null) + httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix); + + return RestService.For(httpClient, RefitSettings); + } + + private async Task GetAuthorizedHttpClient() + { + var httpClient = CreateClient(); + var token = await GetTokenAsync(); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + + return httpClient; + } + + private async Task GetTokenAsync() + { + var restClient = new RestClient(); + + var request = new RestRequest(KeycloakGetTokenUrl, Method.Post); + request.AddParameter("username", keycloakTestUser.Username); + request.AddParameter("password", keycloakTestUser.Password); + request.AddParameter("client_id", keycloakTestUser.ClientId); + request.AddParameter("grant_type", keycloakTestUser.GrantType); + + var keyCloackResponse = await restClient.PostAsync(request); + if (keyCloackResponse.IsSuccessful && !String.IsNullOrEmpty(keyCloackResponse.Content)) + { + var token = JsonSerializer.Deserialize(keyCloackResponse.Content)!; + return token.AccessToken; + } + + return String.Empty; + } }