Add email delivery check command
This commit is contained in:
@@ -41,6 +41,12 @@ def test_cli_has_dump_pbs_users() -> None:
|
||||
assert args.dump_pbs_users is True
|
||||
|
||||
|
||||
def test_cli_has_check_email() -> None:
|
||||
args = build_parser().parse_args(["--check-email"])
|
||||
|
||||
assert args.check_email is True
|
||||
|
||||
|
||||
def test_dump_report_data_does_not_export_sensitive_raw_fields(
|
||||
monkeypatch,
|
||||
capsys,
|
||||
@@ -246,3 +252,39 @@ def test_generate_pdf_sends_email_when_enabled(tmp_path, monkeypatch) -> None:
|
||||
|
||||
assert run(["--generate-pdf"]) == 0
|
||||
assert sent == [("smtp.example.invalid", pdf_path)]
|
||||
|
||||
|
||||
def test_check_email_sends_test_email(monkeypatch) -> None:
|
||||
config = AppConfig(
|
||||
pve_api_url="https://pve.example.invalid:8006",
|
||||
pve_api_token_id="backup-report@pve!report",
|
||||
pve_api_token_secret="secret",
|
||||
report_output_dir=Path("reports"),
|
||||
report_timezone="Europe/Paris",
|
||||
pve_verify_tls=True,
|
||||
pve_ca_bundle=None,
|
||||
pve_timeout_seconds=30,
|
||||
pve_backup_jobs_endpoint="/cluster/backup",
|
||||
pve_task_history_limit=500,
|
||||
pve_task_log_limit=5000,
|
||||
pbs_hostnames={},
|
||||
pbs_servers=(),
|
||||
log_level="INFO",
|
||||
report_filename_prefix="rapport-sauvegardes-pve",
|
||||
email=EmailConfig(
|
||||
enabled=True,
|
||||
smtp_host="smtp.example.invalid",
|
||||
smtp_from="report@example.invalid",
|
||||
smtp_to=("admin@example.invalid",),
|
||||
),
|
||||
)
|
||||
sent = []
|
||||
|
||||
monkeypatch.setattr("pve_backup_report.cli.load_config", lambda: config)
|
||||
monkeypatch.setattr(
|
||||
"pve_backup_report.cli.send_test_email",
|
||||
lambda email_config: sent.append(email_config.smtp_host),
|
||||
)
|
||||
|
||||
assert run(["--check-email"]) == 0
|
||||
assert sent == ["smtp.example.invalid"]
|
||||
|
||||
@@ -3,8 +3,10 @@ from pathlib import Path
|
||||
from pve_backup_report.config import EmailConfig
|
||||
from pve_backup_report.email_report import (
|
||||
build_report_message,
|
||||
EmailReportError,
|
||||
message_id_domain,
|
||||
send_report_email,
|
||||
send_test_email,
|
||||
)
|
||||
|
||||
|
||||
@@ -41,6 +43,46 @@ def test_message_id_domain_uses_sender_domain() -> None:
|
||||
assert message_id_domain(None) is None
|
||||
|
||||
|
||||
def test_send_test_email_builds_message_without_attachment(monkeypatch) -> None:
|
||||
sent = []
|
||||
config = EmailConfig(
|
||||
enabled=True,
|
||||
smtp_host="smtp.example.invalid",
|
||||
smtp_from="report@example.invalid",
|
||||
smtp_to=("admin@example.invalid",),
|
||||
subject="Rapport PVE",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"pve_backup_report.email_report.send_email_message",
|
||||
lambda email_config, message: sent.append((email_config, message)),
|
||||
)
|
||||
|
||||
send_test_email(config)
|
||||
|
||||
assert sent[0][0] == config
|
||||
message = sent[0][1]
|
||||
assert message["Subject"] == "[TEST] Rapport PVE"
|
||||
assert message["To"] == "admin@example.invalid"
|
||||
assert list(message.iter_attachments()) == []
|
||||
|
||||
|
||||
def test_send_test_email_requires_enabled_config() -> None:
|
||||
config = EmailConfig(
|
||||
enabled=False,
|
||||
smtp_host="smtp.example.invalid",
|
||||
smtp_from="report@example.invalid",
|
||||
smtp_to=("admin@example.invalid",),
|
||||
)
|
||||
|
||||
try:
|
||||
send_test_email(config)
|
||||
except EmailReportError as exc:
|
||||
assert "REPORT_EMAIL_ENABLED=false" in str(exc)
|
||||
else:
|
||||
raise AssertionError("EmailReportError attendu")
|
||||
|
||||
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user