Serve downloadable PDFs from a PocketBase documents collection
- New collection "documents" (title, slug, file, category, order, public); editors upload PDFs in the admin, no developer or redeploy needed - The app serves each file at /dokumente/<slug>.pdf, a URL that survives a new upload (PocketBase's own file name gets a random suffix) - New "documents" embed lists them as file cards, grouped by category; /downloads uses it - List/view rules are "public = true", so a draft PDF is not reachable through the PocketBase API either - Dev seed adds example documents with generated PDFs and fixes the const reassignment that broke seeding a fresh volume - Docs: editor guide, architecture, and the manual production setup steps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
97321e2b49
commit
f1a06956f7
18 files changed
+641
-31
No files matched your search
@@ -117,4 +117,30 @@ public sealed class PocketBaseClientTests(PocketBaseFixture pocketBase)
|
||||
faqs.Select(faq => faq.Topic?.Title).ShouldBe(["Mensa und Mittagessen", "Schließfächer"]);
|
||||
faqs.ShouldAllBe(faq => !string.IsNullOrWhiteSpace(faq.Question));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Documents_load_sorted_by_order_without_drafts()
|
||||
{
|
||||
var client = pocketBase.CreateClient();
|
||||
|
||||
var documents = await client.GetDocumentsAsync();
|
||||
|
||||
// Seeded out of order; the draft "Internes Protokoll" must be missing.
|
||||
documents.Select(document => document.Title)
|
||||
.ShouldBe(["Aufnahmeantrag Förderverein", "Einzugsermächtigung", "Elternbrief"]);
|
||||
documents.ShouldAllBe(document => document.File.EndsWith(".pdf", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Document_file_opens_with_the_uploaded_content()
|
||||
{
|
||||
var client = pocketBase.CreateClient();
|
||||
var document = (await client.GetDocumentsAsync())[0];
|
||||
|
||||
await using var stream = await client.OpenDocumentFileAsync(document);
|
||||
using var buffer = new MemoryStream();
|
||||
await stream.CopyToAsync(buffer);
|
||||
|
||||
buffer.ToArray().ShouldBe(PocketBaseFixture.DocumentContent);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ using Elternbeirat.PocketBase;
|
||||
namespace Elternbeirat.Web.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Starts a throwaway PocketBase container once per test run, creates the four
|
||||
/// Starts a throwaway PocketBase container once per test run, creates the
|
||||
/// content collections and seeds them with a small, known data set. The tests run
|
||||
/// against this instance instead of the live one, so they are hermetic (no network
|
||||
/// to Unraid), reproducible (fixed data) and safe (isolated from production).
|
||||
@@ -194,7 +194,7 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Imports the four content collections from the shared schema file, the same
|
||||
/// Imports the content collections from the shared schema file, the same
|
||||
/// one the dev seed migration reads (<c>pb/pb_migrations/collections_schema.json</c>).
|
||||
/// Uses PocketBase's <c>/api/collections/import</c> so the whole schema is
|
||||
/// defined in one place instead of being typed out here.
|
||||
@@ -270,7 +270,7 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
await CreateRecordAsync(http, "pages", new
|
||||
{
|
||||
title = "Downloads", body = "# Downloads", location = "header",
|
||||
order = 5, slug = "downloads", embed = Array.Empty<string>(), @public = true,
|
||||
order = 5, slug = "downloads", embed = new[] { "documents" }, @public = true,
|
||||
});
|
||||
// Page records for the posts and events lists. Routes /posts and /events are
|
||||
// served by PostList and EventList, which read these records for their heading
|
||||
@@ -388,6 +388,47 @@ public sealed class PocketBaseFixture : IAsyncLifetime
|
||||
question = "Wie viel kostet ein Schließfach?", answer = "20 Euro.",
|
||||
topic = lockerTopicId, order = 1, @public = true,
|
||||
});
|
||||
|
||||
// Documents: created out of order on purpose, so the tests prove the list is
|
||||
// sorted by "order". Two share a category, one has none, and one is a draft
|
||||
// whose file must never be served.
|
||||
await CreateDocumentAsync(http, "Einzugsermächtigung", "einzugsermaechtigung", "Förderverein", 2, true);
|
||||
await CreateDocumentAsync(http, "Aufnahmeantrag Förderverein", "aufnahmeantrag-foerderverein", "Förderverein", 1, true);
|
||||
await CreateDocumentAsync(http, "Elternbrief", "elternbrief", "", 3, true);
|
||||
await CreateDocumentAsync(http, "Internes Protokoll", "internal-minutes", "", 4, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The content of every seeded document's file: the smallest input PocketBase's
|
||||
/// MIME sniffing accepts as <c>application/pdf</c>, so the tests can compare the
|
||||
/// served bytes exactly.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<byte> DocumentContent { get; } = "%PDF-1.4\n%%EOF\n"u8.ToArray();
|
||||
|
||||
private static async Task CreateDocumentAsync(
|
||||
HttpClient http, string title, string slug, string category, int order, bool isPublic)
|
||||
{
|
||||
// A file field takes a multipart upload, not JSON. The multipart content owns
|
||||
// and disposes the parts added to it, which CA2000 cannot see.
|
||||
using var content = new MultipartFormDataContent();
|
||||
#pragma warning disable CA2000
|
||||
foreach (var (name, value) in new[]
|
||||
{
|
||||
("title", title),
|
||||
("slug", slug),
|
||||
("category", category),
|
||||
("order", order.ToString(CultureInfo.InvariantCulture)),
|
||||
("public", isPublic ? "true" : "false"),
|
||||
})
|
||||
content.Add(new StringContent(value), name);
|
||||
content.Add(new ByteArrayContent([.. DocumentContent])
|
||||
{
|
||||
Headers = { ContentType = new MediaTypeHeaderValue("application/pdf") },
|
||||
}, "file", $"{slug}.pdf");
|
||||
#pragma warning restore CA2000
|
||||
|
||||
var response = await http.PostAsync(new Uri("/api/collections/documents/records", UriKind.Relative), content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private static async Task<string> CreateRecordAsync(HttpClient http, string collection, object record)
|
||||
|
||||
@@ -102,6 +102,8 @@ public sealed partial class RouteSmokeTests : IDisposable
|
||||
[InlineData("/gibt-es-nicht")]
|
||||
[InlineData("/posts/gibt-es-nicht")]
|
||||
[InlineData("/events/gibt-es-nicht.ics")]
|
||||
[InlineData("/dokumente/gibt-es-nicht.pdf")]
|
||||
[InlineData("/dokumente/internal-minutes.pdf")] // a draft: exists, but not public
|
||||
public async Task Unknown_route_returns_404(string route)
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
@@ -180,6 +182,37 @@ public sealed partial class RouteSmokeTests : IDisposable
|
||||
html.ShouldContain("Kommende Termine"); // events embed heading
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Downloads_page_lists_the_documents_as_file_cards_by_category()
|
||||
{
|
||||
// /downloads opts into the documents embed. Each public document becomes a
|
||||
// lone PDF link (the file card), grouped under its category; the draft is left out.
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
// Decoded, because Blazor HTML-encodes the umlauts of text from PocketBase.
|
||||
var html = WebUtility.HtmlDecode(await client.GetStringAsync(new Uri("/downloads", UriKind.Relative)));
|
||||
|
||||
// [^>]* allows for the scoped-CSS attribute Blazor adds to each element.
|
||||
html.ShouldMatch("<h2[^>]*>Förderverein</h2>");
|
||||
html.ShouldMatch("""<li class="lone-link"[^>]*><a href="/dokumente/aufnahmeantrag-foerderverein.pdf"[^>]*>Aufnahmeantrag Förderverein</a></li>""");
|
||||
html.ShouldContain("href=\"/dokumente/elternbrief.pdf\"");
|
||||
html.ShouldNotContain("internal-minutes");
|
||||
html.IndexOf("aufnahmeantrag-foerderverein", StringComparison.Ordinal)
|
||||
.ShouldBeLessThan(html.IndexOf("einzugsermaechtigung", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Document_is_served_as_pdf_under_its_slug()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
var response = await client.GetAsync(new Uri("/dokumente/einzugsermaechtigung.pdf", UriKind.Relative));
|
||||
|
||||
response.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||
response.Content.Headers.ContentType?.MediaType.ShouldBe("application/pdf");
|
||||
(await response.Content.ReadAsByteArrayAsync()).ShouldBe(PocketBaseFixture.DocumentContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Content_page_renders_its_body()
|
||||
{
|
||||
@@ -329,6 +362,19 @@ public sealed partial class RouteSmokeTests : IDisposable
|
||||
(await response.Content.ReadAsStringAsync()).ShouldContain("Die Seite ist gerade nicht erreichbar");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Document_returns_503_when_PocketBase_is_unreachable()
|
||||
{
|
||||
// Not a page, so the gate lets it through; without PocketBase there is no file
|
||||
// to hand out, and a 503 says "try again" where a 404 would say "gone".
|
||||
await using var factory = UnreachablePocketBase();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
var response = await client.GetAsync(new Uri("/dokumente/elternbrief.pdf", UriKind.Relative));
|
||||
|
||||
response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Calendar_feed_stays_an_empty_calendar_when_PocketBase_is_unreachable()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user