Skip to content

Commit

Permalink
Improve logging for suppressed email confirmation task + fix relative…
Browse files Browse the repository at this point in the history
… URL in verification email (#21843)

* Improve logging for suppressed email confirmation task

* Add site url to verify email template

* Use absolutify to generate confirmation code
  • Loading branch information
KevinMind committed Feb 20, 2024
1 parent 66c2417 commit d655f89
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/olympia/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import olympia.core.logger
from olympia.amo.celery import task
from olympia.amo.decorators import set_modified_on, use_primary_db
from olympia.amo.templatetags.jinja_helpers import absolutify
from olympia.amo.utils import (
SafeStorage,
backup_storage_enabled,
Expand Down Expand Up @@ -189,7 +190,7 @@ def send_suppressed_email_confirmation(suppressed_email_verification_id):

verification.status = SuppressedEmailVerification.STATUS_CHOICES.Pending

confirmation_link = (
confirmation_link = absolutify(
reverse('devhub.email_verification')
+ '?code='
+ str(verification.confirmation_code)
Expand Down Expand Up @@ -280,6 +281,13 @@ def check_suppressed_email_confirmation(suppressed_email_verification_id, page_s

if is_first_page:
total = json['total']

if total == 0:
raise Retry(
f'No emails found for email {email}.'
'retrying as email could not be queued yet'
)

is_first_page = False

data = json['data']
Expand All @@ -297,12 +305,14 @@ def check_suppressed_email_confirmation(suppressed_email_verification_id, page_s
f'expected {", ".join(options)}'
)

task_log.info(f'Found matching email {item}')

verification.update(
status=SuppressedEmailVerification.STATUS_CHOICES[item['status']]
)
return

raise Retry(
f'failed to find {code_snippet} in {total} emails.'
f'failed to find email for code: {code_snippet} in {total} emails.'
'retrying as email could not be queued yet'
)
37 changes: 35 additions & 2 deletions src/olympia/users/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from PIL import Image
from requests.exceptions import Timeout

from olympia.amo.templatetags.jinja_helpers import absolutify
from olympia.amo.tests import TestCase, user_factory
from olympia.amo.tests.test_helpers import get_image_path
from olympia.amo.utils import SafeStorage
Expand Down Expand Up @@ -392,7 +393,7 @@ def test_email_sent(self, mock_check_suppressed_email_confirmation):

assert len(mail.outbox) == 1

expected_confirmation_link = (
expected_confirmation_link = absolutify(
reverse('devhub.email_verification')
+ '?code='
+ str(verification.confirmation_code)
Expand Down Expand Up @@ -477,6 +478,36 @@ def test_socket_labs_returns_5xx(self):
with pytest.raises(Retry):
check_suppressed_email_confirmation.apply([verification.id])

def test_socket_labs_returns_empty(self):
verification = SuppressedEmailVerification.objects.create(
suppressed_email=SuppressedEmail.objects.create(
email=self.user_profile.email
),
)

responses.add(
responses.GET,
(
f'{settings.SOCKET_LABS_HOST}servers/{settings.SOCKET_LABS_SERVER_ID}/'
f'reports/recipient-search/'
),
status=200,
body=json.dumps(
{
'data': [],
'total': 0,
}
),
content_type='application/json',
)

with pytest.raises(Retry) as error_info:
check_suppressed_email_confirmation.apply([verification.id])

assert f'No emails found for email {self.user_profile.email}' in str(
error_info.value
)

def test_auth_header_present(self):
verification = SuppressedEmailVerification.objects.create(
suppressed_email=SuppressedEmail.objects.create(
Expand Down Expand Up @@ -730,5 +761,7 @@ def test_rsponse_does_not_contain_suppressed_email(self):
content_type='application/json',
)

with pytest.raises(Retry):
with pytest.raises(Retry) as error_info:
check_suppressed_email_confirmation.apply([verification.id, response_size])

assert 'failed to find email for code: ' in str(error_info.value)

0 comments on commit d655f89

Please sign in to comment.