145 lines
5.2 KiB
C#
145 lines
5.2 KiB
C#
using System.Net;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
namespace Elternbeirat.Web.Tests;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// <para>
|
|
/// The post routes now read from PocketBase, so the tests point the web app at the
|
|
/// seeded container from <see cref="PocketBaseFixture"/> (shared via the
|
|
/// collection) and assert against its known slugs. Requires Docker, like the
|
|
/// client tests.
|
|
/// </para>
|
|
/// </summary>
|
|
[Collection(PocketBaseTestGroup.Name)]
|
|
public sealed 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<Program> _baseFactory = new();
|
|
private readonly WebApplicationFactory<Program> _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 = _baseFactory.WithWebHostBuilder(builder =>
|
|
builder.UseSetting("PocketBase:BaseUrl", pocketBase.BaseUrl.ToString()));
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_factory.Dispose();
|
|
_baseFactory.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static TheoryData<string> KnownRoutes =>
|
|
[
|
|
"/",
|
|
"/board",
|
|
"/patrons",
|
|
"/faqs",
|
|
"/faq-mensa",
|
|
"/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")]
|
|
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_grouped_list()
|
|
{
|
|
// /faqs is a literal route (FaqList) that must win over the /{Slug}
|
|
// catch-all content page. Assert it shows the FAQ list, grouped by topic
|
|
// from the seeded questions, not a content page body.
|
|
var client = _factory.CreateClient();
|
|
|
|
var html = await client.GetStringAsync(new Uri("/faqs", UriKind.Relative));
|
|
|
|
html.ShouldContain("Häufige Fragen");
|
|
html.ShouldContain("Wann gibt es Mittagessen?");
|
|
html.ShouldContain("Mensa und Mittagessen"); // German topic heading
|
|
}
|
|
|
|
[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 Home_renders_the_embeds_its_page_opts_into()
|
|
{
|
|
// The home page's embed field is ["posts","events"], so the root route
|
|
// renders the posts teaser and the events teaser below the intro.
|
|
var client = _factory.CreateClient();
|
|
|
|
var html = await client.GetStringAsync(new Uri("/", UriKind.Relative));
|
|
|
|
html.ShouldContain("Aktuelle Beiträge"); // posts embed heading
|
|
html.ShouldContain("Neuer Vorstand"); // a seeded post title
|
|
html.ShouldContain("Kommende Termine"); // events embed heading
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Content_page_renders_the_embed_its_page_opts_into()
|
|
{
|
|
// /patrons is a catch-all content page whose embed field is ["faqs"], so it
|
|
// shows its body plus the FAQ teaser -- proving the ContentPage embed path.
|
|
var client = _factory.CreateClient();
|
|
|
|
var html = await client.GetStringAsync(new Uri("/patrons", UriKind.Relative));
|
|
|
|
html.ShouldContain("Förderverein"); // the page body
|
|
html.ShouldContain("Häufige Fragen"); // faqs embed heading
|
|
html.ShouldContain("Wann gibt es Mittagessen?"); // a seeded question
|
|
}
|
|
}
|