2023-12-29 11:46:17 +05:00
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-12-29 16:08:25 +05:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-12-29 11:46:17 +05:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-01-19 17:49:09 +05:00
|
|
|
using Refit;
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Text.Json;
|
2024-03-11 15:16:07 +05:00
|
|
|
using AsbCloudDb;
|
2024-02-09 11:24:55 +05:00
|
|
|
using AsbCloudWebApi.IntegrationTests.Converters;
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.IntegrationTests;
|
|
|
|
|
2024-03-13 16:34:51 +05:00
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
2023-12-29 11:46:17 +05:00
|
|
|
{
|
2024-03-11 15:16:07 +05:00
|
|
|
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
PropertyNameCaseInsensitive = true,
|
2024-02-09 11:24:55 +05:00
|
|
|
Converters = { new ValidationResultConverter() }
|
2024-03-11 15:16:07 +05:00
|
|
|
};
|
2024-01-19 17:49:09 +05:00
|
|
|
|
2024-03-11 15:16:07 +05:00
|
|
|
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
2024-01-19 17:49:09 +05:00
|
|
|
|
2024-03-13 16:34:51 +05:00
|
|
|
private readonly string connectionString;
|
|
|
|
|
|
|
|
public WebAppFactoryFixture()
|
2023-12-29 11:46:17 +05:00
|
|
|
{
|
2023-12-29 16:08:25 +05:00
|
|
|
var configuration = new ConfigurationBuilder()
|
2024-03-11 15:16:07 +05:00
|
|
|
.AddJsonFile("appsettings.Tests.json")
|
2023-12-29 16:08:25 +05:00
|
|
|
.Build();
|
2024-03-13 16:34:51 +05:00
|
|
|
|
|
|
|
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
|
|
|
|
connectionString = dbConnection.GetConnectionString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
{
|
2024-03-11 15:16:07 +05:00
|
|
|
builder.ConfigureServices(services =>
|
2023-12-29 11:46:17 +05:00
|
|
|
{
|
2024-03-11 15:16:07 +05:00
|
|
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>));
|
2023-12-29 11:46:17 +05:00
|
|
|
|
2024-03-11 15:16:07 +05:00
|
|
|
if (descriptor != null)
|
2023-12-29 11:46:17 +05:00
|
|
|
services.Remove(descriptor);
|
|
|
|
|
|
|
|
services.AddDbContext<AsbCloudDbContext>(options =>
|
|
|
|
options.UseNpgsql(connectionString));
|
2024-03-11 15:16:07 +05:00
|
|
|
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
var scopedServices = scope.ServiceProvider;
|
|
|
|
var dbContext = scopedServices.GetRequiredService<AsbCloudDbContext>();
|
|
|
|
|
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
|
|
|
dbContext.Deposits.AddRange(Data.Defaults.Deposits);
|
|
|
|
dbContext.SaveChanges();
|
2023-12-29 11:46:17 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-13 16:34:51 +05:00
|
|
|
public override async ValueTask DisposeAsync()
|
2023-12-29 11:46:17 +05:00
|
|
|
{
|
2024-03-13 16:34:51 +05:00
|
|
|
var dbContext = new AsbCloudDbContext(
|
|
|
|
new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
|
|
.UseNpgsql(connectionString)
|
|
|
|
.Options);
|
2024-03-11 15:16:07 +05:00
|
|
|
|
2024-03-13 16:34:51 +05:00
|
|
|
await dbContext.Database.EnsureDeletedAsync();
|
2023-12-29 11:46:17 +05:00
|
|
|
}
|
|
|
|
|
2024-03-11 15:16:07 +05:00
|
|
|
public T GetAuthorizedHttpClient<T>(string uriSuffix)
|
2023-12-29 11:46:17 +05:00
|
|
|
{
|
2024-03-11 15:16:07 +05:00
|
|
|
var httpClient = GetAuthorizedHttpClient();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private HttpClient GetAuthorizedHttpClient()
|
|
|
|
{
|
|
|
|
var httpClient = CreateClient();
|
|
|
|
var jwtToken = ApiTokenHelper.GetAdminUserToken();
|
|
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
|
|
|
return httpClient;
|
|
|
|
}
|
2023-12-29 11:46:17 +05:00
|
|
|
}
|