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 SENDINBLUE_TEST_API_KEY = os.getenv('SENDINBLUE_TEST_API_KEY') @tag('sendinblue', 'live') @unittest.skipUnless(SENDINBLUE_TEST_API_KEY, "Set SENDINBLUE_TEST_API_KEY environment variable " "to run SendinBlue integration tests") @override_settings(ANYMAIL_SENDINBLUE_API_KEY=SENDINBLUE_TEST_API_KEY, ANYMAIL_SENDINBLUE_SEND_DEFAULTS=dict(), EMAIL_BACKEND="anymail.backends.sendinblue.EmailBackend") class SendinBlueBackendIntegrationTests(AnymailTestMixin, SimpleTestCase): """SendinBlue v3 API integration tests SendinBlue doesn't have sandbox so these tests run against the **live** SendinBlue API, using the environment variable `SENDINBLUE_TEST_API_KEY` as the API key If those variables are not set, these tests won't run. https://developers.sendinblue.com/docs/faq#section-how-can-i-test-the-api- """ def setUp(self): super().setUp() self.message = AnymailMessage('Anymail SendinBlue integration test', 'Text content', 'from@test-sb.anymail.info', ['test+to1@anymail.info']) self.message.attach_alternative('
HTML content
', "text/html") def test_simple_send(self): # Example of getting the SendinBlue 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') # SendinBlue always queues self.assertRegex(message_id, r'\<.+@.+\>') # Message-ID can be ...@smtp-relay.mail.fr or .sendinblue.com 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 SendinBlue all-options integration test", body="This is the text body", from_email='"Test From, with comma"HTML content
', "text/html") # SendinBlue requires an HTML body message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain") message.attach("attachment2.csv", "ID,Name\n1,Amy Lina", "text/csv") message.send() self.assertEqual(message.anymail_status.status, {'queued'}) # SendinBlue always queues self.assertRegex(message.anymail_status.message_id, r'\<.+@.+\>') def test_template(self): message = AnymailMessage( template_id=5, # There is a *new-style* template with this id in the Anymail test account from_email='Sender