ASF-injectie 4/5: tests voor OfferingsHtmlTransform (regio's, data-blok, JSON-LD, inline, fail-open)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
||||
|
||||
public class OfferingsHtmlTransformTests
|
||||
{
|
||||
private readonly IOfferingsService _offerings = Substitute.For<IOfferingsService>();
|
||||
private readonly IOfferingBannersService _banners = Substitute.For<IOfferingBannersService>();
|
||||
private static readonly HtmlTransformContext Context = new("https://test.andreaslop.nl");
|
||||
|
||||
private OfferingsHtmlTransform Sut() => new(_offerings, _banners);
|
||||
|
||||
private static OfferingDto Offering(string slug, string title, string price, int order = 0, bool featured = false) => new(
|
||||
Guid.NewGuid().ToString(), slug, title, "Beschrijving", price, "eenmalig", ["1 uur fotografie", "Online galerij"], "Boek", featured, order);
|
||||
|
||||
private void Live(params OfferingDto[] list) => _offerings.GetOfferingsAsync().Returns(list);
|
||||
|
||||
private void Banner(string position, PublicOfferingBannerDto? banner) => _banners.GetActiveAsync(position).Returns(banner);
|
||||
|
||||
// ---- what a page asks for decides what is loaded ----
|
||||
|
||||
[Theory]
|
||||
[InlineData("<p>nothing</p>", false)]
|
||||
[InlineData("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->", true)]
|
||||
[InlineData("<span data-cms-price=\"a\">x</span>", true)]
|
||||
public void Wants_OnlyPagesWithCmsMarkers(string html, bool expected) => Sut().Wants(html).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public async Task APageWithOnlyAnOfferingsRegion_NeverLoadsBanners()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 185"));
|
||||
|
||||
await Sut().ApplyAsync("<!--CMS:OFFERINGS-HTML-->x<!--/CMS:OFFERINGS-HTML-->", Context, default);
|
||||
|
||||
await _banners.DidNotReceiveWithAnyArgs().GetActiveAsync(default!);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task APageWithOnlyABannerRegion_NeverLoadsOfferings()
|
||||
{
|
||||
Banner("top", null);
|
||||
|
||||
await Sut().ApplyAsync("<!--CMS:BANNER-TOP-HTML-->x<!--/CMS:BANNER-TOP-HTML-->", Context, default);
|
||||
|
||||
await _offerings.DidNotReceive().GetOfferingsAsync();
|
||||
}
|
||||
|
||||
// ---- regions ----
|
||||
|
||||
[Fact]
|
||||
public async Task TheRegions_AreFilledFromLiveData_AndAnEndedBannerEmptiesItsRegion()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 185"));
|
||||
Banner("top", new PublicOfferingBannerDto("", "Nog twee plekken", null, null, "top"));
|
||||
Banner("bottom", null);
|
||||
var html = "<!--CMS:BANNER-TOP-HTML-->baked top<!--/CMS:BANNER-TOP-HTML-->" +
|
||||
"<!--CMS:OFFERINGS-HTML-->baked list<!--/CMS:OFFERINGS-HTML-->" +
|
||||
"<!--CMS:BANNER-BOTTOM-HTML-->baked bottom<!--/CMS:BANNER-BOTTOM-HTML-->";
|
||||
|
||||
var result = await Sut().ApplyAsync(html, Context, default);
|
||||
|
||||
result.Should().Contain("<!--CMS:BANNER-TOP-HTML--><aside class=\"cms-banner\"><span>Nog twee plekken</span></aside><!--/CMS:BANNER-TOP-HTML-->");
|
||||
result.Should().Contain("<!--CMS:OFFERINGS-HTML--><ul class=\"cms-offerings\"><li class=\"cms-offering\" data-slug=\"newborn\">");
|
||||
result.Should().Contain("<!--CMS:BANNER-BOTTOM-HTML--><!--/CMS:BANNER-BOTTOM-HTML-->", "no active banner (204) empties the region");
|
||||
result.Should().NotContain("baked");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenNoPackageIsLive_TheListRegionIsEmptied()
|
||||
{
|
||||
Live();
|
||||
|
||||
var result = await Sut().ApplyAsync("<!--CMS:OFFERINGS-HTML-->baked<!--/CMS:OFFERINGS-HTML-->", Context, default);
|
||||
|
||||
result.Should().Be("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->");
|
||||
}
|
||||
|
||||
// ---- data block ----
|
||||
|
||||
[Fact]
|
||||
public async Task TheDataBlock_HasTheShapeOfThePublicApi_PlusTheSiteUrl()
|
||||
{
|
||||
var newborn = Offering("newborn", "Newborn", "€ 185", featured: true);
|
||||
Live(newborn);
|
||||
var top = new PublicOfferingBannerDto("Titel", "Tekst", "Meer", "/contact", "top");
|
||||
Banner("top", top);
|
||||
Banner("bottom", null);
|
||||
|
||||
var html = await Sut().ApplyAsync("<head><!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>", Context, default);
|
||||
|
||||
html.Should().StartWith("<head><script type=\"application/json\" id=\"cms-data\">").And.EndWith("</script></head>");
|
||||
var json = html["<head><script type=\"application/json\" id=\"cms-data\">".Length..^"</script></head>".Length];
|
||||
var data = JsonDocument.Parse(json).RootElement;
|
||||
data.GetProperty("siteUrl").GetString().Should().Be("https://test.andreaslop.nl");
|
||||
|
||||
// The same JSON as the public endpoints write (camelCase, nulls included).
|
||||
var api = new JsonSerializerOptions(JsonSerializerDefaults.Web);
|
||||
data.GetProperty("offerings").GetRawText().Should().Be(JsonSerializer.Serialize(new[] { newborn }, api));
|
||||
data.GetProperty("banners").GetProperty("top").GetRawText().Should().Be(JsonSerializer.Serialize(top, api));
|
||||
data.GetProperty("banners").GetProperty("bottom").ValueKind.Should().Be(JsonValueKind.Null);
|
||||
data.GetProperty("offerings")[0].GetProperty("slug").GetString().Should().Be("newborn");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheDataBlock_CannotCloseTheScriptElement()
|
||||
{
|
||||
Live(Offering("x", "</script><img src=x onerror=alert(1)> & \u2028", "€ 100"));
|
||||
Banner("top", null);
|
||||
Banner("bottom", null);
|
||||
|
||||
var html = await Sut().ApplyAsync("<!--CMS:DATA-->", Context, default);
|
||||
|
||||
var inner = html["<script type=\"application/json\" id=\"cms-data\">".Length..^"</script>".Length];
|
||||
inner.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028");
|
||||
JsonDocument.Parse(inner).RootElement.GetProperty("offerings")[0].GetProperty("title").GetString()
|
||||
.Should().Be("</script><img src=x onerror=alert(1)> & \u2028");
|
||||
}
|
||||
|
||||
// ---- JSON-LD ----
|
||||
|
||||
private const string Service = "{\"@context\":\"https://schema.org\",\"@type\":\"WebPage\",\"mainEntity\":{\"@type\":\"Service\",\"@id\":\"https://andreaslop.nl/soorten-fotoshoots/newborn#service\",\"name\":\"Newborn é\",\"offers\":{\"@type\":\"Offer\",\"price\":150,\"priceCurrency\":\"EUR\"}}}";
|
||||
|
||||
private static string Page(string json, string slug = "newborn") =>
|
||||
$"<!--CMS:OFFER-JSON-LD slug=\"{slug}\"--><script type=\"application/ld+json\">{json}</script>";
|
||||
|
||||
[Fact]
|
||||
public async Task JsonLd_ThePriceComesFromTheLivePackage_AndTheRestOfTheBlockStays()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 1.250,-"));
|
||||
|
||||
var html = await Sut().ApplyAsync(Page(Service), Context, default);
|
||||
|
||||
html.Should().StartWith("<script type=\"application/ld+json\">").And.NotContain("<!--");
|
||||
var json = JsonDocument.Parse(html["<script type=\"application/ld+json\">".Length..^"</script>".Length]).RootElement;
|
||||
var offers = json.GetProperty("mainEntity").GetProperty("offers");
|
||||
offers.GetProperty("price").GetInt32().Should().Be(1250);
|
||||
offers.GetProperty("priceCurrency").GetString().Should().Be("EUR");
|
||||
json.GetProperty("mainEntity").GetProperty("name").GetString().Should().Be("Newborn é");
|
||||
html.Should().Contain("Newborn é", "accents stay readable, not \\u-escaped");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("op aanvraag")]
|
||||
[InlineData("€ 12,50")]
|
||||
public async Task JsonLd_WhenThePriceIsNoPrice_OffersAreRemoved(string price)
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", price));
|
||||
|
||||
var html = await Sut().ApplyAsync(Page(Service), Context, default);
|
||||
|
||||
html.Should().NotContain("offers").And.Contain("\"name\"");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonLd_WhenThePackageIsNotLive_OffersAreRemoved()
|
||||
{
|
||||
Live(Offering("gezin", "Gezin", "€ 175"));
|
||||
|
||||
var html = await Sut().ApplyAsync(Page(Service), Context, default);
|
||||
|
||||
html.Should().NotContain("offers").And.Contain("\"name\"");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonLd_ABlockThatIsNotJson_IsLeftAlone_MarkerIncluded()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 185"));
|
||||
var page = Page("{ not json");
|
||||
|
||||
(await Sut().ApplyAsync(page, Context, default)).Should().Be(page);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonLd_AnOffersArray_GetsThePriceOnItsFirstOffer()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 185"));
|
||||
var json = "{\"@type\":\"Service\",\"offers\":[{\"@type\":\"Offer\",\"price\":1}]}";
|
||||
|
||||
var html = await Sut().ApplyAsync(Page(json), Context, default);
|
||||
|
||||
html.Should().Contain("\"price\":185");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonLd_NeverWritesALiteralClosingScriptTag()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 185"));
|
||||
var json = "{\"name\":\"a \\u003C/script> b\",\"offers\":{\"@type\":\"Offer\",\"price\":1}}";
|
||||
|
||||
var html = await Sut().ApplyAsync(Page(json), Context, default);
|
||||
|
||||
html.Split("</script>").Length.Should().Be(2, "only the closing tag of the block itself");
|
||||
}
|
||||
|
||||
// ---- inline ----
|
||||
|
||||
[Fact]
|
||||
public async Task Inline_ThePriceOfASpanFollowsTheLivePackage()
|
||||
{
|
||||
Live(Offering("newborn", "Newborn", "€ 1.250"), Offering("gezin", "Gezin", "op aanvraag"));
|
||||
var html = "<p><span data-cms-price=\"newborn\">€ 185</span> <span data-cms-price=\"gezin\">€ 175</span> <span data-cms-price=\"weg\">€ 1</span></p>";
|
||||
|
||||
var result = await Sut().ApplyAsync(html, Context, default);
|
||||
|
||||
result.Should().Be("<p><span data-cms-price=\"newborn\">€ 1.250</span> <span data-cms-price=\"gezin\">€ 175</span> <span data-cms-price=\"weg\">€ 1</span></p>");
|
||||
}
|
||||
|
||||
// ---- failure is the injector's business ----
|
||||
|
||||
[Fact]
|
||||
public async Task ADatabaseFailure_Propagates_SoTheInjectorServesTheBakedPage()
|
||||
{
|
||||
_offerings.GetOfferingsAsync().ThrowsAsync(new InvalidOperationException("db down"));
|
||||
|
||||
var act = () => Sut().ApplyAsync("<!--CMS:OFFERINGS-HTML-->baked<!--/CMS:OFFERINGS-HTML-->", Context, default);
|
||||
|
||||
await act.Should().ThrowAsync<InvalidOperationException>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user