From 5b60c6201359695e0937e972044373077f1536c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Snoksrud?= Date: Sun, 6 Dec 2026 14:33:03 +0100 Subject: [PATCH] mail: read reply_to and enabled as yes/no switches Gaia now reads mail config/reply_to and config/enabled back as yes/no (novem-code/gaia#8039). Mail.reply_to returned the raw text, so a caller comparing it with "true" changed outcome, and a truth test was always true since "no" is a non-empty string. reply_to now returns a bool, accepting yes/no and the older true/false. Assigning a bool writes yes/no, which is the only spelling the renderer honours today. Strings still pass through unchanged. An enabled property does the same for config/enabled, which had no accessor. Closes #348 --- novem/vis/mail.py | 33 ++++++++++++++++++++++++++++----- tests/test_mail.py | 22 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/novem/vis/mail.py b/novem/vis/mail.py index 8a898e1..eea6565 100644 --- a/novem/vis/mail.py +++ b/novem/vis/mail.py @@ -7,6 +7,15 @@ from .mail_sections import NovemEmailSection, PreviewSection +_AFFIRMATIVE = ("yes", "true", "y", "1", "on") + + +def _switch(value: Union[bool, str]) -> str: + """Encode a mail switch for writing; strings pass through unchanged.""" + if isinstance(value, bool): + return "yes" if value else "no" + return value + class Mail(NovemVisAPI): """A novem e-mail, addressed by name. @@ -53,7 +62,7 @@ def __init__( theme: Optional[str] = None, size: Optional[str] = None, template: Optional[str] = None, - reply_to: Optional[str] = None, + reply_to: Optional[Union[bool, str]] = None, **kwargs: Any, ) -> None: """ @@ -364,9 +373,23 @@ def template(self, value: str) -> None: return self.api_write("/config/template", value) @property - def reply_to(self) -> str: - return self.api_read("/config/reply_to") + def reply_to(self) -> bool: + """Whether the sender's reply address is added as Reply-To. + + Gaia reads the switch back as ``yes``/``no``; older values stored as + ``true``/``false`` are accepted too. + """ + return self.api_read("/config/reply_to").strip().lower() in _AFFIRMATIVE @reply_to.setter - def reply_to(self, value: str) -> None: - return self.api_write("/config/reply_to", value) + def reply_to(self, value: Union[bool, str]) -> None: + return self.api_write("/config/reply_to", _switch(value)) + + @property + def enabled(self) -> bool: + """Whether the mail may be sent to its regular recipients.""" + return self.api_read("/config/enabled").strip().lower() in _AFFIRMATIVE + + @enabled.setter + def enabled(self, value: Union[bool, str]) -> None: + return self.api_write("/config/enabled", _switch(value)) diff --git a/tests/test_mail.py b/tests/test_mail.py index 5ee8051..80b77f7 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -6,6 +6,8 @@ from functools import partial from unittest.mock import patch +import pytest + from novem import Mail, Plot from novem.mail import AuthorSection, ParagraphSection, PreviewSection, VisSection from novem.utils import API_ROOT @@ -912,3 +914,23 @@ def test_mail_log(requests_mock, fs): # Assert that the output matches the expected string assert output == "log_test_mail" + + +@pytest.mark.parametrize("field", ["reply_to", "enabled"]) +@pytest.mark.parametrize( + "stored,expected", + [("yes", True), ("no", False), ("yes\n", True), ("true", True), ("false", False), ("True", True), ("", False)], +) +def test_mail_switch_readback(requests_mock, field, stored, expected): + m = setup_mail_mock(requests_mock, {"mail_id": "test_mail", "reqs": []}) + requests_mock.register_uri("get", f"{API_ROOT}vis/mails/test_mail/config/{field}", text=stored) + assert getattr(m, field) is expected + + +@pytest.mark.parametrize("field", ["reply_to", "enabled"]) +@pytest.mark.parametrize("value,sent", [(True, "yes"), (False, "no"), ("true", "true"), ("no", "no")]) +def test_mail_switch_write(requests_mock, field, value, sent): + m = setup_mail_mock(requests_mock, {"mail_id": "test_mail", "reqs": []}) + post = requests_mock.register_uri("post", f"{API_ROOT}vis/mails/test_mail/config/{field}", text="") + setattr(m, field, value) + assert post.last_request.text == sent