Contact-mail: vervolg op review (notitie weg, tekstdeel ingesprongen, nieuwe Core-tests voor details en Reply-To)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 15:31:49 +01:00
co-authored by Claude Sonnet 5.5
parent bc7936034c
commit 7974d22912
3 changed files with 122 additions and 4 deletions
@@ -479,3 +479,109 @@ public class MailPropertyTests(ITestOutputHelper output)
return value == token;
});
}
/// <summary>NEW tests for the contact-message mail (details block, text part); the existing tests above are unchanged.</summary>
public class ContactMessageMailRenderingTests
{
private readonly MailTemplateRenderer _renderer = new();
private static MailModel Model(params MailDetail[]? details) => new("De Site", Details: details);
[Fact]
public void Details_AreHtmlEncoded_BeforeLineBreaksBecomeBr()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl",
Model(new MailDetail("Bericht", "<script>alert(\"x\")</script> & 'q'\r\nregel2\nregel3")));
mail.Html.Should().NotContain("<script>").And.Contain("&lt;script&gt;")
.And.Contain("&amp;").And.Contain("regel2").And.Contain("<br>regel3");
mail.Html.Should().Contain("&lt;/script&gt; &amp; ");
}
[Fact]
public void ALabelIsEncodedToo()
=> _renderer.Render(MailKind.ContactMessage, "en", Model(new MailDetail("<b>L</b>", "v"))).Html
.Should().NotContain("<b>L</b>").And.Contain("&lt;b&gt;");
[Theory]
[InlineData(false)]
[InlineData(true)]
public void NullOrEmptyDetails_GiveNoBlock(bool empty)
{
var model = empty ? new MailModel("De Site", Details: []) : new MailModel("De Site");
var mail = _renderer.Render(MailKind.ContactMessage, "nl", model);
mail.Html.Should().NotContain("class=\"mail-rule\"");
mail.Text.Should().NotContain(" ");
}
[Fact]
public void TheSubject_IsFixed_AndNeverContainsADetail()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl", Model(new MailDetail("Onderwerp", "ZXQ-geheim")));
mail.Subject.Should().Be("Nieuw bericht via De Site");
_renderer.Render(MailKind.ContactMessage, "en", Model()).Subject.Should().Be("New message via De Site");
}
[Fact]
public void TheText_IndentsEveryVisitorLine_SoFakeHeadingsCannotImitateTheLayout()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl",
Model(new MailDetail("Bericht", "hallo\n--\nNieuw bericht via nep")));
var lines = mail.Text.Split('\n').Select(l => l.TrimEnd('\r')).ToList();
lines.Should().Contain("Bericht:").And.Contain(" hallo").And.Contain(" --").And.Contain(" Nieuw bericht via nep");
lines.Count(l => l == "--").Should().Be(1, "only the real footer separator starts at column 0");
}
[Fact]
public void NoReplyHintIsInTheMail_BecauseReplyToMayHaveBeenLeftOut()
=> _renderer.Render(MailKind.ContactMessage, "nl", Model()).Html.Should().NotContain("antwoorden");
}
public class SmtpReplyToTests
{
private static SmtpEmailSender Sender(int port) => new(
Options.Create(new EmailOptions
{
FromAddress = "no-reply@example.nl",
FromName = "Example Site",
Smtp = { Host = "127.0.0.1", Port = port, EnableSsl = false, TimeoutSeconds = 5 }
}),
Microsoft.Extensions.Logging.Abstractions.NullLogger<SmtpEmailSender>.Instance);
private static async Task<string> SendAsync(string? replyAddress, string? replyName)
{
using var server = new StubSmtpServer();
var result = await Sender(server.Port).SendAsync(
new OutgoingMail("to@example.org", "Hi", "<p>x</p>", "x", ReplyToAddress: replyAddress, ReplyToName: replyName));
result.Sent.Should().BeTrue();
return server.ReceivedData!;
}
[Fact]
public async Task AValidReplyTo_IsSet_WithItsName()
=> (await SendAsync("jan@example.nl", "Jan Jansen")).Should().Contain("Reply-To: Jan Jansen <jan@example.nl>");
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("not-an-address")]
[InlineData("a@b.nl\r\nBcc: evil@example.com")]
public async Task AnInvalidReplyTo_IsLeftOut_AndTheMailIsStillSent(string? address)
=> (await SendAsync(address, "Jan")).Should().NotContain("Reply-To:").And.NotContain("evil@example.com");
[Fact]
public async Task ANameWithControlCharacters_IsDropped_NeverReachingAHeader()
{
var data = await SendAsync("jan@example.nl", "Jan\r\nBcc: evil@example.com");
data.Should().Contain("Reply-To: jan@example.nl").And.NotContain("evil@example.com");
}
[Fact]
public async Task WithoutAReplyTo_ThereIsNoHeader()
=> (await SendAsync(null, null)).Should().NotContain("Reply-To:");
}