2020
2121import mock
2222
23- import django
24- from unittest .mock import patch , MagicMock
23+ from unittest .mock import MagicMock
2524
2625from postmark import (
2726 PMBatchMail , PMMail , PMMailInactiveRecipientException ,
3231from django .conf import settings
3332
3433
35- if not settings .configured :
36- settings .configure (
37- POSTMARK_TRACK_OPENS = False ,
38- POSTMARK_API_KEY = "dummy" ,
39- POSTMARK_SENDER = "test@example.com" ,
40- )
41- django .setup ()
42-
4334def make_fake_response (payload , code = 200 ):
4435 """Helper to fake an HTTP response object."""
4536 mock = MagicMock ()
@@ -48,62 +39,6 @@ def make_fake_response(payload, code=200):
4839 mock .close .return_value = None
4940 return mock
5041
51- @patch ("postmark.core.urlopen" )
52- def test_mail_returns_result (mock_urlopen ):
53- # Fake Postmark single send response
54- fake_payload = {
55- "To" : "receiver@example.com" ,
56- "SubmittedAt" : "2025-09-18T10:00:00Z" ,
57- "MessageID" : "abc-123" ,
58- "ErrorCode" : 0 ,
59- "Message" : "OK"
60- }
61- mock_urlopen .return_value = make_fake_response (fake_payload )
62-
63- mail = PMMail (
64- api_key = "test-api-key" ,
65- sender = "sender@example.com" ,
66- to = "receiver@example.com" ,
67- subject = "Hello" ,
68- text_body = "Testing single mail return" ,
69- )
70- result = mail .send ()
71-
72- assert isinstance (result , dict )
73- assert result ["ErrorCode" ] == 0
74- assert result ["Message" ] == "OK"
75-
76-
77- @patch ("postmark.core.urlopen" )
78- def test_batch_mail_returns_results (mock_urlopen ):
79- # Fake batch response
80- fake_payload = [
81- {
82- "To" : "receiver@example.com" ,
83- "SubmittedAt" : "2025-09-18T10:00:00Z" ,
84- "MessageID" : "abc-123" ,
85- "ErrorCode" : 0 ,
86- "Message" : "OK" ,
87- }
88- ]
89- mock_urlopen .return_value = make_fake_response (fake_payload )
90-
91- # Build PMMail objects
92- message = PMMail (
93- api_key = "test-api-key" ,
94- sender = "sender@example.com" ,
95- to = "receiver@example.com" ,
96- subject = "Hello" ,
97- text_body = "Testing batch return" ,
98- )
99-
100- # Pass list of PMMail objects to PMBatchMail
101- batch = PMBatchMail (api_key = "test-api-key" , messages = [message ])
102- results = batch .send ()
103-
104- assert isinstance (results , list )
105- assert results [0 ]["ErrorCode" ] == 0
106-
10742
10843class PMMailTests (unittest .TestCase ):
10944 def test_406_error_inactive_recipient (self ):
@@ -257,6 +192,37 @@ def test_send_metadata_invalid_format(self):
257192 self .assertRaises (TypeError , PMMail , api_key = 'test' , sender = 'from@example.com' , to = 'to@example.com' ,
258193 subject = 'test' , text_body = 'test' , metadata = {'test' : {}})
259194
195+ def test_mail_returns_result (self ):
196+ fake_payload = {
197+ "To" : "receiver@example.com" ,
198+ "SubmittedAt" : "2025-09-18T10:00:00Z" ,
199+ "MessageID" : "abc-123" ,
200+ "ErrorCode" : 0 ,
201+ "Message" : "OK"
202+ }
203+
204+ mail = PMMail (
205+ api_key = "test-api-key" ,
206+ sender = "sender@example.com" ,
207+ to = "receiver@example.com" ,
208+ subject = "Hello" ,
209+ text_body = "Testing single mail return" ,
210+ )
211+
212+ with mock .patch ("postmark.core.urlopen" ) as mock_urlopen :
213+ mock_urlopen .return_value = make_fake_response (fake_payload )
214+
215+ # Test boolean return
216+ result = mail .send ()
217+ self .assertTrue (result )
218+
219+ # Test JSON return explicitly
220+ result_json = mail .send (return_json = True )
221+ self .assertIsInstance (result_json , dict )
222+ self .assertEqual (result_json ["ErrorCode" ], 0 )
223+ self .assertEqual (result_json ["Message" ], "OK" )
224+
225+
260226
261227class PMBatchMailTests (unittest .TestCase ):
262228 def test_406_error_inactive_recipient (self ):
@@ -321,6 +287,40 @@ def test_500_error_server_error(self):
321287 500 , '' , {}, None )):
322288 self .assertRaises (PMMailServerErrorException , batch .send )
323289
290+ def test_batch_mail_returns_results (self ):
291+ fake_payload = [
292+ {
293+ "To" : "receiver@example.com" ,
294+ "SubmittedAt" : "2025-09-18T10:00:00Z" ,
295+ "MessageID" : "abc-123" ,
296+ "ErrorCode" : 0 ,
297+ "Message" : "OK" ,
298+ }
299+ ]
300+
301+ message = PMMail (
302+ api_key = "test-api-key" ,
303+ sender = "sender@example.com" ,
304+ to = "receiver@example.com" ,
305+ subject = "Hello" ,
306+ text_body = "Testing batch return" ,
307+ )
308+ # pass list of PMMail objects to PMBatchMail
309+ batch = PMBatchMail (api_key = "test-api-key" , messages = [message ])
310+
311+ with mock .patch ("postmark.core.urlopen" ) as mock_urlopen :
312+ mock_urlopen .return_value = make_fake_response (fake_payload )
313+
314+ # Test boolean return
315+ results = batch .send ()
316+ self .assertTrue (results )
317+
318+ # Test JSON return explicitly
319+ results_json = batch .send (return_json = True )
320+ self .assertIsInstance (results_json , list )
321+ self .assertEqual (results_json [0 ]["ErrorCode" ], 0 )
322+
323+
324324
325325class PMBounceManagerTests (unittest .TestCase ):
326326 def test_activate (self ):
0 commit comments