51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Server.WatchBody.Web;
|
|
using Server.WatchBody.Web.Config;
|
|
using Server.WatchBody.Web.Db;
|
|
using Xunit;
|
|
|
|
namespace Server.WatchBody.Web.Tests;
|
|
|
|
/// <summary>
|
|
/// Общий экземпляр приложения для всех интеграционных тестов: тестовая
|
|
/// БД — docker watchbody-pg (порт 5433), данные идемпотентны по
|
|
/// фиксированным uuid/ключам.
|
|
/// </summary>
|
|
public class TestApp : IDisposable
|
|
{
|
|
public const string IvanKey = "test-key-ivan-0001";
|
|
public const string PetrKey = "test-key-petr-0002";
|
|
public const string AnnaKey = "test-key-anna-0003";
|
|
|
|
private readonly WebApplication _app;
|
|
|
|
public HttpClient Client { get; }
|
|
public ServerConfig Config { get; }
|
|
|
|
public TestApp()
|
|
{
|
|
var configPath = Path.Combine(AppContext.BaseDirectory, "appsettings.test.json");
|
|
Config = ServerConfig.Load(configPath);
|
|
var cs = new PostgresConnectionString(Config.DbConnectionString);
|
|
WebAppStartup.Initialize(cs, Config);
|
|
|
|
var builder = WebApplication.CreateBuilder([]);
|
|
builder.WebHost.UseSetting("urls", "http://127.0.0.1:0");
|
|
_app = WebAppStartup.Build(builder, Config, cs);
|
|
_app.StartAsync().Wait();
|
|
|
|
var url = _app.Urls.First(u => u.StartsWith("http://"));
|
|
Client = new HttpClient { BaseAddress = new Uri(url) };
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Client.Dispose();
|
|
_app.StopAsync().Wait();
|
|
_app.DisposeAsync().AsTask().Wait();
|
|
}
|
|
}
|
|
|
|
[CollectionDefinition("web")]
|
|
public class WebTestCollection : ICollectionFixture<TestApp>;
|