|
20 | 20 |
|
21 | 21 | import mock |
22 | 22 |
|
| 23 | +from unittest.mock import MagicMock |
| 24 | + |
23 | 25 | from postmark import ( |
24 | 26 | PMBatchMail, PMMail, PMMailInactiveRecipientException, |
25 | 27 | PMMailUnprocessableEntityException, PMMailServerErrorException, |
|
29 | 31 | from django.conf import settings |
30 | 32 |
|
31 | 33 |
|
| 34 | +def make_fake_response(payload, code=200): |
| 35 | + """Helper to fake an HTTP response object.""" |
| 36 | + mock = MagicMock() |
| 37 | + mock.code = code |
| 38 | + mock.read.return_value = json.dumps(payload).encode("utf-8") |
| 39 | + mock.close.return_value = None |
| 40 | + return mock |
| 41 | + |
| 42 | + |
32 | 43 | class PMMailTests(unittest.TestCase): |
33 | 44 | def test_406_error_inactive_recipient(self): |
34 | 45 | json_payload = BytesIO() |
@@ -181,6 +192,37 @@ def test_send_metadata_invalid_format(self): |
181 | 192 | self.assertRaises(TypeError, PMMail, api_key='test', sender='from@example.com', to='to@example.com', |
182 | 193 | subject='test', text_body='test', metadata={'test': {}}) |
183 | 194 |
|
| 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 | + |
184 | 226 |
|
185 | 227 | class PMBatchMailTests(unittest.TestCase): |
186 | 228 | def test_406_error_inactive_recipient(self): |
@@ -245,6 +287,40 @@ def test_500_error_server_error(self): |
245 | 287 | 500, '', {}, None)): |
246 | 288 | self.assertRaises(PMMailServerErrorException, batch.send) |
247 | 289 |
|
| 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 | + |
248 | 324 |
|
249 | 325 | class PMBounceManagerTests(unittest.TestCase): |
250 | 326 | def test_activate(self): |
|
0 commit comments