All checks were successful
continuous-integration/drone/push Build is passing
And update all references including comments etc.
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using DotNet.Testcontainers.Images;
|
|
using Microsoft.AspNetCore.Authorization.Policy;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Testcontainers.PostgreSql;
|
|
using Vegasco.Server.Api.Common;
|
|
|
|
namespace Vegasco.Server.Api.Tests.Integration;
|
|
|
|
public sealed class WebAppFactory : WebApplicationFactory<IApiMarker>, IAsyncLifetime
|
|
{
|
|
private readonly PostgreSqlContainer _database = new PostgreSqlBuilder()
|
|
.WithImage(DockerImage)
|
|
.WithImagePullPolicy(PullPolicy.Always)
|
|
.Build();
|
|
|
|
private const string DockerImage = "postgres:alpine";
|
|
|
|
public HttpClient HttpClient => CreateClient();
|
|
|
|
private PostgresRespawner? _postgresRespawner;
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
await _database.StartAsync();
|
|
|
|
// Force application startup (i.e. initialization and validation)
|
|
_ = CreateClient();
|
|
|
|
_postgresRespawner = await PostgresRespawner.CreateAsync(_database.GetConnectionString());
|
|
}
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
IEnumerable<KeyValuePair<string, string?>> customConfig =
|
|
[
|
|
new KeyValuePair<string, string?>($"ConnectionStrings:{AppHost.Shared.Constants.Database.Name}", _database.GetConnectionString()),
|
|
new KeyValuePair<string, string?>("JWT:ValidAudience", "https://localhost"),
|
|
new KeyValuePair<string, string?>("JWT:MetadataUrl", "https://localhost"),
|
|
new KeyValuePair<string, string?>("JWT:NameClaimType", null),
|
|
];
|
|
|
|
builder.UseConfiguration(new ConfigurationBuilder()
|
|
.AddInMemoryCollection(customConfig)
|
|
.Build());
|
|
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
});
|
|
|
|
builder.ConfigureTestServices(services =>
|
|
{
|
|
services.RemoveAll<IPolicyEvaluator>();
|
|
services.AddSingleton<IPolicyEvaluator, TestUserAlwaysAuthorizedPolicyEvaluator>();
|
|
});
|
|
}
|
|
|
|
public async Task ResetDatabaseAsync()
|
|
{
|
|
await _postgresRespawner!.ResetDatabaseAsync();
|
|
}
|
|
|
|
async Task IAsyncLifetime.DisposeAsync()
|
|
{
|
|
_postgresRespawner!.Dispose();
|
|
await _database.DisposeAsync();
|
|
}
|
|
} |