90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from app.services.mail_sender import build_message, send_via_smtp, sign_message
|
|
|
|
|
|
def test_dkim_sign_uses_selector_before_domain():
|
|
message = build_message(
|
|
sender="user@ritoesis.com",
|
|
recipients=["recipient@example.com"],
|
|
cc=[],
|
|
subject="Test",
|
|
body_text="Hello",
|
|
body_html=None,
|
|
)
|
|
domain = SimpleNamespace(
|
|
domain_name="ritoesis.com",
|
|
dkim_selector="agmail",
|
|
dkim_private_key="private-key",
|
|
)
|
|
|
|
with patch(
|
|
"app.services.mail_sender.dkim.sign",
|
|
return_value=b"DKIM-Signature: test\r\n",
|
|
) as signer:
|
|
sign_message(message, domain)
|
|
|
|
assert signer.call_args.args[1] == b"agmail"
|
|
assert signer.call_args.args[2] == b"ritoesis.com"
|
|
|
|
|
|
async def test_send_via_smtp_uses_configured_client_hostname():
|
|
with patch(
|
|
"app.services.mail_sender.aiosmtplib.send",
|
|
new_callable=AsyncMock,
|
|
) as send_mock:
|
|
await send_via_smtp(
|
|
b"raw",
|
|
sender="noreply@gentic.email",
|
|
recipients=["user@example.com"],
|
|
)
|
|
|
|
assert send_mock.await_args.kwargs["local_hostname"] == "backend.gentic.email"
|
|
|
|
|
|
def test_build_message_sets_in_reply_to_and_references():
|
|
message = build_message(
|
|
sender="suyash@gentic.email",
|
|
recipients=["customer@gmail.com"],
|
|
cc=[],
|
|
subject="Re: fresh test",
|
|
body_text="human said B",
|
|
body_html=None,
|
|
in_reply_to="<CAHabc123@mail.gmail.com>",
|
|
references=["<CAHabc123@mail.gmail.com>"],
|
|
message_id="<B@gentic.email>",
|
|
)
|
|
assert message["Message-ID"] == "<B@gentic.email>"
|
|
assert message["In-Reply-To"] == "<CAHabc123@mail.gmail.com>"
|
|
assert message["References"] == "<CAHabc123@mail.gmail.com>"
|
|
raw = message.as_bytes()
|
|
assert b"In-Reply-To: <CAHabc123@mail.gmail.com>" in raw
|
|
assert b"References: <CAHabc123@mail.gmail.com>" in raw
|
|
|
|
|
|
def test_signed_mime_preserves_in_reply_to():
|
|
from unittest.mock import patch
|
|
|
|
message = build_message(
|
|
sender="suyash@gentic.email",
|
|
recipients=["customer@gmail.com"],
|
|
cc=[],
|
|
subject="Re: fresh test",
|
|
body_text="human said B",
|
|
body_html=None,
|
|
in_reply_to="<CAHabc123@mail.gmail.com>",
|
|
references=["<CAHabc123@mail.gmail.com>"],
|
|
message_id="<B@gentic.email>",
|
|
)
|
|
domain = SimpleNamespace(
|
|
domain_name="gentic.email",
|
|
dkim_selector="agmail",
|
|
dkim_private_key="private-key",
|
|
)
|
|
with patch(
|
|
"app.services.mail_sender.dkim.sign",
|
|
return_value=b"DKIM-Signature: test\r\n",
|
|
):
|
|
signed = sign_message(message, domain)
|
|
assert b"In-Reply-To: <CAHabc123@mail.gmail.com>" in signed
|