using System.Net.Http.Json;
using ItWorx.KnowledgeHub.Contracts.Knowledge;
using ItWorx.KnowledgeHub.IntegrationTests.Infrastructure;
namespace ItWorx.KnowledgeHub.IntegrationTests.Knowledge;
/// Markdown and Mermaid output is sanitized against active content
/// (docs/27-acceptance-matrix.md, M2-A03) — a corpus of realistic attack payloads
/// rendered through the real HTTP pipeline against a real database, not a unit test
/// against the sanitizer in isolation.
[Collection(PostgresTestGroup.Name)]
public sealed class MarkdownSanitizationTests(PostgresContainerFixture postgres) : OwnerSetupTestBase(postgres)
{
[Fact]
public async Task Preview_renders_unsaved_markdown_with_the_same_sanitization_boundary()
{
using var client = Factory.CreateClient();
await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword);
var response = await SetupHelper.PostWithCsrfAsync(
client,
"/api/v1/pages/preview",
new PreviewMarkdownRequest("# Nieuw document\n\n**Veilig** "));
response.EnsureSuccessStatusCode();
var preview = await response.Content.ReadFromJsonAsync();
Assert.NotNull(preview);
Assert.Contains("Nieuw document", preview.RenderedHtml, StringComparison.Ordinal);
Assert.Contains("Veilig", preview.RenderedHtml, StringComparison.Ordinal);
Assert.DoesNotContain("", "\n
\n";
var response = await SetupHelper.PostWithCsrfAsync(
client, "/api/v1/pages", new CreatePageRequest(null, "Marker security test", "WikiPage", markdown, null));
response.EnsureSuccessStatusCode();
var page = await response.Content.ReadFromJsonAsync();
Assert.NotNull(page);
Assert.DoesNotContain("managed-source", page.RenderedHtml, StringComparison.Ordinal);
Assert.Contains("unknown-control", page.RenderedHtml, StringComparison.Ordinal);
Assert.DoesNotContain("