def test_sending(self): """Verify that send_messages returns sent count if message is sent.""" sendgrid_backend = SendGridBackend(api_key="test") sendgrid_backend.sg.client = Mock() self.assertEqual( sendgrid_backend.send_messages(emails=[self.test_message]), 1 )
def test_build_w_bcc_sg_email(self): msg = EmailMessage(bcc=('*****@*****.**',)) with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [ {'bcc': [{'email': '*****@*****.**'}], 'subject': ''}], 'from': {'email': 'webmaster@localhost'}, 'subject': ''} ) # Test using "name <email>" format. msg = EmailMessage(bcc=('Andrii Soldatenko <*****@*****.**>',)) with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [ {'bcc': [ {'name': 'Andrii Soldatenko', 'email': '*****@*****.**'}], 'subject': ''}], 'from': {'email': 'webmaster@localhost'}, 'subject': ''} )
def test_send_messages_error(self): mock_client = MockClient(self.host) backend = SendGridBackend() backend.sg.client = mock_client msg = EmailMessage() with self.assertRaises(HTTPError): backend.send_messages( emails=[SendGridBackend()._build_sg_mail(msg)])
def test_build_empty_multi_alternatives_sg_email(self): html_content = '<p>This is an <strong>important</strong> message.</p>' msg = EmailMultiAlternatives() msg.attach_alternative(html_content, "text/html") with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, { 'content': [{ 'type': 'text/plain', 'value': '' }, { 'type': 'text/html', 'value': '<p>This is an ' '<strong>important</strong> ' 'message.</p>' }], 'from': { 'email': 'webmaster@localhost' }, 'personalizations': [{}], 'subject': '' })
def test_read_from_init_if_provided(settings): """Should pick up API key from init, if provided.""" settings.SENDGRID_API_KEY = 'somerandom key' actual_key = 'another' sg = SendGridBackend(api_key=actual_key) assert sg.api_key == actual_key
def test_attachment_string_type(self): backend = SendGridBackend() attachments = (('file.txt', 'File content', 'text/plain'), ) mail = EmailMessage( subject='Email subject', body='Email body', from_email='*****@*****.**', to=(RECIPIENT, ), attachments=attachments, ) # Replicate backend sending prepared_mail = backend._build_sg_mail(mail) # Send to SendGrid response = backend.sg.client.mail.send.post(request_body=prepared_mail) self.assertEqual(response.status_code, 202)
def test_reply_to_header(self): backend = SendGridBackend() mail = EmailMultiAlternatives( subject='Your subject', body='Simple text body', from_email='*****@*****.**', to=[ '*****@*****.**', ], headers={'Reply-To': '*****@*****.**'}) # Replicate backend sending prepared_mail = backend._build_sg_mail(mail) # Check for propper assembly self.assertEqual( prepared_mail, { 'from': { 'email': '*****@*****.**' }, 'subject': 'Your subject', 'personalizations': [{ 'to': [{ 'email': '*****@*****.**' }], 'subject': 'Your subject' }], 'content': [{ 'type': 'text/plain', 'value': 'Simple text body' }], 'reply_to': { 'email': '*****@*****.**' } }) # Send to SendGrid response = backend.sg.client.mail.send.post(request_body=prepared_mail) self.assertEqual(response.status_code, 202)
def test_build_empty_sg_mail(self): msg = EmailMessage() with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'from': {'email': 'webmaster@localhost'}, 'subject': '', 'content': [{'type': 'text/plain', 'value': ''}], 'personalizations': [{'subject': ''}]} )
def test_build_w_to_sg_email(self): msg = EmailMessage(to=('*****@*****.**',)) with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [ {'to': [{'email': '*****@*****.**'}], 'subject': ''}], 'from': {'email': 'webmaster@localhost'}, 'subject': ''} )
def test_build_sg_email_w_extra_headers(self): msg = EmailMessage() msg.extra_headers = {'EXTRA_HEADER': 'VALUE'} with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'type': 'text/plain', 'value': ''}], 'from': {'email': 'webmaster@localhost'}, 'headers': {'EXTRA_HEADER': 'VALUE'}, 'personalizations': [{'subject': ''}], 'subject': ''} )
def test_build_w_reply_to_sg_email(self): # Test setting a Reply-To header. msg = EmailMessage() msg.extra_headers = {'Reply-To': '*****@*****.**'} with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [{'subject': ''}], 'reply_to': {'email': '*****@*****.**'}, 'from': {'email': 'webmaster@localhost'}, 'subject': ''} ) # Test using the reply_to attribute. msg = EmailMessage(reply_to=('*****@*****.**',)) with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [{'subject': ''}], 'reply_to': {'email': '*****@*****.**'}, 'from': {'email': 'webmaster@localhost'}, 'subject': ''} ) # Test using "name <email>" format. msg = EmailMessage( reply_to=('Andrii Soldatenko <*****@*****.**>',)) with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'value': '', 'type': 'text/plain'}], 'personalizations': [{'subject': ''}], 'reply_to': { 'name': 'Andrii Soldatenko', 'email': '*****@*****.**'}, 'from': {'email': 'webmaster@localhost'}, 'subject': ''} )
def test_attachment_image_type(self): backend = SendGridBackend() with open(os.path.join(os.path.dirname(__file__), 'octocat.png'), 'rb') as f: img = f.read() attachments = (('octocat.png', img, 'image/png'), ) mail = EmailMessage( subject='Email subject', body='Email body', from_email='*****@*****.**', to=(RECIPIENT, ), attachments=attachments, ) # Replicate backend sending prepared_mail = backend._build_sg_mail(mail) # Send to SendGrid response = backend.sg.client.mail.send.post(request_body=prepared_mail) self.assertEqual(response.status_code, 202)
def test_build_sg_email_w_custom_args(self): msg = EmailMessage() msg.custom_args = {'custom_arg1': '12345-abcdef'} with self.settings(SENDGRID_API_KEY='test_key'): mail = SendGridBackend()._build_sg_mail(msg) self.assertEqual( mail, {'content': [{'type': 'text/plain', 'value': ''}], 'custom_args': {'custom_arg1': '12345-abcdef'}, 'from': {'email': 'webmaster@localhost'}, 'personalizations': [{'subject': ''}], 'subject': ''} )
def test_failing_silently(self): """Verify that send_messages can fail silently.""" sendgrid_backend = SendGridBackend(api_key="test") sendgrid_backend.sg.client = Mock() http_error = HTTPError(url="", code=999, msg=None, hdrs=None, fp=None) sendgrid_backend.sg.client.mail.send.post = Mock( side_effect=http_error ) self.assertFalse(sendgrid_backend.fail_silently) with self.assertRaises(HTTPError): sendgrid_backend.send_messages(emails=[self.test_message]) sendgrid_backend.fail_silently = True self.assertEqual( sendgrid_backend.send_messages(emails=[self.test_message]), 0 )
def test_sending_without_emails(self): """ Verify that send_messages returns nothing if no messages are passed. """ sendgrid_backend = SendGridBackend(api_key="test") self.assertEqual(sendgrid_backend.send_messages(emails=[]), None)
def test_raises_if_sendgrid_api_key_doesnt_exists(self): with self.assertRaises(ImproperlyConfigured): SendGridBackend()
def test_if_not_emails(self): with self.settings(SENDGRID_API_KEY='test_key'): SendGridBackend().send_messages(emails=[])
def test_read_from_settings_by_default(settings): """API key should be read from settings if not provided in init.""" settings.SENDGRID_API_KEY = 'somerandom key' sg = SendGridBackend() assert sg.api_key == settings.SENDGRID_API_KEY