Skip to content

Commit

Permalink
sending emails: handle case where we want to email confirming account…
Browse files Browse the repository at this point in the history
… deletion (#13396)
  • Loading branch information
ewdurbin authored Apr 8, 2023
1 parent ff5db0f commit f29cda4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
32 changes: 19 additions & 13 deletions tests/unit/email/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def test_sends_unverified_with_override(


class TestSendEmail:
def test_send_email_success(self, db_session, monkeypatch):
@pytest.mark.parametrize("delete_user", [True, False])
def test_send_email_success(self, delete_user, db_session, monkeypatch):
class FakeMailSender:
def __init__(self):
self.emails = []
Expand Down Expand Up @@ -321,6 +322,8 @@ def __init__(self):
self.user = FakeUser()

def get_user(self, user_id):
if delete_user:
return None
return self.user

user_service = FakeUserEventService()
Expand Down Expand Up @@ -373,18 +376,21 @@ def get_user(self, user_id):
"recipient": "recipient",
}
]
assert user_service.user.events == [
{
"tag": "account:email:sent",
"ip_address": request.remote_addr,
"additional": {
"from_": "[email protected]",
"to": "recipient",
"subject": msg.subject,
"redact_ip": False,
},
}
]
if delete_user:
assert user_service.user.events == []
else:
assert user_service.user.events == [
{
"tag": "account:email:sent",
"ip_address": request.remote_addr,
"additional": {
"from_": "[email protected]",
"to": "recipient",
"subject": msg.subject,
"redact_ip": False,
},
}
]

def test_send_email_failure_retry(self, monkeypatch):
exc = Exception()
Expand Down
3 changes: 2 additions & 1 deletion warehouse/email/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def send_email(task, request, recipient, msg, success_event):
sender.send(recipient, msg)
user_service = request.find_service(IUserService, context=None)
user = user_service.get_user(success_event.pop("user_id"))
user.record_event(**success_event)
if user is not None: # We send account deletion confirmation emails
user.record_event(**success_event)
except (BadHeaders, EncodingError, InvalidMessage) as exc:
raise exc
except Exception as exc:
Expand Down

0 comments on commit f29cda4

Please sign in to comment.