-
Notifications
You must be signed in to change notification settings - Fork 442
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
Make log_data required in mailer.send #9416
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -7,14 +7,10 @@ | |
|
||
|
||
class TestEmailService: | ||
def test_send_creates_email_message(self, email_service, pyramid_mailer): | ||
email = EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
email_service.send(email) | ||
def test_send_creates_email_message( | ||
self, email_data, log_data, email_service, pyramid_mailer | ||
): | ||
email_service.send(email_data, log_data) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
recipients=["[email protected]"], | ||
|
@@ -25,7 +21,7 @@ def test_send_creates_email_message(self, email_service, pyramid_mailer): | |
) | ||
|
||
def test_send_creates_email_message_with_html_body( | ||
self, email_service, pyramid_mailer | ||
self, log_data, email_service, pyramid_mailer | ||
): | ||
email = EmailData( | ||
recipients=["[email protected]"], | ||
|
@@ -34,7 +30,7 @@ def test_send_creates_email_message_with_html_body( | |
tag=EmailTag.TEST, | ||
html="<p>An HTML body</p>", | ||
) | ||
email_service.send(email) | ||
email_service.send(email, log_data) | ||
|
||
pyramid_mailer.message.Message.assert_called_once_with( | ||
recipients=["[email protected]"], | ||
|
@@ -45,60 +41,32 @@ def test_send_creates_email_message_with_html_body( | |
) | ||
|
||
def test_send_dispatches_email_using_request_mailer( | ||
self, email_service, pyramid_mailer | ||
self, email_data, log_data, email_service, pyramid_mailer | ||
): | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
message = pyramid_mailer.message.Message.return_value | ||
|
||
email = EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
email_service.send(email) | ||
email_service.send(email_data, log_data) | ||
|
||
request_mailer.send_immediately.assert_called_once_with(message) | ||
|
||
def test_raises_smtplib_exception(self, email_service, pyramid_mailer): | ||
def test_raises_smtplib_exception( | ||
self, email_data, log_data, email_service, pyramid_mailer | ||
): | ||
request_mailer = pyramid_mailer.get_mailer.return_value | ||
request_mailer.send_immediately.side_effect = smtplib.SMTPException() | ||
|
||
email = EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
with pytest.raises(smtplib.SMTPException): | ||
email_service.send(email) | ||
email_service.send(email_data, log_data) | ||
|
||
def test_send_logging(self, email_service, info_caplog): | ||
email_data = EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
user_id = 123 | ||
log_data = LogData( | ||
tag=email_data.tag, | ||
sender_id=user_id, | ||
recipient_ids=[user_id], | ||
) | ||
def test_send_logging(self, email_data, log_data, email_service, info_caplog): | ||
email_service.send(email_data, log_data) | ||
|
||
assert info_caplog.messages == [ | ||
f"Sent email: tag={log_data.tag!r}, sender_id={user_id}, recipient_ids={[user_id]}" | ||
f"Sent email: tag={log_data.tag!r}, sender_id={log_data.sender_id}, recipient_ids={log_data.recipient_ids}" | ||
] | ||
|
||
def test_send_logging_with_extra(self, email_service, info_caplog): | ||
email_data = EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
def test_send_logging_with_extra(self, email_data, email_service, info_caplog): | ||
user_id = 123 | ||
annotation_id = "annotation_id" | ||
log_data = LogData( | ||
|
@@ -113,6 +81,23 @@ def test_send_logging_with_extra(self, email_service, info_caplog): | |
f"Sent email: tag={log_data.tag!r}, sender_id={user_id}, recipient_ids={[user_id]}, annotation_id={annotation_id!r}" | ||
] | ||
|
||
@pytest.fixture | ||
def email_data(self): | ||
return EmailData( | ||
recipients=["[email protected]"], | ||
subject="My email subject", | ||
body="Some text body", | ||
tag=EmailTag.TEST, | ||
) | ||
|
||
@pytest.fixture | ||
def log_data(self): | ||
return LogData( | ||
tag=EmailTag.TEST, | ||
sender_id=123, | ||
recipient_ids=[123], | ||
) | ||
|
||
@pytest.fixture | ||
def pyramid_request(self, pyramid_request): | ||
pyramid_request.debug = False | ||
|
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import datetime | ||
from unittest.mock import sentinel | ||
from unittest.mock import call, create_autospec, sentinel | ||
|
||
import pytest | ||
from sqlalchemy.exc import IntegrityError | ||
|
||
from h.models import Activation, User | ||
from h.services.exceptions import ConflictError | ||
from h.services.user_signup import UserSignupService, user_signup_service_factory | ||
from h.tasks import mailer | ||
|
||
|
||
class TestUserSignupService: | ||
|
@@ -22,7 +23,6 @@ def test_signup_creates_user_in_db(self, db_session, svc): | |
db_session.close() | ||
|
||
user = db_session.query(User).filter_by(username="foo").one_or_none() | ||
|
||
assert user is not None | ||
|
||
def test_signup_creates_activation_for_user(self, svc): | ||
|
@@ -94,10 +94,9 @@ def test_signup_sets_password_using_password_service( | |
user_password_service.update_password.assert_called_once_with(user, "wibble") | ||
|
||
def test_signup_sends_email( | ||
self, svc, signup, tasks_mailer, pyramid_request, asdict | ||
self, svc, signup, tasks_mailer, pyramid_request, asdict, LogData | ||
): | ||
signup.generate.return_value = sentinel.email | ||
asdict.return_value = sentinel.email_data | ||
|
||
user = svc.signup(username="foo", email="[email protected]") | ||
|
||
|
@@ -108,8 +107,12 @@ def test_signup_sends_email( | |
activation_code=user.activation.code, | ||
) | ||
|
||
asdict.assert_called_once_with(signup.generate.return_value) | ||
tasks_mailer.send.delay.assert_called_once_with(asdict.return_value) | ||
asdict.assert_has_calls( | ||
[call(signup.generate.return_value), call(LogData.return_value)] | ||
) | ||
tasks_mailer.send.delay.assert_called_once_with( | ||
sentinel.email_data, sentinel.log_data | ||
) | ||
|
||
def test_signup_does_not_send_email_when_activation_not_required( | ||
self, svc, signup, tasks_mailer | ||
|
@@ -179,15 +182,20 @@ def svc(self, pyramid_request, user_password_service, subscription_service): | |
|
||
@pytest.fixture(autouse=True) | ||
def tasks_mailer(self, patch): | ||
return patch("h.services.user_signup.tasks_mailer") | ||
mock = patch("h.services.user_signup.tasks_mailer") | ||
mock.send.delay = create_autospec(mailer.send.run) | ||
return mock | ||
|
||
@pytest.fixture(autouse=True) | ||
def signup(self, patch): | ||
return patch("h.services.user_signup.signup") | ||
|
||
@pytest.fixture(autouse=True) | ||
def asdict(self, patch): | ||
return patch("h.services.user_signup.asdict") | ||
return patch( | ||
"h.services.user_signup.asdict", | ||
side_effect=[sentinel.email_data, sentinel.log_data], | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("user_password_service") | ||
|
@@ -212,3 +220,8 @@ def test_it( | |
@pytest.fixture | ||
def UserSignupService(self, patch): | ||
return patch("h.services.user_signup.UserSignupService") | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def LogData(patch): | ||
return patch("h.services.user_signup.LogData") |
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,52 +6,38 @@ | |
from h.tasks import mailer | ||
|
||
|
||
def test_send_without_log_data(email_service): | ||
email_data = { | ||
"recipients": ["[email protected]"], | ||
"subject": "My email subject", | ||
"body": "Some text body", | ||
"tag": EmailTag.TEST, | ||
} | ||
mailer.send(email_data) | ||
|
||
email_service.send.assert_called_once_with(EmailData(**email_data), None) | ||
|
||
|
||
def test_send_with_log_data(email_service): | ||
email_data = { | ||
"recipients": ["[email protected]"], | ||
"subject": "My email subject", | ||
"body": "Some text body", | ||
"tag": EmailTag.TEST, | ||
} | ||
log_data = { | ||
"sender_id": 123, | ||
"recipient_ids": [456], | ||
"tag": EmailTag.TEST, | ||
"extra": {"annotation_id": "annotation_id"}, | ||
} | ||
def test_send(email_data, log_data, email_service): | ||
mailer.send(email_data, log_data) | ||
|
||
email_service.send.assert_called_once_with( | ||
EmailData(**email_data), LogData(**log_data) | ||
) | ||
|
||
|
||
def test_send_retries_if_mailing_fails(email_service): | ||
def test_send_retries_if_mailing_fails(email_data, log_data, email_service): | ||
email_service.send.side_effect = Exception() | ||
mailer.send.retry = mock.Mock(wraps=mailer.send.retry) | ||
|
||
email_data = { | ||
with pytest.raises(Exception) as exc_info: # noqa: PT011 | ||
mailer.send(email_data, log_data) | ||
assert exc_info.type is Exception | ||
|
||
assert mailer.send.retry.called | ||
|
||
|
||
@pytest.fixture | ||
def email_data(): | ||
return { | ||
"recipients": ["[email protected]"], | ||
"subject": "My email subject", | ||
"body": "Some text body", | ||
"tag": EmailTag.TEST, | ||
} | ||
with pytest.raises(Exception): # noqa: B017, PT011 | ||
mailer.send(email_data) | ||
|
||
assert mailer.send.retry.called | ||
|
||
@pytest.fixture | ||
def log_data(): | ||
return {"tag": EmailTag.TEST, "sender_id": 123, "recipient_ids": [123]} | ||
|
||
|
||
@pytest.fixture | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autospec
doesn't affect celery tasksend
for whatever reason, will fix that in other tests as well.celery.app.task.Task.run
is what we need to emulate which is basically__wrapped__
on a decorator.What that means that if the service code and test code are in agreement we weren't checking if the service code calls
mailer.send
with a correct signature.Also can't use
patch
instead because ofpatch target has already been mocked out
error.