using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudWebApi.IntegrationTests.TestFakers; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using ProtoBuf.Serializers; using Refit; using System.Net.Http.Headers; using System.Text.Json; using Xunit; namespace AsbCloudWebApi.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory, IAsyncLifetime { private static readonly JsonSerializerOptions jsonSerializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true }; private static readonly RefitSettings refitSettings = new RefitSettings(new SystemTextJsonContentSerializer(jsonSerializerOptions)); protected override void ConfigureWebHost(IWebHostBuilder builder) { var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); var connectionString = configuration.GetConnectionString("TestConnection")!; builder.ConfigureServices(services => { var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(DbContextOptions)); if (descriptor is not null) services.Remove(descriptor); services.AddDbContext(options => options.UseNpgsql(connectionString)); }); } public async Task InitializeAsync() { using var scope = Services.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureDeleted(); dbContext.Database.EnsureCreatedAndMigrated(); await FillBaseDatasets(dbContext); } private static async Task FillBaseDatasets(AsbCloudDbContext dbContext) { dbContext.AddRange(Data.Defaults.Deposits); dbContext.AddRange(Data.Defaults.Clusters); dbContext.AddRange(Data.Defaults.Wells); dbContext.AddRange(Data.Defaults.RelationsCompanyWell); await dbContext.SaveChangesAsync(); } public new async Task DisposeAsync() { using var scope = Services.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); await dbContext.Database.EnsureDeletedAsync(); } public HttpClient GetAuthorizedHttpClient() { var httpClient = CreateClient(); var jwtToken = ApiTokenHelper.GetAdminUserToken(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); return httpClient; } public T GetAuthorizedHttpClient() { var httpClient = GetAuthorizedHttpClient(); return RestService.For(httpClient, refitSettings); } }