Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions novem/vis/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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))
22 changes: 22 additions & 0 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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