Skip to content

Commit 17fdc93

Browse files
committed
add tests for mailgun
1 parent a9d2879 commit 17fdc93

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

emailer_lib/egress.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def send_intermediate_email_with_mailgun(
261261
data=data, files=files if files else None, domain=domain
262262
)
263263

264+
# The response object has a .json() method that returns the actual data
264265
return response
265266

266267

emailer_lib/tests/test_egress.py

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def test_send_intermediate_email_with_smtp_unknown_mime_type(monkeypatch):
145145

146146

147147
def test_send_intermediate_email_with_smtp_sendmail_args(monkeypatch):
148-
"""Test that sendmail is called with correct sender, recipients, and message format."""
149148
email = make_basic_email()
150149
mock_smtp, mock_smtp_ssl, context = setup_smtp_mocks(monkeypatch)
151150

@@ -204,16 +203,86 @@ def test_send_quarto_email_with_gmail(monkeypatch):
204203
assert i_email.recipients == ["[email protected]"]
205204

206205

206+
def test_send_intermediate_email_with_mailgun(monkeypatch):
207+
email = make_basic_email()
208+
email.external_attachments = ["file.txt"]
209+
210+
# Mock the response object with .json() method
211+
mock_response = MagicMock()
212+
mock_response.json.return_value = {
213+
"id": "<20251028141836.beb7f6b3fd2be2b7@sandboxedc0eedbb2da49f39cbc02665f66556c.mailgun.org>",
214+
"message": "Queued. Thank you."
215+
}
216+
mock_response.__repr__ = lambda self: "<Response [200]>"
217+
218+
# Mock the Mailgun Client
219+
mock_client_instance = MagicMock()
220+
mock_messages = MagicMock()
221+
mock_client_instance.messages = mock_messages
222+
mock_messages.create = MagicMock(return_value=mock_response)
223+
224+
mock_client_class = MagicMock(return_value=mock_client_instance)
225+
226+
with patch("mailgun.client.Client", mock_client_class):
227+
with patch("builtins.open", mock_open(read_data=b"file content")):
228+
response = send_intermediate_email_with_mailgun(
229+
api_key="test-api-key",
230+
domain="mg.example.com",
231+
232+
i_email=email,
233+
)
234+
235+
# Verify Client was initialized with correct auth
236+
mock_client_class.assert_called_once_with(auth=("api", "test-api-key"))
237+
238+
mock_messages.create.assert_called_once()
239+
call_args = mock_messages.create.call_args
240+
241+
data = call_args.kwargs["data"]
242+
assert data["from"] == "[email protected]"
243+
assert data["to"] == ["[email protected]"]
244+
assert data["subject"] == "Test"
245+
assert data["html"] == "<p>Hi</p>"
246+
assert data["text"] == "Plain text"
247+
248+
# Check files were passed
249+
files = call_args.kwargs["files"]
250+
assert files is not None
251+
assert len(files) == 2 # 1 inline, 1 external
252+
253+
assert call_args.kwargs["domain"] == "mg.example.com"
254+
255+
assert response == mock_response
256+
assert response.json() == {
257+
"id": "<20251028141836.beb7f6b3fd2be2b7@sandboxedc0eedbb2da49f39cbc02665f66556c.mailgun.org>",
258+
"message": "Queued. Thank you."
259+
}
260+
261+
262+
def test_send_intermediate_email_with_mailgun_no_recipients():
263+
email = IntermediateEmail(
264+
html="<p>Hi</p>",
265+
subject="Test",
266+
recipients=None,
267+
)
268+
269+
with pytest.raises(TypeError, match="i_email must have a populated recipients attribute"):
270+
send_intermediate_email_with_mailgun(
271+
api_key="test-api-key",
272+
domain="mg.example.com",
273+
274+
i_email=email,
275+
)
276+
277+
207278
@pytest.mark.parametrize(
208279
"send_func",
209280
[
210281
send_intermediate_email_with_redmail,
211282
send_intermediate_email_with_yagmail,
212-
send_intermediate_email_with_mailgun,
213283
],
214284
)
215285
def test_not_implemented_functions(send_func):
216-
"""Test that unimplemented send functions raise NotImplementedError."""
217286
email = make_basic_email()
218287
with pytest.raises(NotImplementedError):
219288
send_func(email)

0 commit comments

Comments
 (0)