Files
Flypaper/test/Flypaper.Tests/Storage/SettingsOverrideTests.cs
T
TomandClaude Sonnet 5.5 c5167262b9
Build and deploy / build (push) Successful in 1m34s
Build and deploy / deploy (push) Successful in 20s
Remove the unused content choice and the plain-text hold method
The link labyrinth held every catch that mattered, text-drip was never
used. Flypaper__Drip__Content now only triggers the removed-settings warning.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 14:21:54 +01:00

147 lines
6.2 KiB
C#

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<FlypaperDbContext>().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<IOptionsMonitor<FlypaperOptions>>().CurrentValue;
var configuration = host.Services.GetRequiredService<IConfiguration>();
core.MaxConnections.ShouldBe(5);
core.MaxConnectionsPerIp.ShouldBe(10);
host.Services.GetRequiredService<IOptionsMonitor<RetentionOptions>>().CurrentValue.Days.ShouldBe(30);
host.Services.GetRequiredService<IOptionsMonitor<HttpTrapOptions>>().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<IOptionsMonitor<FlypaperOptions>>().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<IOptionsMonitor<FlypaperOptions>>();
var store = host.Services.GetRequiredService<RuntimeSettingsStore>();
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<IOptionsMonitor<FlypaperOptions>>();
var drip = host.Services.GetRequiredService<IOptionsMonitor<DripOptions>>();
var store = host.Services.GetRequiredService<RuntimeSettingsStore>();
string[] holdKeys = [.. SettingKeys.HoldMethod];
await store.SaveAsync(new HoldMethodSettings(TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "/trap-a/", TimeSpan.FromMinutes(5), 7, TimeSpan.FromHours(24)));
await store.SaveAsync(new RuntimeSettings(50, 4, 90, true));
drip.CurrentValue.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(200));
drip.CurrentValue.MaxChunkSize.ShouldBe(16);
drip.CurrentValue.HeadStartSize.ShouldBe(256);
drip.CurrentValue.MatchPath.ShouldBeFalse();
drip.CurrentValue.LinkSpacing.ShouldBe(0);
drip.CurrentValue.BaitPaths.ShouldBe("/trap-a/");
core.CurrentValue.MaxHoldDuration.ShouldBe(TimeSpan.FromMinutes(5));
core.CurrentValue.MaxConnections.ShouldBe(50);
(await store.HasOverridesAsync(holdKeys)).ShouldBeTrue();
await store.SaveAsync(new HoldMethodSettings(TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "", TimeSpan.FromMinutes(5), 0, TimeSpan.FromHours(2)));
drip.CurrentValue.BaitPaths.ShouldBe("");
await store.ResetAsync(holdKeys);
drip.CurrentValue.BaitPaths.ShouldBe(BaitPaths.Default);
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<string, string?>
{
["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);
}
}
}