2024-11-14 15:17:43 +05:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-11-21 11:41:53 +05:00
|
|
|
|
using Persistence.API;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
using Persistence.Database.Model;
|
2024-11-18 14:22:09 +05:00
|
|
|
|
using Persistence.Database.Postgres;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
using Refit;
|
2024-11-21 11:41:53 +05:00
|
|
|
|
using RestSharp;
|
2024-11-20 16:08:16 +05:00
|
|
|
|
using System.Net.Http.Headers;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
using System.Text.Json;
|
2024-11-18 11:32:57 +05:00
|
|
|
|
using Persistence.Database.Postgres;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
|
|
|
|
namespace Persistence.IntegrationTests;
|
|
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
|
|
|
|
{
|
|
|
|
|
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
//Converters = { new ValidationResultConverter() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
|
|
|
|
|
|
|
|
|
private readonly string connectionString;
|
2024-11-21 11:41:53 +05:00
|
|
|
|
private readonly KeyCloakUser keycloakTestUser;
|
|
|
|
|
public readonly string KeycloakGetTokenUrl;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
|
|
|
|
public WebAppFactoryFixture()
|
|
|
|
|
{
|
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile("appsettings.Tests.json")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
|
|
|
|
|
connectionString = dbConnection.GetConnectionString();
|
2024-11-21 11:41:53 +05:00
|
|
|
|
|
|
|
|
|
keycloakTestUser = configuration.GetSection("KeycloakTestUser").Get<KeyCloakUser>()!;
|
|
|
|
|
|
|
|
|
|
KeycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Value!;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.ConfigureServices(services =>
|
|
|
|
|
{
|
|
|
|
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<PersistenceDbContext>));
|
|
|
|
|
|
|
|
|
|
if (descriptor != null)
|
|
|
|
|
services.Remove(descriptor);
|
|
|
|
|
|
|
|
|
|
services.AddDbContext<PersistenceDbContext>(options =>
|
|
|
|
|
options.UseNpgsql(connectionString));
|
|
|
|
|
|
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
|
var scopedServices = scope.ServiceProvider;
|
|
|
|
|
var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>();
|
|
|
|
|
|
2024-11-18 11:32:57 +05:00
|
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
2024-11-14 15:17:43 +05:00
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
var dbContext = new PersistenceDbContext(
|
|
|
|
|
new DbContextOptionsBuilder<PersistenceDbContext>()
|
|
|
|
|
.UseNpgsql(connectionString)
|
|
|
|
|
.Options);
|
|
|
|
|
|
|
|
|
|
await dbContext.Database.EnsureDeletedAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetHttpClient<T>(string uriSuffix)
|
|
|
|
|
{
|
|
|
|
|
var httpClient = CreateClient();
|
|
|
|
|
if (string.IsNullOrEmpty(uriSuffix))
|
|
|
|
|
return RestService.For<T>(httpClient, RefitSettings);
|
|
|
|
|
|
|
|
|
|
if (httpClient.BaseAddress is not null)
|
|
|
|
|
httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix);
|
|
|
|
|
|
|
|
|
|
return RestService.For<T>(httpClient, RefitSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 11:41:53 +05:00
|
|
|
|
public async Task<T> GetAuthorizedHttpClient<T>(string uriSuffix)
|
2024-11-20 16:08:16 +05:00
|
|
|
|
{
|
2024-11-21 11:41:53 +05:00
|
|
|
|
var httpClient = await GetAuthorizedHttpClient();
|
2024-11-20 16:08:16 +05:00
|
|
|
|
if (string.IsNullOrEmpty(uriSuffix))
|
|
|
|
|
return RestService.For<T>(httpClient, RefitSettings);
|
|
|
|
|
|
|
|
|
|
if (httpClient.BaseAddress is not null)
|
|
|
|
|
httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix);
|
|
|
|
|
|
|
|
|
|
return RestService.For<T>(httpClient, RefitSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 11:41:53 +05:00
|
|
|
|
private async Task<HttpClient> GetAuthorizedHttpClient()
|
2024-11-20 16:08:16 +05:00
|
|
|
|
{
|
|
|
|
|
var httpClient = CreateClient();
|
2024-11-21 11:41:53 +05:00
|
|
|
|
var token = await GetTokenAsync();
|
|
|
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
|
|
2024-11-20 16:08:16 +05:00
|
|
|
|
return httpClient;
|
|
|
|
|
}
|
2024-11-21 11:41:53 +05:00
|
|
|
|
|
|
|
|
|
private async Task<string> 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<JwtToken>(keyCloackResponse.Content)!;
|
|
|
|
|
return token.AccessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
2024-11-14 15:17:43 +05:00
|
|
|
|
}
|