import os import unittest from django.test import SimpleTestCase, override_settings, tag from anymail.exceptions import AnymailAPIError from anymail.message import AnymailMessage from .utils import AnymailTestMixin ANYMAIL_TEST_POSTAL_API_KEY = os.getenv('ANYMAIL_TEST_POSTAL_API_KEY') ANYMAIL_TEST_POSTAL_API_URL = os.getenv('ANYMAIL_TEST_POSTAL_API_URL') @tag('postal', 'live') @unittest.skipUnless(ANYMAIL_TEST_POSTAL_API_KEY and ANYMAIL_TEST_POSTAL_API_URL, "Set ANYMAIL_TEST_POSTAL_API_KEY and ANYMAIL_TEST_POSTAL_API_URL " "environment variables to run Postal integration tests") @override_settings(ANYMAIL_POSTAL_API_KEY=ANYMAIL_TEST_POSTAL_API_KEY, ANYMAIL_POSTAL_API_URL=ANYMAIL_TEST_POSTAL_API_URL, EMAIL_BACKEND="anymail.backends.postal.EmailBackend") class PostalBackendIntegrationTests(AnymailTestMixin, SimpleTestCase): """Postal API integration tests These tests run against the **live** Postal API, using the environment variable `ANYMAIL_TEST_POSTAL_API_KEY` as the API key and `ANYMAIL_TEST_POSTAL_API_URL` as server url. If these variables are not set, these tests won't run. """ def setUp(self): super().setUp() self.message = AnymailMessage('Anymail Postal integration test', 'Text content', 'from@example.com', ['test+to1@anymail.info']) self.message.attach_alternative('
HTML content
', "text/html") def test_simple_send(self): # Example of getting the Postal send status and message id from the message sent_count = self.message.send() self.assertEqual(sent_count, 1) anymail_status = self.message.anymail_status sent_status = anymail_status.recipients['test+to1@anymail.info'].status message_id = anymail_status.recipients['test+to1@anymail.info'].message_id self.assertEqual(sent_status, 'queued') self.assertGreater(len(message_id), 0) # non-empty string self.assertEqual(anymail_status.status, {sent_status}) # set of all recipient statuses self.assertEqual(anymail_status.message_id, message_id) def test_all_options(self): message = AnymailMessage( subject="Anymail Postal all-options integration test", body="This is the text body", from_email="Test From