mirror of
https://git.hiabuto.net/C3MA/CCMA.git
synced 2026-09-06 03:20:49 +02:00
Reject unbalanced repeat markers in mail templates
validate_mail_template() only looked at placeholder names, and "{{#claims}}" is
not a placeholder -- so an unclosed block or a stray "{{/claims}}" passed the
check and rendered as itself: the member would read the marker in their mail.
Repeat markers are now checked structurally: every one names a block the template
actually has, openers and closers pair up in order, blocks do not nest (the
renderer does not support it either), and the subject takes no markers at all.
Each case explains what is wrong and what is missing.
The same check runs before sending, not just before saving: the templates are
plain files in the store and can be edited outside CCMA, where refusing to send
beats mailing a marker. Saving several edited templates now validates all of
them before writing the first, so a mistake in one no longer leaves the others
half-saved.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
d3dbb5e96d
commit
c46662561e
@@ -3,12 +3,15 @@ import pytest
|
||||
from ccma.domain.mail_templates import (
|
||||
MAIL_TEMPLATES,
|
||||
MailTemplate,
|
||||
MailTemplateBlock,
|
||||
MailTemplateError,
|
||||
MailTemplateSpec,
|
||||
default_mail_template,
|
||||
parse_mail_template,
|
||||
placeholder_entries,
|
||||
render_mail_template,
|
||||
serialize_mail_template,
|
||||
validate_block_markers,
|
||||
validate_mail_template,
|
||||
)
|
||||
from ccma.storage.repository import MemberRepository, RepositoryError
|
||||
@@ -154,3 +157,61 @@ def test_every_block_snippet_is_valid_in_its_own_template() -> None:
|
||||
for spec in MAIL_TEMPLATES:
|
||||
for block in spec.blocks:
|
||||
validate_mail_template(spec.key, "Betreff", block.snippet)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("body", "expected"),
|
||||
[
|
||||
("Hallo\n{{#claims}}\n{{claim.title}}", "wird nicht geschlossen"),
|
||||
("Hallo\n{{/claims}}\nGruß", "ohne zugehöriges"),
|
||||
("{{#claims}}\n{{claim.title}}\n{{/claims}}\n{{/claims}}", "ohne zugehöriges"),
|
||||
("{{#claims}}\n{{#claims}}\nx\n{{/claims}}\n{{/claims}}", "verschachtelt"),
|
||||
("{{#members}}\nx\n{{/members}}", "Unbekannter Wiederholungsblock"),
|
||||
("{{ # claims }}\nx", "wird nicht geschlossen"),
|
||||
],
|
||||
)
|
||||
def test_unbalanced_repeat_markers_are_rejected(body, expected) -> None:
|
||||
with pytest.raises(MailTemplateError, match=expected):
|
||||
validate_mail_template("welcome", "Betreff", body)
|
||||
|
||||
|
||||
def test_repeat_markers_in_the_subject_are_rejected() -> None:
|
||||
with pytest.raises(MailTemplateError, match="Betreff"):
|
||||
validate_mail_template("welcome", "Beitrag {{#claims}}", "Hallo")
|
||||
|
||||
|
||||
def test_a_template_without_blocks_rejects_every_marker() -> None:
|
||||
with pytest.raises(MailTemplateError, match="Verfügbar: keine"):
|
||||
validate_mail_template("sepa", "Betreff", "{{#claims}}\nx\n{{/claims}}")
|
||||
|
||||
|
||||
def test_a_closing_marker_must_match_the_open_one(monkeypatch) -> None:
|
||||
two_blocks = MailTemplateSpec(
|
||||
key="two-blocks",
|
||||
label="Test",
|
||||
filename="test.txt",
|
||||
description="",
|
||||
placeholders=(),
|
||||
blocks=(
|
||||
MailTemplateBlock("first", "", (("item.a", ""),), "{{item.a}}"),
|
||||
MailTemplateBlock("second", "", (("item.b", ""),), "{{item.b}}"),
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr("ccma.domain.mail_templates.MAIL_TEMPLATES", (two_blocks,))
|
||||
|
||||
with pytest.raises(MailTemplateError, match="Erwartet wird \\{\\{/first\\}\\}"):
|
||||
validate_block_markers("two-blocks", "Betreff", "{{#first}}\nx\n{{/second}}")
|
||||
|
||||
|
||||
def test_a_broken_template_is_not_stored(tmp_path) -> None:
|
||||
repository = MemberRepository(tmp_path)
|
||||
repository.initialize()
|
||||
path = repository.mail_templates_root / "willkommen.txt"
|
||||
before = path.read_text(encoding="utf-8")
|
||||
|
||||
with pytest.raises(RepositoryError, match="wird nicht geschlossen"):
|
||||
repository.save_mail_template(
|
||||
"welcome", subject="Willkommen", body="Hallo\n{{#claims}}\n{{claim.title}}"
|
||||
)
|
||||
|
||||
assert path.read_text(encoding="utf-8") == before
|
||||
|
||||
Reference in New Issue
Block a user