ASF-injectie 5/5: end-to-end test op de master-host, ASF Website:AuthoredBaseUrl, rooktest-uitbreiding, docs
- ASF appsettings: Website:AuthoredBaseUrl=https://andreaslop.nl, PublicBaseUrl leeg (env). - Api.Tests/AsfInjectionPipelineTests: pagina met markers, echte database; een CMS-wijziging is direct zichtbaar (ETag wijzigt), data-blok == publieke API, 304, sitemap-adres. - smoke-site.sh: onverwerkte CMS-markers op / falen; data-blok moet siteUrl van deze host hebben. - docs: asf-host-injection 'as built'. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -50,7 +50,15 @@
|
||||
"Mode": "PrerenderedPages",
|
||||
// Secure default: every website response carries noindex and /robots.txt disallows all. A
|
||||
// production instance must set Website__NoIndex=false explicitly.
|
||||
"NoIndex": true
|
||||
"NoIndex": true,
|
||||
// The public address of this instance (env: Website__PublicBaseUrl), e.g. https://test.andreaslop.nl on the test
|
||||
// host. Empty = the address the site was built with is served as it is (production).
|
||||
"PublicBaseUrl": "",
|
||||
// The address the website build writes into its pages, sitemap.xml and robots.txt (canonical, og:*, JSON-LD).
|
||||
// When PublicBaseUrl is set to something else, the host replaces it by PublicBaseUrl while serving those files,
|
||||
// so test and production run the same website build. Also used as the data block's siteUrl when
|
||||
// PublicBaseUrl is empty.
|
||||
"AuthoredBaseUrl": "https://andreaslop.nl"
|
||||
},
|
||||
// Mail (invitations, password reset, notices). Mail is OFF while Email:Smtp:Host is empty and every
|
||||
// flow then works as before (copy the link). Enable per environment with environment variables:
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Api.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The whole chain on the master host with Andrea Slop Fotografie's settings: a prerendered page with the website
|
||||
/// build's markers, the real Offerings module over a real database, and the injector. A change made through the
|
||||
/// module's services must show in the served HTML at once (cache invalidation), including the ETag.
|
||||
/// </summary>
|
||||
public sealed class AsfInjectionFactory : AsfVariantFactory, IDisposable
|
||||
{
|
||||
public string WebRoot { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-injection-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
public AsfInjectionFactory() => Directory.CreateDirectory(Path.Combine(WebRoot, "web"));
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
builder.UseWebRoot(WebRoot);
|
||||
builder.UseSetting("Website:PublicBaseUrl", "https://test.andreaslop.nl");
|
||||
builder.ConfigureAppConfiguration((_, config) =>
|
||||
config.AddInMemoryCollection(new Dictionary<string, string?> { ["Website:PublicBaseUrl"] = "https://test.andreaslop.nl" }));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
if (disposing && Directory.Exists(WebRoot)) Directory.Delete(WebRoot, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
public class AsfInjectionPipelineTests : IClassFixture<AsfInjectionFactory>
|
||||
{
|
||||
private const string Page =
|
||||
"<!doctype html><html><head>" +
|
||||
"<link rel=\"canonical\" href=\"https://andreaslop.nl/soorten-fotoshoots/smoke\">" +
|
||||
"<!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script>" +
|
||||
"<!--CMS:OFFER-JSON-LD slug=\"smoke\"--><script type=\"application/ld+json\">{\"@type\":\"Service\",\"offers\":{\"@type\":\"Offer\",\"price\":1,\"priceCurrency\":\"EUR\"}}</script>" +
|
||||
"</head><body><!--CMS:BANNER-TOP-HTML-->baked top<!--/CMS:BANNER-TOP-HTML-->" +
|
||||
"<!--CMS:OFFERINGS-HTML-->baked list<!--/CMS:OFFERINGS-HTML-->" +
|
||||
"<p>vanaf <span data-cms-price=\"smoke\">€ 1</span></p></body></html>";
|
||||
|
||||
private readonly AsfInjectionFactory _factory;
|
||||
|
||||
public AsfInjectionPipelineTests(AsfInjectionFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
File.WriteAllText(Path.Combine(factory.WebRoot, "web", "smoke.html"), Page);
|
||||
File.WriteAllText(Path.Combine(factory.WebRoot, "web", "sitemap.xml"), "<urlset><url><loc>https://andreaslop.nl/</loc></url></urlset>");
|
||||
}
|
||||
|
||||
private async Task<T> InScope<T>(Func<IServiceProvider, Task<T>> action)
|
||||
{
|
||||
_ = _factory.CreateClient(); // starts the host and migrates the database
|
||||
await using var scope = _factory.Services.CreateAsyncScope();
|
||||
return await action(scope.ServiceProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ACmsChange_ShowsInTheServedPage_AtOnce_AndTheAddressIsTheTestHosts()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
var caller = Guid.NewGuid();
|
||||
|
||||
// Clean slate for the slug of this test (the database is shared with other tests).
|
||||
await InScope(async sp =>
|
||||
{
|
||||
var service = sp.GetRequiredService<IOfferingsService>();
|
||||
foreach (var existing in (await service.ListAllAsync()).Where(o => o.Slug == "smoke")) await service.DeleteAsync(Guid.Parse(existing.Id), caller);
|
||||
return 0;
|
||||
});
|
||||
|
||||
// Nothing live: regions are emptied, the data block has no package, the price element is gone, the address is the host's.
|
||||
var empty = await client.GetAsync("/smoke");
|
||||
empty.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var emptyBody = await empty.Content.ReadAsStringAsync();
|
||||
emptyBody.Should().Contain("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->");
|
||||
emptyBody.Should().Contain("https://test.andreaslop.nl/soorten-fotoshoots/smoke").And.NotContain("https://andreaslop.nl");
|
||||
emptyBody.Should().NotContain("\"offers\"").And.NotContain("baked");
|
||||
var etagBefore = empty.Headers.ETag!.Tag;
|
||||
|
||||
// Create the package through the module: the very next request shows it.
|
||||
await InScope(async sp =>
|
||||
{
|
||||
await sp.GetRequiredService<IOfferingsService>().CreateAsync(
|
||||
new CreateOfferingRequest("Smoke", null, "€ 1.250", null, ["Eén uur", "O'Brien & zoon"], "Boek", Slug: "smoke"), caller);
|
||||
await sp.GetRequiredService<IOfferingBannersService>().CreateAsync(
|
||||
new OfferingBannerRequest("", "Nog twee plekken", Position: "top"), caller);
|
||||
return 0;
|
||||
});
|
||||
|
||||
var live = await client.GetAsync("/smoke");
|
||||
var body = await live.Content.ReadAsStringAsync();
|
||||
live.Headers.ETag!.Tag.Should().NotBe(etagBefore);
|
||||
body.Should().Contain("<li class=\"cms-offering\" data-slug=\"smoke\"><h3>Smoke</h3><p class=\"cms-price\">€ 1.250</p>");
|
||||
body.Should().Contain("<li>O'Brien & zoon</li>");
|
||||
body.Should().Contain("<!--CMS:BANNER-TOP-HTML--><aside class=\"cms-banner\"><span>Nog twee plekken</span></aside><!--/CMS:BANNER-TOP-HTML-->");
|
||||
body.Should().Contain("<span data-cms-price=\"smoke\">€ 1.250</span>");
|
||||
body.Should().Contain("\"price\":1250").And.Contain("\"priceCurrency\":\"EUR\"");
|
||||
|
||||
// The data block is the public API's answer plus the site's address.
|
||||
var start = body.IndexOf("<script type=\"application/json\" id=\"cms-data\">", StringComparison.Ordinal) + "<script type=\"application/json\" id=\"cms-data\">".Length;
|
||||
var json = body[start..body.IndexOf("</script>", start, StringComparison.Ordinal)];
|
||||
var data = JsonDocument.Parse(json).RootElement;
|
||||
data.GetProperty("siteUrl").GetString().Should().Be("https://test.andreaslop.nl");
|
||||
var api = JsonDocument.Parse(await client.GetStringAsync("/api/v1/offerings")).RootElement;
|
||||
Same(data.GetProperty("offerings"), api).Should().BeTrue("the data block carries the same JSON as GET /api/v1/offerings");
|
||||
var topApi = await client.GetStringAsync("/api/v1/offerings/banner?position=top");
|
||||
Same(data.GetProperty("banners").GetProperty("top"), JsonDocument.Parse(topApi).RootElement).Should().BeTrue();
|
||||
data.GetProperty("banners").GetProperty("bottom").ValueKind.Should().Be(JsonValueKind.Null);
|
||||
|
||||
// A matching ETag is a 304.
|
||||
var conditional = new HttpRequestMessage(HttpMethod.Get, "/smoke");
|
||||
conditional.Headers.IfNoneMatch.Add(live.Headers.ETag);
|
||||
(await client.SendAsync(conditional)).StatusCode.Should().Be(HttpStatusCode.NotModified);
|
||||
|
||||
// Switching the package off removes it again, and the JSON-LD loses its offers.
|
||||
await InScope(async sp =>
|
||||
{
|
||||
var service = sp.GetRequiredService<IOfferingsService>();
|
||||
var id = Guid.Parse((await service.ListAllAsync()).Single(o => o.Slug == "smoke").Id);
|
||||
await service.DeleteAsync(id, caller);
|
||||
return 0;
|
||||
});
|
||||
var gone = await client.GetStringAsync("/smoke");
|
||||
gone.Should().NotContain("data-slug=\"smoke\"").And.NotContain("\"offers\"");
|
||||
}
|
||||
|
||||
// The same JSON value; the writers may differ in escaping (the API writes "€" as is, the data block as €).
|
||||
private static bool Same(JsonElement a, JsonElement b) =>
|
||||
System.Text.Json.Nodes.JsonNode.DeepEquals(System.Text.Json.Nodes.JsonNode.Parse(a.GetRawText()), System.Text.Json.Nodes.JsonNode.Parse(b.GetRawText()));
|
||||
|
||||
[Fact]
|
||||
public async Task TheSitemap_GetsTheTestHostsAddress_ButStillComesFromTheBuild()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
var response = await client.GetAsync("/sitemap.xml");
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
(await response.Content.ReadAsStringAsync()).Should().Contain("<loc>https://test.andreaslop.nl/</loc>");
|
||||
}
|
||||
}
|
||||
@@ -133,6 +133,16 @@ public class SiteContractTests
|
||||
config.GetSection("SecurityHeaders:AllowedConnectOrigins").Get<string[]>().Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AndreaSlopFotografie_WritesItsProductionAddressInTheBuild_AndHasNoPublicAddressByDefault()
|
||||
{
|
||||
// The build is the same on test and production: the host swaps the authored address for the instance's own.
|
||||
var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie);
|
||||
|
||||
config["Website:AuthoredBaseUrl"].Should().Be("https://andreaslop.nl");
|
||||
config["Website:PublicBaseUrl"].Should().BeNullOrEmpty("the instance's own address is environment configuration");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AndreaSlopFotografie_MailBrandingPointsAtTheAdminAssets()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user