|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from email.headerregistry import Address |
| 4 | + |
| 5 | +from django.test import SimpleTestCase, override_settings, tag |
| 6 | + |
| 7 | +from anymail.exceptions import AnymailAPIError |
| 8 | +from anymail.message import AnymailMessage |
| 9 | + |
| 10 | +from .utils import AnymailTestMixin, sample_image_path |
| 11 | + |
| 12 | +ANYMAIL_TEST_MAILPACE_SERVER_TOKEN = os.getenv("ANYMAIL_TEST_MAILPACE_SERVER_TOKEN") |
| 13 | +ANYMAIL_TEST_MAILPACE_DOMAIN = os.getenv("ANYMAIL_TEST_MAILPACE_DOMAIN") |
| 14 | + |
| 15 | + |
| 16 | +@tag("mailpace", "live") |
| 17 | +@unittest.skipUnless( |
| 18 | + ANYMAIL_TEST_MAILPACE_SERVER_TOKEN and ANYMAIL_TEST_MAILPACE_DOMAIN, |
| 19 | + "Set ANYMAIL_TEST_MAILPACE_SERVER_TOKEN and ANYMAIL_TEST_MAILPACE_DOMAIN" |
| 20 | + " environment variables to run MailPace integration tests", |
| 21 | +) |
| 22 | +@override_settings( |
| 23 | + ANYMAIL_MAILPACE_SERVER_TOKEN=ANYMAIL_TEST_MAILPACE_SERVER_TOKEN, |
| 24 | + EMAIL_BACKEND="anymail.backends.mailpace.EmailBackend", |
| 25 | +) |
| 26 | +class MailPaceBackendIntegrationTests(AnymailTestMixin, SimpleTestCase): |
| 27 | + """ |
| 28 | + MailPace API integration tests |
| 29 | +
|
| 30 | + These tests run against the **live** MailPace API, using the |
| 31 | + environment variable `ANYMAIL_TEST_MAILPACE_SERVER_TOKEN` as the API key, |
| 32 | + and `ANYMAIL_TEST_MAILPACE_DOMAIN` to construct sender addresses. |
| 33 | + If those variables are not set, these tests won't run. |
| 34 | + """ |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + super().setUp() |
| 38 | + self.from_email = str( |
| 39 | + Address(username="from", domain=ANYMAIL_TEST_MAILPACE_DOMAIN) |
| 40 | + ) |
| 41 | + self.message = AnymailMessage( |
| 42 | + "Anymail MailPace integration test", |
| 43 | + "Text content", |
| 44 | + self.from_email, |
| 45 | + |
| 46 | + ) |
| 47 | + self.message.attach_alternative("<p>HTML content</p>", "text/html") |
| 48 | + |
| 49 | + def test_simple_send(self): |
| 50 | + # Example of getting the MailPace send status and message id from the message |
| 51 | + sent_count = self.message.send() |
| 52 | + self.assertEqual(sent_count, 1) |
| 53 | + |
| 54 | + anymail_status = self.message.anymail_status |
| 55 | + sent_status = anymail_status. recipients[ "[email protected]"]. status |
| 56 | + message_id = anymail_status. recipients[ "[email protected]"]. message_id |
| 57 | + |
| 58 | + self.assertEqual(sent_status, "queued") |
| 59 | + self.assertGreater(message_id, 0) # integer MailPace reference ID |
| 60 | + # set of all recipient statuses: |
| 61 | + self.assertEqual(anymail_status.status, {sent_status}) |
| 62 | + self.assertEqual(anymail_status.message_id, message_id) |
| 63 | + |
| 64 | + def test_all_options(self): |
| 65 | + message = AnymailMessage( |
| 66 | + subject="Anymail MailPace all-options integration test", |
| 67 | + body="This is the text body", |
| 68 | + from_email=str( |
| 69 | + Address( |
| 70 | + display_name="Test From, with comma", |
| 71 | + username="sender", |
| 72 | + domain=ANYMAIL_TEST_MAILPACE_DOMAIN, |
| 73 | + ) |
| 74 | + ), |
| 75 | + to=[ |
| 76 | + |
| 77 | + '"Recipient 2, with comma" <[email protected]>', |
| 78 | + ], |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + headers={"List-Unsubscribe": "<https://example.com/unsub?id=123>"}, |
| 83 | + tags=["tag 1", "tag 2"], |
| 84 | + ) |
| 85 | + message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain") |
| 86 | + message.attach("attachment2.csv", "ID,Name\n1,Amy Lina", "text/csv") |
| 87 | + cid = message.attach_inline_image_file(sample_image_path()) |
| 88 | + message.attach_alternative( |
| 89 | + "<p><b>HTML:</b> with <a href='http://example.com'>link</a>" |
| 90 | + "and image: <img src='cid:%s'></div>" % cid, |
| 91 | + "text/html", |
| 92 | + ) |
| 93 | + |
| 94 | + message.send() |
| 95 | + self.assertEqual(message.anymail_status.status, {"queued"}) |
| 96 | + self.assertEqual( |
| 97 | + message. anymail_status. recipients[ "[email protected]"]. status, "queued" |
| 98 | + ) |
| 99 | + self.assertEqual( |
| 100 | + message. anymail_status. recipients[ "[email protected]"]. status, "queued" |
| 101 | + ) |
| 102 | + |
| 103 | + def test_invalid_from(self): |
| 104 | + self.message.from_email = "webmaster@localhost" # Django's default From |
| 105 | + with self.assertRaisesMessage( |
| 106 | + AnymailAPIError, "does not match domain in From field (localhost)" |
| 107 | + ): |
| 108 | + self.message.send() |
| 109 | + |
| 110 | + @override_settings(ANYMAIL_MAILPACE_SERVER_TOKEN="Hey, that's not a server token!") |
| 111 | + def test_invalid_server_token(self): |
| 112 | + with self.assertRaisesMessage(AnymailAPIError, "Invalid API Token"): |
| 113 | + self.message.send() |
0 commit comments