feat: harden campaign delivery and editing
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from email.message import EmailMessage
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
@@ -183,3 +184,91 @@ def test_report_send_fails_closed_until_durable_mail_outbox_exists() -> None:
|
||||
)
|
||||
|
||||
generate_report.assert_not_called()
|
||||
|
||||
|
||||
def test_live_report_submits_exact_message_to_durable_mail_command() -> None:
|
||||
campaign = SimpleNamespace(
|
||||
id="campaign-1",
|
||||
tenant_id="tenant-1",
|
||||
name="Campaign",
|
||||
external_id="campaign-1",
|
||||
)
|
||||
version = SimpleNamespace(
|
||||
id="version-1",
|
||||
campaign_id="campaign-1",
|
||||
raw_json={"server": {"mail_profile_id": "profile-1"}},
|
||||
execution_snapshot={"snapshot_version": "7"},
|
||||
)
|
||||
config = SimpleNamespace(
|
||||
server=SimpleNamespace(
|
||||
profile_capabilities=SimpleNamespace(smtp_available=True)
|
||||
),
|
||||
)
|
||||
snapshot = SimpleNamespace(
|
||||
mail_profile_id="profile-1",
|
||||
smtp_server_id="smtp-1",
|
||||
smtp_credential_id="credential-1",
|
||||
smtp_transport_revision="revision-1",
|
||||
)
|
||||
message = EmailMessage()
|
||||
message["From"] = "Sender <sender@example.test>"
|
||||
message["To"] = "recipient@example.test"
|
||||
message["Subject"] = "Report"
|
||||
message.set_content("content")
|
||||
mail = SimpleNamespace(
|
||||
durable_delivery_available=True,
|
||||
submit_delivery_command=lambda _session, **_kwargs: {
|
||||
"id": "command-1",
|
||||
"status": "pending",
|
||||
"duplicate": False,
|
||||
},
|
||||
)
|
||||
|
||||
class Session:
|
||||
def get(self, _model, _id):
|
||||
return campaign
|
||||
|
||||
with (
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing._selected_version",
|
||||
return_value=version,
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing._load_config",
|
||||
return_value=config,
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing.ensure_execution_snapshot",
|
||||
return_value=snapshot,
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing.generate_campaign_report",
|
||||
return_value={},
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing.build_report_message",
|
||||
return_value=message,
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing._effective_from",
|
||||
return_value=("sender@example.test", "Sender"),
|
||||
),
|
||||
patch(
|
||||
"govoplan_campaign.backend.reports.emailing.mail_integration",
|
||||
return_value=mail,
|
||||
),
|
||||
):
|
||||
result = send_campaign_report_email(
|
||||
Session(), # type: ignore[arg-type]
|
||||
tenant_id="tenant-1",
|
||||
campaign_id="campaign-1",
|
||||
to=["recipient@example.test"],
|
||||
idempotency_key="request-1",
|
||||
created_by_user_id="user-1",
|
||||
)
|
||||
|
||||
assert result.command_id == "command-1"
|
||||
assert result.delivery_status == "pending"
|
||||
assert result.sent is False
|
||||
assert result.audit_dict()["recipient_count"] == 1
|
||||
assert "recipient@example.test" not in repr(result.audit_dict())
|
||||
|
||||
Reference in New Issue
Block a user