Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Sendgrid's http client #121

Merged
merged 2 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opwen_email_server/services/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, key: str) -> None:

@cached_property
def _client(self) -> Callable[[dict], int]:
if not self._key:
if not self._key: # pragma: no cover
def send_email_fake(email: dict) -> int:
self.log_warning('No key, not sending email %r', email)
return 202
Expand Down
51 changes: 34 additions & 17 deletions tests/opwen_email_server/services/test_sendgrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from unittest import TestCase
from unittest.mock import Mock
from unittest.mock import patch

from opwen_email_server.services.sendgrid import SendSendgridEmail

Expand All @@ -9,20 +11,15 @@ class SendgridEmailSenderTests(TestCase):
sender = 'sendgridtests@lokole.ca'

def test_sends_email(self):
send_email = SendSendgridEmail(key='')

success = send_email({
self.assertSendsEmail({
'to': [self.recipient1],
'from': self.sender,
'subject': self.test_sends_email.__name__,
'message': 'simple email with <b>formatting</b>'})

self.assertTrue(success)
'message': 'simple email with <b>formatting</b>'
})

def test_sends_email_with_attachments(self):
send_email = SendSendgridEmail(key='')

success = send_email({
self.assertSendsEmail({
'to': [self.recipient1],
'from': self.sender,
'subject': self.test_sends_email_with_attachments.__name__,
Expand All @@ -31,17 +28,37 @@ def test_sends_email_with_attachments(self):
{'filename': 'Some file.txt',
'content': b'first file'},
{'filename': 'Another file.txt',
'content': b'second file'}]})

self.assertTrue(success)
'content': b'second file'}]
})

def test_sends_email_to_multiple_recipients(self):
send_email = SendSendgridEmail(key='')

success = send_email({
self.assertSendsEmail({
'to': [self.recipient1, self.recipient2],
'from': self.sender,
'subject': self.test_sends_email_to_multiple_recipients.__name__,
'message': 'simple email with two recipients'})
'message': 'simple email with two recipients'
})

def test_client_made_bad_request(self):
self.assertSendsEmail(
{'message': self.test_client_made_bad_request.__name__},
success=False,
status=400)

def assertSendsEmail(self, email: dict, success: bool = True, status: int = 200):
with patch('urllib.request.build_opener') as mock_build_opener:
self.given_response_status(mock_build_opener, status)

send_email = SendSendgridEmail(key='fake')

send_success = send_email(email)

self.assertTrue(send_success if success else not send_success)

self.assertTrue(success)
@classmethod
def given_response_status(cls, mock_build_opener, status):
mock_opener = Mock()
mock_response = Mock()
mock_build_opener.return_value = mock_opener
mock_opener.open.return_value = mock_response
mock_response.getcode.return_value = status