using System.Net; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Data.Sqlite; using Shouldly; namespace Flypaper.Tests.Web; public sealed class HealthEndpointTests : IDisposable { private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-health-{Guid.NewGuid():N}.db"); private readonly WebApplicationFactory _factory; public HealthEndpointTests() { _factory = new WebApplicationFactory().WithWebHostBuilder(builder => { builder.UseSetting("ConnectionStrings:Flypaper", $"Data Source={_dbPath}"); builder.UseSetting("Flypaper:Http:KnownHosts:0", "localhost"); }); } [Fact] public async Task Reports_healthy_without_login() { using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); var response = await client.GetAsync("/health"); response.StatusCode.ShouldBe(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()).ShouldBe("healthy"); } [Fact] public async Task Keeps_dashboard_behind_login() { using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); var response = await client.GetAsync("/"); response.StatusCode.ShouldBe(HttpStatusCode.Redirect); response.Headers.Location!.OriginalString.ShouldStartWith("http://localhost/login"); } public void Dispose() { _factory.Dispose(); SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } }