Skip to content

Commit

Permalink
Added functionality to send email whenever primary email is changed (p…
Browse files Browse the repository at this point in the history
…ypi#3158)

Addressed code reviews
  • Loading branch information
waseem18 authored and di committed Mar 7, 2018
1 parent bc48e99 commit c8ed765
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/unit/manage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,10 @@ def test_change_primary_email(self, monkeypatch, db_request):
user = UserFactory()
old_primary = EmailFactory(primary=True, user=user)
new_primary = EmailFactory(primary=False, verified=True, user=user)
email = EmailFactory(user=user)

db_request.user = user

db_request.find_service = lambda *a, **kw: pretend.stub()
db_request.POST = {'primary_email_id': new_primary.id}
db_request.session.flash = pretend.call_recorder(lambda *a, **kw: None)
Expand All @@ -389,7 +391,14 @@ def test_change_primary_email(self, monkeypatch, db_request):
)
view = views.ManageAccountViews(db_request)

send_email = pretend.call_recorder(lambda *a: None)
monkeypatch.setattr(
views, 'send_primary_email_change_email', send_email
)
assert view.change_primary_email() == view.default_response
assert send_email.calls == [
pretend.call(db_request, db_request.user, email.email)
]
assert db_request.session.flash.calls == [
pretend.call(
f'Email address {new_primary.email} set as primary.',
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,50 @@ def test_account_deletion_email(
assert send_email.delay.calls == [
pretend.call('Email Body', [stub_user.email], 'Email Subject'),
]


class TestPrimaryEmailChangeEmail:

def test_primary_email_change_email(
self, pyramid_request, pyramid_config, monkeypatch):

stub_user = pretend.stub(
email='new_email',
username='username',
)
subject_renderer = pyramid_config.testing_add_renderer(
'email/primary-email-change.subject.txt'
)
subject_renderer.string_response = 'Email Subject'
body_renderer = pyramid_config.testing_add_renderer(
'email/primary-email-change.body.txt'
)
body_renderer.string_response = 'Email Body'

send_email = pretend.stub(
delay=pretend.call_recorder(lambda *args, **kwargs: None)
)
pyramid_request.task = pretend.call_recorder(
lambda *args, **kwargs: send_email
)
monkeypatch.setattr(email, 'send_email', send_email)

result = email.send_primary_email_change_email(
pyramid_request,
stub_user,
"old_email"
)

assert result == {
'username': stub_user.username,
'old_email': "old_email",
'new_email': stub_user.email
}
subject_renderer.assert_()
body_renderer.assert_(username=stub_user.username)
assert pyramid_request.task.calls == [
pretend.call(send_email),
]
assert send_email.delay.calls == [
pretend.call('Email Body', ['old_email'], 'Email Subject'),
]
20 changes: 20 additions & 0 deletions warehouse/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,23 @@ def send_account_deletion_email(request, user):
request.task(send_email).delay(body, [user.email], subject)

return fields


def send_primary_email_change_email(request, user, email):
fields = {
'username': user.username,
'old_email': email,
'new_email': user.email,
}

subject = render(
'email/primary-email-change.subject.txt', fields, request=request
)

body = render(
'email/primary-email-change.body.txt', fields, request=request
)

request.task(send_email).delay(body, [email], subject)

return fields
6 changes: 5 additions & 1 deletion warehouse/manage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from warehouse.accounts.views import logout
from warehouse.email import (
send_account_deletion_email, send_email_verification_email,
send_password_change_email,
send_password_change_email, send_primary_email_change_email
)
from warehouse.manage.forms import (
AddEmailForm, ChangePasswordForm, CreateRoleForm, ChangeRoleForm,
Expand Down Expand Up @@ -163,6 +163,7 @@ def delete_email(self):
request_param=["primary_email_id"],
)
def change_primary_email(self):
previous_primary_email = self.request.user.email
try:
new_primary_email = self.request.db.query(Email).filter(
Email.user_id == self.request.user.id,
Expand All @@ -187,6 +188,9 @@ def change_primary_email(self):
queue='success',
)

send_primary_email_change_email(
self.request, self.request.user, previous_primary_email
)
return self.default_response

@view_config(
Expand Down
14 changes: 14 additions & 0 deletions warehouse/templates/email/primary-email-change.body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
The primary email for your PyPI account:

'{{ username }}'

been changed from this address:

{{ old_email }}

to this address:

{{ new_email }}

If you did not make this change, you can reply to this email directly to
communicate with the PyPI administrators.
1 change: 1 addition & 0 deletions warehouse/templates/email/primary-email-change.subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyPI Primary Email Change Notification

0 comments on commit c8ed765

Please sign in to comment.