Files
PnvPanel/backend/tests/PnvPanel.IntegrationTests/TestSupport/PnvPanelWebApplicationFactory.cs
T
Leonid Pershin 8067be3c35
CI / Backend (build + test) (push) Successful in 1m17s
CI / Frontend (lint + typecheck + build) (push) Successful in 35s
Implement rate limiting and enhance authentication flow
- Added rate limiting configuration for authentication endpoints, allowing customizable request limits via environment variables.
- Updated authentication flow to utilize HttpRequest for cookie management, ensuring secure handling of refresh tokens.
- Introduced a new endpoint to retrieve user subscription details.
- Enhanced the handling of Telegram bot token validation to prevent errors with empty tokens.
- Updated the application to serialize enums as strings for better documentation and compatibility with TypeScript.
- Improved test coverage for new features and adjustments in command handlers.
2026-07-02 12:40:23 +03:00

68 lines
2.9 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using PnvPanel.Application.Common.Interfaces;
using Testcontainers.PostgreSql;
using Xunit;
namespace PnvPanel.IntegrationTests.TestSupport;
/// <summary>
/// Реальный Postgres через Testcontainers (не InMemory/Sqlite — нужно проверить Postgres-специфичное
/// поведение: pg_advisory_xact_lock для квоты конфигов, uuid[]/jsonb колонки). Program.cs сам
/// применяет миграции и сидит роли/админа при старте хоста — свежий контейнер становится полностью
/// готовой БД без ручных шагов.
/// </summary>
public sealed class PnvPanelWebApplicationFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
public const string AdminUserName = "test-admin";
public const string AdminPassword = "TestAdmin123!";
private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder()
.WithImage("postgres:17-alpine")
.WithDatabase("pnvpanel")
.WithUsername("pnvpanel")
.WithPassword("pnvpanel")
.Build();
public async Task InitializeAsync()
{
await _postgres.StartAsync();
}
async Task IAsyncLifetime.DisposeAsync()
{
await _postgres.StopAsync();
await base.DisposeAsync();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
builder.ConfigureAppConfiguration((_, config) =>
{
config.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:Default"] = _postgres.GetConnectionString(),
["AdminSeed:Username"] = AdminUserName,
["AdminSeed:Password"] = AdminPassword,
// Пусто — TelegramBotHostedService при пустом токене не стартует (см. Api/Telegram/TelegramBotHostedService.cs).
["Telegram:BotToken"] = "",
// Весь collection делит один TestServer/host — все запросы идут от одного "клиента",
// дефолтный лимит 20/мин быстро исчерпывается. Поднимаем для тестового окружения.
["RateLimiting:AuthPermitLimit"] = "10000",
});
});
builder.ConfigureServices(services =>
{
// Реальной панели 3x-ui в тестах нет — подменяем гейтвей заглушкой.
services.RemoveAll<IXuiPanelGateway>();
services.AddSingleton<IXuiPanelGateway, FakeXuiPanelGateway>();
});
}
}