Files
surveybox/tests/test_mail_custom_units.py
marco.morath 42dd1f2d4a
CI / audit (push) Skipped
CI / checks (push) Failing after 37s
CI / image (push) Skipped
Initial Commit: Status Testing
2026-11-25 18:46:04 +01:00

53 lines
2.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Eigene Mail-Vorlagen ohne Datenbank: Platzhalter, Prüfung, Anwenden."""
from __future__ import annotations
from types import SimpleNamespace
from surveybox.mail import custom, render
def context(**extra: object) -> dict[str, object]:
return {"app_name": "Feuerwehr", "origin": "https://x", "name": "Erika",
"form": SimpleNamespace(title="Sommerfest"), "link": "https://x/f/abc",
"guest": False, "links_url": "https://x/links", "unsubscribe_url": None,
"settings_url": "https://x/s", **extra}
def test_placeholders_follow_the_template() -> None:
assert {"greeting", "details", "app", "title", "name", "link", "entry"} <= set(
custom.placeholders("promoted"))
assert "title" not in custom.placeholders("invite")
assert custom.required("invite") == ["link"]
assert custom.required("register_exists") == ["forgot_url", "login_url"]
def test_validate() -> None:
ok = custom.clean(" Platz für {name}\n ", "{greeting}\r\n\r\nSchön!\n\n{details}")
assert ok.subject == "Platz für {name}" and "\r" not in ok.body
assert custom.validate("promoted", ok) == []
errors = dict(custom.validate("promoted", custom.Custom("", "Hallo {nope} ohne Link")))
assert set(errors) == {"subject", "unknown", "missing"}
assert errors["unknown"] == "{nope}" and errors["missing"] == "{link}"
assert custom.validate("promoted", custom.Custom("x", "Hier: {link}")) == []
def test_render_with_custom_text() -> None:
own = custom.Custom("Platz frei bei {app}: {title}",
"{greeting}\n\nWir freuen uns!\n\n{details}\n\nEure Feuerwehr")
mail = render.render("promoted", "en", context(entry="Früh"), own)
assert mail.subject == "Platz frei bei Feuerwehr: Sommerfest"
assert mail.text.startswith("Hello Erika,\n\nWir freuen uns!\n\nA place")
assert "https://x/f/abc" in mail.text and "Eure Feuerwehr" in mail.text
assert mail.text.count("Hello Erika") == 1 # Anrede nicht doppelt
assert '<a href="https://x/f/abc">' in mail.html and "-- \n" in mail.text
# Ohne Namen: allgemeine Anrede
plain = render.render("promoted", "en", context(name="", entry="Früh"), own)
assert plain.text.startswith("Hello,\n\nWir")
def test_preview_with_missing_values() -> None:
mail = render.render("confirmed", "en", {"name": "A"}, strict=False)
assert mail.subject and mail.text