111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
using Flypaper.Core;
|
|
using Flypaper.Core.Settings;
|
|
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, TimeSpan.FromMinutes(10), 90, false));
|
|
|
|
options.CurrentValue.MaxConnections.ShouldBe(50);
|
|
options.CurrentValue.MaxConnectionsPerIp.ShouldBe(4);
|
|
options.CurrentValue.MaxHoldDuration.ShouldBe(TimeSpan.FromMinutes(10));
|
|
options.CurrentValue.IgnorePrivateNetworks.ShouldBeFalse();
|
|
(await store.HasOverridesAsync()).ShouldBeTrue();
|
|
|
|
await store.ResetAsync();
|
|
|
|
options.CurrentValue.MaxConnections.ShouldBe(100);
|
|
options.CurrentValue.IgnorePrivateNetworks.ShouldBeTrue();
|
|
(await store.HasOverridesAsync()).ShouldBeFalse();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|