|
20 | 20 |
|
21 | 21 | import mock |
22 | 22 |
|
| 23 | +import django |
| 24 | +from unittest.mock import patch, MagicMock |
| 25 | + |
23 | 26 | from postmark import ( |
24 | 27 | PMBatchMail, PMMail, PMMailInactiveRecipientException, |
25 | 28 | PMMailUnprocessableEntityException, PMMailServerErrorException, |
|
29 | 32 | from django.conf import settings |
30 | 33 |
|
31 | 34 |
|
| 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 | + |
| 43 | +def make_fake_response(payload, code=200): |
| 44 | + """Helper to fake an HTTP response object.""" |
| 45 | + mock = MagicMock() |
| 46 | + mock.code = code |
| 47 | + mock.read.return_value = json.dumps(payload).encode("utf-8") |
| 48 | + mock.close.return_value = None |
| 49 | + return mock |
| 50 | + |
| 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 | + |
| 107 | + |
32 | 108 | class PMMailTests(unittest.TestCase): |
33 | 109 | def test_406_error_inactive_recipient(self): |
34 | 110 | json_payload = BytesIO() |
|
0 commit comments