-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move email-sending code into EmailService
- Loading branch information
Showing
6 changed files
with
125 additions
and
84 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import smtplib | ||
|
||
import pyramid_mailer | ||
import pyramid_mailer.message | ||
|
||
from h.tasks.celery import get_task_logger | ||
|
||
logger = get_task_logger(__name__) | ||
|
||
|
||
class EmailService: | ||
"""A service for sending emails.""" | ||
|
||
def __init__(self, request, mailer): | ||
self._request = request | ||
self._mailer = mailer | ||
|
||
def send( | ||
self, recipients: list[str], subject: str, body: str, html: str | None = None | ||
): | ||
email = pyramid_mailer.message.Message( | ||
subject=subject, recipients=recipients, body=body, html=html | ||
) | ||
if self._request.debug: # pragma: no cover | ||
logger.info("emailing in debug mode: check the `mail/` directory") | ||
try: | ||
self._mailer.send_immediately(email) | ||
except smtplib.SMTPRecipientsRefused as exc: # pragma: no cover | ||
logger.warning( | ||
"Recipient was refused when trying to send an email. Does the user have an invalid email address?", | ||
exc_info=exc, | ||
) | ||
except smtplib.SMTPException as exc: | ||
raise exc | ||
|
||
|
||
def factory(_context, request): | ||
mailer = pyramid_mailer.get_mailer(request) | ||
return EmailService(request, mailer) |
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pytest | ||
|
||
from h.services.email import EmailService | ||
|
||
|
||
class TestEmailService: | ||
def test_send_creates_email_message(self, email_service, pyramid_mailer): | ||
email_service.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
subject="My email subject", | ||
recipients=["[email protected]"], | ||
body="Some text body", | ||
html=None, | ||
) | ||
|
||
def test_send_creates_email_message_with_html_body( | ||
self, email_service, pyramid_mailer | ||
): | ||
email_service.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
html="<p>An HTML body</p>", | ||
) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
subject="My email subject", | ||
recipients=["[email protected]"], | ||
body="Some text body", | ||
html="<p>An HTML body</p>", | ||
) | ||
|
||
def test_send_dispatches_email_using_request_mailer( | ||
self, email_service, pyramid_mailer, pyramid_request | ||
): | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
message = pyramid_mailer.message.Message.return_value | ||
|
||
email_service.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
) | ||
|
||
request_mailer.send_immediately.assert_called_once_with(message) | ||
|
||
@pytest.fixture | ||
def pyramid_request(self, pyramid_request): | ||
pyramid_request.debug = False | ||
return pyramid_request | ||
|
||
@pytest.fixture | ||
def pyramid_mailer(self, patch): | ||
return patch("h.services.email.pyramid_mailer", autospec=True) | ||
|
||
@pytest.fixture | ||
def email_service(self, pyramid_request, pyramid_mailer): | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
return EmailService(pyramid_request, request_mailer) |
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 |
---|---|---|
|
@@ -6,72 +6,8 @@ | |
from h.tasks import mailer | ||
|
||
|
||
@mock.patch("h.tasks.mailer.celery", autospec=True) | ||
@mock.patch("h.tasks.mailer.pyramid_mailer", autospec=True) | ||
def test_send_creates_email_message(pyramid_mailer, celery, pyramid_request): | ||
celery.request = pyramid_request | ||
|
||
mailer.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
subject="My email subject", | ||
recipients=["[email protected]"], | ||
body="Some text body", | ||
html=None, | ||
) | ||
|
||
|
||
@mock.patch("h.tasks.mailer.celery", autospec=True) | ||
@mock.patch("h.tasks.mailer.pyramid_mailer", autospec=True) | ||
def test_send_creates_email_message_with_html_body( | ||
pyramid_mailer, celery, pyramid_request | ||
): | ||
celery.request = pyramid_request | ||
|
||
mailer.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
html="<p>An HTML body</p>", | ||
) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
subject="My email subject", | ||
recipients=["[email protected]"], | ||
body="Some text body", | ||
html="<p>An HTML body</p>", | ||
) | ||
|
||
|
||
@mock.patch("h.tasks.mailer.celery", autospec=True) | ||
@mock.patch("h.tasks.mailer.pyramid_mailer", autospec=True) | ||
def test_send_dispatches_email_using_request_mailer( | ||
pyramid_mailer, celery, pyramid_request | ||
): | ||
celery.request = pyramid_request | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
message = pyramid_mailer.message.Message.return_value | ||
|
||
mailer.send( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
) | ||
|
||
pyramid_mailer.get_mailer.assert_called_once_with(pyramid_request) | ||
request_mailer.send_immediately.assert_called_once_with(message) | ||
|
||
|
||
@mock.patch("h.tasks.mailer.celery", autospec=True) | ||
@mock.patch("h.tasks.mailer.pyramid_mailer", autospec=True) | ||
def test_send_retries_if_mailing_fails(pyramid_mailer, celery, pyramid_request): | ||
celery.request = pyramid_request | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
request_mailer.send_immediately.side_effect = SMTPServerDisconnected() | ||
def test_send_retries_if_mailing_fails(email_service): | ||
email_service.send.side_effect = SMTPServerDisconnected() | ||
|
||
mailer.send.retry = mock.Mock(spec_set=[]) | ||
mailer.send( | ||
|
@@ -87,3 +23,10 @@ def test_send_retries_if_mailing_fails(pyramid_mailer, celery, pyramid_request): | |
def pyramid_request(pyramid_request): | ||
pyramid_request.debug = False | ||
return pyramid_request | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def celery(patch, pyramid_request): | ||
cel = patch("h.tasks.mailer.celery") | ||
cel.request = pyramid_request | ||
return cel |