using System.Net; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; namespace Elternbeirat.Web.Tests; /// /// Smoke tests: every known route must return 200, unknown routes must return /// 404. This catches broken content files, renamed slugs or routing regressions /// before a deploy. /// /// The post routes now read from PocketBase, so the tests point the web app at the /// seeded container from (shared via the /// collection) and assert against its known slugs. Requires Docker, like the /// client tests. /// /// [Collection(PocketBaseTestGroup.Name)] public sealed partial class RouteSmokeTests : IDisposable { // WithWebHostBuilder returns a new factory that wraps the base one; both are // disposable, so both are held and disposed to avoid leaking either. private readonly WebApplicationFactory _baseFactory = new(); private readonly WebApplicationFactory _factory; public RouteSmokeTests(PocketBaseFixture pocketBase) { // Point the app's PocketBaseClient at the seeded test container instead of // the value from appsettings. UseSetting (not ConfigureAppConfiguration) // because it wins over appsettings.Development.json, which otherwise pins the // client to localhost:8090 while the test container uses a random port. _factory = WithClock( _baseFactory.WithWebHostBuilder(builder => builder.UseSetting("PocketBase:BaseUrl", pocketBase.BaseUrl.ToString())), BeforeFixtureEvents); } /// /// "Now" for the default factory: a week before the fixture's first event, so /// "upcoming" stays true no matter when the tests run. /// private static readonly DateTimeOffset BeforeFixtureEvents = new(2026, 10, 1, 10, 0, 0, TimeSpan.Zero); /// /// Wraps so the app's clock shows . /// private static WebApplicationFactory WithClock(WebApplicationFactory factory, DateTimeOffset now) => factory.WithWebHostBuilder(builder => builder.ConfigureTestServices(services => services.AddSingleton(new FixedTimeProvider(now)))); public void Dispose() { _factory.Dispose(); _baseFactory.Dispose(); } /// /// Every route that a visitor can reach through the navigation, the FAQ hub /// or the news section. The post slugs match the fixture's seed. /// public static TheoryData KnownRoutes => [.. Routes]; /// /// The that render an HTML page, i.e. all but the /// calendar feed. /// public static TheoryData PageRoutes => [.. Routes.Where(route => !route.EndsWith(".ics", StringComparison.Ordinal))]; private static readonly string[] Routes = [ "/", "/board", "/patrons", "/faqs", "/faq-lunch", "/downloads", "/contact", "/imprint", "/privacy", "/posts", "/posts/new-board", "/posts/new-hall", "/events", "/events.ics", ]; [Theory] [MemberData(nameof(KnownRoutes))] public async Task Known_route_returns_200(string route) { var client = _factory.CreateClient(); var response = await client.GetAsync(new Uri(route, UriKind.Relative)); response.StatusCode.ShouldBe(HttpStatusCode.OK); } [Theory] [InlineData("/gibt-es-nicht")] [InlineData("/posts/gibt-es-nicht")] [InlineData("/events/gibt-es-nicht.ics")] public async Task Unknown_route_returns_404(string route) { var client = _factory.CreateClient(); var response = await client.GetAsync(new Uri(route, UriKind.Relative)); response.StatusCode.ShouldBe(HttpStatusCode.NotFound); } [Fact] public async Task Faqs_route_renders_the_topic_accordion() { // /faqs is a literal route (FaqList) that must win over the /{Slug} // catch-all content page. It reads the "faqs" page for heading and intro // and renders each topic as a collapsible group with its questions nested // inside, so both the topic headings and the questions are in the markup. var client = _factory.CreateClient(); var html = await client.GetStringAsync(new Uri("/faqs", UriKind.Relative)); html.ShouldContain("FAQs"); // heading from the page title html.ShouldContain("Mensa und Mittagessen"); // German topic heading (a group) html.ShouldContain("Wann gibt es Mittagessen?"); // a question nested in the group } [Fact] public async Task Layout_navigation_is_built_from_the_pages() { // The header and footer menus are generated from the "pages" records, not // hard-coded. Assert a header link, a footer link, and that the non-public // draft page never leaks into the menu. var client = _factory.CreateClient(); var html = await client.GetStringAsync(new Uri("/", UriKind.Relative)); html.ShouldContain("href=\"/board\""); // header page html.ShouldContain("href=\"/imprint\""); // footer page html.ShouldNotContain("href=\"/draft\""); // public=false, filtered out } [Fact] public async Task Footer_links_every_public_header_and_footer_page_in_order() { // The footer repeats the header pages as "Schnellzugriff" and then lists the // footer pages, each column in its order field. Only the