Files
vigilcare-clinical/VigilCare.WardGateway.Tests/Fixtures/GatewayApiFixture.cs
T

80 lines
3.2 KiB
C#

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
public class GatewayApiFixture : WebApplicationFactory<Program>, IAsyncLifetime
{
public const string TestConnectionString =
"Host=localhost;Port=5437;Database=vigilcare_ward_test;Username=postgres;Password=password";
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.ConfigureAppConfiguration((_, config) =>
{
config.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:GatewayDb"] = TestConnectionString,
["Redis:ConnectionString"] = "localhost:6383,allowAdmin=true",
["RabbitMq:Host"] = "localhost",
["RabbitMq:Port"] = "5675",
["RabbitMq:Username"] = "guest",
["RabbitMq:Password"] = "guest",
["RabbitMq:PagingAckTimeoutMs"] = "5000",
["CentralApi:BaseUrl"] = "http://127.0.0.1:1",
["Gateway:GatewayId"] = "22222222-2222-2222-2222-222222222222",
["Gateway:SiteId"] = "11111111-1111-1111-1111-111111111111",
["Gateway:Department"] = "ICU",
["Gateway:EncounterSyncIntervalMinutes"] = "60",
["Gateway:CentralReachabilityIntervalSeconds"] = "1",
["Gateway:HeartbeatIntervalSeconds"] = "1",
["Gateway:SyncBatchSize"] = "500",
["ApiKey:Gateway"] = "dev-gateway-key-change-in-production",
["Jwt:SigningKey"] = "dev-signing-key-minimum-32-bytes-long!!",
["Jwt:Issuer"] = "vigilcare-gateway",
["Jwt:Audience"] = "vigilcare-dashboard",
});
});
builder.ConfigureServices(services =>
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = TestingAuthHandler.SchemeName;
options.DefaultChallengeScheme = TestingAuthHandler.SchemeName;
})
.AddScheme<AuthenticationSchemeOptions, TestingAuthHandler>(
TestingAuthHandler.SchemeName, _ => { });
});
}
public async Task InitializeAsync()
{
var options = new DbContextOptionsBuilder<GatewayDbContext>()
.UseNpgsql(TestConnectionString)
.Options;
await using (var migrateDb = new GatewayDbContext(options))
{
await migrateDb.Database.MigrateAsync();
await GatewayDbResetHelper.ResetAsync(migrateDb);
}
using var scope = Services.CreateScope();
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
var server = redis.GetServer(redis.GetEndPoints().First());
await server.FlushAllDatabasesAsync();
}
protected override void ConfigureClient(HttpClient client)
{
base.ConfigureClient(client);
client.AsNurse();
}
public new async Task DisposeAsync() => await base.DisposeAsync();
}