-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-1867] Fix sendgrid py3k bug; add sandbox mode
- Fix sendgrid attachments bug in py3k - Add sandbox mode option - Minor style fixes - Add test
- Loading branch information
1 parent
74c613b
commit 123cf66
Showing
2 changed files
with
75 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
|
||
import copy | ||
import unittest | ||
import tempfile | ||
import os | ||
|
||
from airflow.contrib.utils.sendgrid import send_email | ||
|
||
|
@@ -41,58 +43,78 @@ def setUp(self): | |
self.cc = ['[email protected]', '[email protected]'] | ||
self.bcc = ['[email protected]', '[email protected]'] | ||
self.expected_mail_data = { | ||
'content': [{'type': u'text/html', 'value': '<b>Foo</b> bar'}], | ||
'content': [{'type': u'text/html', 'value': self.html_content}], | ||
'personalizations': [ | ||
{'cc': [{'email': '[email protected]'}, {'email': '[email protected]'}], | ||
'to': [{'email': '[email protected]'}, {'email': '[email protected]'}], | ||
'bcc': [{'email': '[email protected]'}, {'email': '[email protected]'}]}], | ||
'from': {'email': u'[email protected]'}, | ||
'subject': 'sendgrid-send-email unit test'} | ||
'subject': 'sendgrid-send-email unit test', | ||
'mail_settings': {}, | ||
} | ||
self.personalization_custom_args = {'arg1': 'val1', 'arg2': 'val2'} | ||
self.categories = ['cat1', 'cat2'] | ||
# extras | ||
self.expected_mail_data_extras = copy.deepcopy(self.expected_mail_data) | ||
self.expected_mail_data_extras['personalizations'][0]['custom_args'] = \ | ||
self.personalization_custom_args | ||
self.expected_mail_data_extras['personalizations'][0]['custom_args'] = ( | ||
self.personalization_custom_args) | ||
self.expected_mail_data_extras['categories'] = self.categories | ||
self.expected_mail_data_extras['from'] = \ | ||
{'name': 'Foo', 'email': '[email protected]'} | ||
self.expected_mail_data_extras['from'] = { | ||
'name': 'Foo', | ||
'email': '[email protected]', | ||
} | ||
# sender | ||
self.expected_mail_data_sender = copy.deepcopy(self.expected_mail_data) | ||
self.expected_mail_data_sender['from'] = \ | ||
{'name': 'Foo Bar', 'email': '[email protected]'} | ||
self.expected_mail_data_sender['from'] = { | ||
'name': 'Foo Bar', | ||
'email': '[email protected]', | ||
} | ||
|
||
# Test the right email is constructed. | ||
|
||
@mock.patch('os.environ.get') | ||
# Test the right email is constructed. | ||
@mock.patch('os.environ', dict(os.environ, SENDGRID_MAIL_FROM='[email protected]')) | ||
@mock.patch('airflow.contrib.utils.sendgrid._post_sendgrid_mail') | ||
def test_send_email_sendgrid_correct_email(self, mock_post, mock_get): | ||
def get_return(var): | ||
return {'SENDGRID_MAIL_FROM': '[email protected]'}.get(var) | ||
def test_send_email_sendgrid_correct_email(self, mock_post): | ||
with tempfile.NamedTemporaryFile(mode='wt', suffix='.txt') as f: | ||
f.write('this is some test data') | ||
f.flush() | ||
|
||
filename = os.path.basename(f.name) | ||
expected_mail_data = dict( | ||
self.expected_mail_data, | ||
attachments=[{ | ||
'content': 'dGhpcyBpcyBzb21lIHRlc3QgZGF0YQ==', | ||
'content_id': '<{0}>'.format(filename), | ||
'disposition': 'attachment', | ||
'filename': filename, | ||
'type': 'text/plain', | ||
}], | ||
) | ||
|
||
mock_get.side_effect = get_return | ||
send_email(self.to, self.subject, self.html_content, cc=self.cc, bcc=self.bcc) | ||
mock_post.assert_called_with(self.expected_mail_data) | ||
send_email(self.to, | ||
self.subject, | ||
self.html_content, | ||
cc=self.cc, | ||
bcc=self.bcc, | ||
files=[f.name]) | ||
mock_post.assert_called_with(expected_mail_data) | ||
|
||
# Test the right email is constructed. | ||
@mock.patch('os.environ.get') | ||
@mock.patch( | ||
'os.environ', | ||
dict(os.environ, | ||
SENDGRID_MAIL_FROM='[email protected]', | ||
SENDGRID_MAIL_SENDER='Foo') | ||
) | ||
@mock.patch('airflow.contrib.utils.sendgrid._post_sendgrid_mail') | ||
def test_send_email_sendgrid_correct_email_extras(self, mock_post, mock_get): | ||
def get_return(var): | ||
return {'SENDGRID_MAIL_FROM': '[email protected]', | ||
'SENDGRID_MAIL_SENDER': 'Foo'}.get(var) | ||
|
||
mock_get.side_effect = get_return | ||
def test_send_email_sendgrid_correct_email_extras(self, mock_post): | ||
send_email(self.to, self.subject, self.html_content, cc=self.cc, bcc=self.bcc, | ||
personalization_custom_args=self.personalization_custom_args, | ||
categories=self.categories) | ||
mock_post.assert_called_with(self.expected_mail_data_extras) | ||
|
||
@mock.patch('os.environ.get') | ||
@mock.patch('os.environ', {}) | ||
@mock.patch('airflow.contrib.utils.sendgrid._post_sendgrid_mail') | ||
def test_send_email_sendgrid_sender(self, mock_post, mock_get): | ||
|
||
mock_get.return_value = None | ||
def test_send_email_sendgrid_sender(self, mock_post): | ||
send_email(self.to, self.subject, self.html_content, cc=self.cc, bcc=self.bcc, | ||
from_email='[email protected]', from_name='Foo Bar') | ||
mock_post.assert_called_with(self.expected_mail_data_sender) |