Add email delivery for generated reports
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
from pathlib import Path
|
||||
|
||||
from pve_backup_report.config import EmailConfig
|
||||
from pve_backup_report.email_report import build_report_message, send_report_email
|
||||
|
||||
|
||||
def test_build_report_message_attaches_pdf(tmp_path: Path) -> None:
|
||||
pdf_path = tmp_path / "rapport.pdf"
|
||||
pdf_path.write_bytes(b"%PDF-1.7")
|
||||
config = EmailConfig(
|
||||
enabled=True,
|
||||
smtp_host="smtp.example.invalid",
|
||||
smtp_from="report@example.invalid",
|
||||
smtp_to=("admin@example.invalid",),
|
||||
subject="Rapport PVE",
|
||||
)
|
||||
|
||||
message = build_report_message(config, pdf_path)
|
||||
|
||||
assert message["Subject"] == "Rapport PVE"
|
||||
assert message["From"] == "report@example.invalid"
|
||||
assert message["To"] == "admin@example.invalid"
|
||||
attachments = list(message.iter_attachments())
|
||||
assert len(attachments) == 1
|
||||
assert attachments[0].get_filename() == "rapport.pdf"
|
||||
assert attachments[0].get_content_type() == "application/pdf"
|
||||
assert attachments[0].get_payload(decode=True) == b"%PDF-1.7"
|
||||
|
||||
|
||||
def test_send_report_email_uses_starttls_and_auth(tmp_path: Path, monkeypatch) -> None:
|
||||
pdf_path = tmp_path / "rapport.pdf"
|
||||
pdf_path.write_bytes(b"%PDF-1.7")
|
||||
calls = []
|
||||
|
||||
class FakeSmtp:
|
||||
def __init__(self, host: str, port: int, timeout: int) -> None:
|
||||
calls.append(("connect", host, port, timeout))
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, traceback) -> None:
|
||||
calls.append(("close",))
|
||||
|
||||
def starttls(self) -> None:
|
||||
calls.append(("starttls",))
|
||||
|
||||
def login(self, username: str, password: str) -> None:
|
||||
calls.append(("login", username, password))
|
||||
|
||||
def send_message(self, message) -> None:
|
||||
calls.append(("send", message["To"]))
|
||||
|
||||
monkeypatch.setattr("pve_backup_report.email_report.smtplib.SMTP", FakeSmtp)
|
||||
config = EmailConfig(
|
||||
enabled=True,
|
||||
smtp_host="smtp.example.invalid",
|
||||
smtp_port=587,
|
||||
smtp_username="report@example.invalid",
|
||||
smtp_password="secret",
|
||||
smtp_from="report@example.invalid",
|
||||
smtp_to=("admin@example.invalid",),
|
||||
smtp_starttls=True,
|
||||
smtp_timeout_seconds=12,
|
||||
)
|
||||
|
||||
send_report_email(config, pdf_path)
|
||||
|
||||
assert calls == [
|
||||
("connect", "smtp.example.invalid", 587, 12),
|
||||
("starttls",),
|
||||
("login", "report@example.invalid", "secret"),
|
||||
("send", "admin@example.invalid"),
|
||||
("close",),
|
||||
]
|
||||
Reference in New Issue
Block a user