using Flypaper.Core; using Flypaper.Core.Settings; using Flypaper.Core.Strategies; using Flypaper.Http; using Flypaper.Storage; using Flypaper.Storage.Entities; using Flypaper.Storage.Maintenance; using Flypaper.Storage.Settings; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Shouldly; namespace Flypaper.Tests.Storage; public sealed class SettingsOverrideTests : IDisposable { private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-test-{Guid.NewGuid():N}.db"); private IHost? _host; [Fact] public async Task Db_overrides_startup_values_for_whitelisted_keys_only() { await using (var db = new FlypaperDbContext(new DbContextOptionsBuilder().UseSqlite($"Data Source={_dbPath}").Options)) { await db.Database.MigrateAsync(); db.Settings.AddRange( new SettingEntity { Key = SettingKeys.MaxConnections, Value = "5" }, new SettingEntity { Key = SettingKeys.RetentionDays, Value = "30" }, new SettingEntity { Key = "Flypaper:Http:TrustedProxies:0", Value = "203.0.113.0/24" }, new SettingEntity { Key = "Flypaper:Admin:Password", Value = "evil" }); await db.SaveChangesAsync(); } var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var core = host.Services.GetRequiredService>().CurrentValue; var configuration = host.Services.GetRequiredService(); core.MaxConnections.ShouldBe(5); core.MaxConnectionsPerIp.ShouldBe(10); host.Services.GetRequiredService>().CurrentValue.Days.ShouldBe(30); host.Services.GetRequiredService>().CurrentValue.TrustedProxies.ShouldBe(["172.18.0.0/16"]); configuration["Flypaper:Admin:Password"].ShouldBe("secret"); } [Fact] public async Task Missing_db_overrides_leave_startup_values_untouched() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); host.Services.GetRequiredService>().CurrentValue.MaxConnections.ShouldBe(100); } [Fact] public async Task Saving_reloads_options_and_reset_restores_startup_values() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var options = host.Services.GetRequiredService>(); var store = host.Services.GetRequiredService(); await store.SaveAsync(new RuntimeSettings(50, 4, 90, false)); options.CurrentValue.MaxConnections.ShouldBe(50); options.CurrentValue.MaxConnectionsPerIp.ShouldBe(4); options.CurrentValue.IgnorePrivateNetworks.ShouldBeFalse(); (await store.HasOverridesAsync()).ShouldBeTrue(); await store.ResetAsync(); options.CurrentValue.MaxConnections.ShouldBe(100); options.CurrentValue.IgnorePrivateNetworks.ShouldBeTrue(); (await store.HasOverridesAsync()).ShouldBeFalse(); } [Fact] public async Task Hold_method_is_saved_and_reset_separately_from_general_settings() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var core = host.Services.GetRequiredService>(); var drip = host.Services.GetRequiredService>(); var store = host.Services.GetRequiredService(); string[] holdKeys = [.. SettingKeys.HoldMethod]; await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, TimeSpan.FromMinutes(5))); await store.SaveAsync(new RuntimeSettings(50, 4, 90, true)); drip.CurrentValue.Content.ShouldBe(DripContent.Text); drip.CurrentValue.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(200)); drip.CurrentValue.MaxChunkSize.ShouldBe(16); drip.CurrentValue.HeadStartSize.ShouldBe(256); drip.CurrentValue.MatchPath.ShouldBeFalse(); core.CurrentValue.MaxHoldDuration.ShouldBe(TimeSpan.FromMinutes(5)); core.CurrentValue.MaxConnections.ShouldBe(50); (await store.HasOverridesAsync(holdKeys)).ShouldBeTrue(); await store.ResetAsync(holdKeys); drip.CurrentValue.Content.ShouldBe(DripContent.Html); drip.CurrentValue.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(50)); core.CurrentValue.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1)); core.CurrentValue.MaxConnections.ShouldBe(50); (await store.HasOverridesAsync(holdKeys)).ShouldBeFalse(); (await store.HasOverridesAsync()).ShouldBeTrue(); } private IHost BuildHost() { var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { DisableDefaults = true }); builder.Configuration.AddInMemoryCollection(new Dictionary { ["ConnectionStrings:Flypaper"] = $"Data Source={_dbPath}", ["Flypaper:MaxConnections"] = "100", ["Flypaper:MaxConnectionsPerIp"] = "10", ["Flypaper:Http:TrustedProxies:0"] = "172.18.0.0/16", ["Flypaper:Admin:Password"] = "secret" }); builder.AddFlypaperStorage(); builder.Services.AddLogging(); builder.Services.AddFlypaperCore(builder.Configuration); builder.Services.AddFlypaperHttp(builder.Configuration); return _host = builder.Build(); } public void Dispose() { _host?.Dispose(); SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } }