From 098c180b12b0881f910035d34d4cdf4347af8d9d Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 21 Nov 2024 11:41:53 +0500 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B2=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Persistence.API/appsettings.Tests.json | 9 +++- Persistence.API/appsettings.json | 6 +-- .../Controllers/DataSaubControllerTest.cs | 2 +- .../TimeSeriesBaseControllerTest.cs | 5 ++- Persistence.IntegrationTests/JwtToken.cs | 8 ++++ Persistence.IntegrationTests/KeyCloakUser.cs | 27 ++++++++++++ .../Persistence.IntegrationTests.csproj | 1 + .../WebAppFactoryFixture.cs | 43 +++++++++++++++---- 8 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 Persistence.IntegrationTests/JwtToken.cs create mode 100644 Persistence.IntegrationTests/KeyCloakUser.cs 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 025492a..0e15a60 100644 --- a/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/DataSaubControllerTest.cs @@ -11,7 +11,7 @@ public class DataSaubControllerTest : TimeSeriesBaseControllerTest : BaseIntegrat { dbContext.CleanupDbSet(); - client = factory.GetAuthorizedHttpClient>(string.Empty); + Task.Run(async () => + { + client = await factory.GetAuthorizedHttpClient>(string.Empty); + }).Wait(); } public async Task InsertRangeSuccess(TDto dto) diff --git a/Persistence.IntegrationTests/JwtToken.cs b/Persistence.IntegrationTests/JwtToken.cs new file mode 100644 index 0000000..38bf315 --- /dev/null +++ b/Persistence.IntegrationTests/JwtToken.cs @@ -0,0 +1,8 @@ +using System.Text.Json.Serialization; + +namespace Persistence.IntegrationTests; +public class JwtToken +{ + [JsonPropertyName("access_token")] + public required string AccessToken { get; set; } +} diff --git a/Persistence.IntegrationTests/KeyCloakUser.cs b/Persistence.IntegrationTests/KeyCloakUser.cs new file mode 100644 index 0000000..aa4d335 --- /dev/null +++ b/Persistence.IntegrationTests/KeyCloakUser.cs @@ -0,0 +1,27 @@ +namespace Persistence.IntegrationTests; + +/// +/// настройки 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 912eeda..f36e78d 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 62489e3..8ac7bf2 100644 --- a/Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -3,12 +3,13 @@ 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 System.Text.Json; +using RestSharp; using System.Net.Http.Headers; +using System.Text.Json; namespace Persistence.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory @@ -23,6 +24,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() { @@ -32,6 +35,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) @@ -53,7 +60,6 @@ public class WebAppFactoryFixture : WebApplicationFactory var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureCreatedAndMigrated(); - //dbContext.Deposits.AddRange(Data.Defaults.Deposits); dbContext.SaveChanges(); }); } @@ -80,9 +86,9 @@ public class WebAppFactoryFixture : WebApplicationFactory return RestService.For(httpClient, RefitSettings); } - public T GetAuthorizedHttpClient(string uriSuffix) + public async Task GetAuthorizedHttpClient(string uriSuffix) { - var httpClient = GetAuthorizedHttpClient(); + var httpClient = await GetAuthorizedHttpClient(); if (string.IsNullOrEmpty(uriSuffix)) return RestService.For(httpClient, RefitSettings); @@ -92,11 +98,32 @@ public class WebAppFactoryFixture : WebApplicationFactory return RestService.For(httpClient, RefitSettings); } - private HttpClient GetAuthorizedHttpClient() + private async Task GetAuthorizedHttpClient() { var httpClient = CreateClient(); - ////var jwtToken = ApiTokenHelper.GetAdminUserToken(); - //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); + 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; + } }