This commit is contained in:
zandercymatics 2024-08-23 14:57:49 -06:00
parent 056df7151d
commit e84f346e4b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 36 additions and 37 deletions

View file

@ -1915,20 +1915,8 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
else:
obj.action_needed_reason_email = default_email
if obj.status in DomainRequest.get_statuses_that_send_emails():
if not settings.IS_PRODUCTION:
profile_flag = flag_is_active(None, "profile_feature")
if profile_flag and hasattr(obj, "creator"):
recipient = obj.creator
elif not profile_flag and hasattr(obj, "submitter"):
recipient = obj.submitter
else:
recipient = None
# Displays a warning in admin when an email cannot be sent,
# Or a success message if it was.
if recipient and recipient.email:
self._check_for_valid_email(request, recipient.email)
if obj.status in DomainRequest.get_statuses_that_send_emails() and not settings.IS_PRODUCTION:
self._check_for_valid_email(request, obj)
# == Handle status == #
if obj.status == original_obj.status:
@ -1942,12 +1930,23 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
if should_save:
return super().save_model(request, obj, form, change)
def _check_for_valid_email(self, request, email):
def _check_for_valid_email(self, request, obj):
"""Certain emails are whitelisted in non-production environments,
so we should display that information using this function.
"""
profile_flag = flag_is_active(request, "profile_feature")
if profile_flag and hasattr(obj, "creator"):
recipient = obj.creator
elif not profile_flag and hasattr(obj, "submitter"):
recipient = obj.submitter
else:
recipient = None
# Displays a warning in admin when an email cannot be sent,
# Or a success message if it was.
if recipient and recipient.email:
email = recipient.email
allowed = models.AllowedEmail.is_allowed_email(email)
error_message = f"Could not send email. The email '{email}' does not exist within the whitelist."
if not allowed:

View file

@ -53,11 +53,6 @@ class TestDomainRequestAdmin(MockEppLib):
tests have available staffuser, superuser, client, admin and test_helper
"""
@classmethod
def tearDownClass(cls):
super().tearDownClass()
AllowedEmail.objects.all.delete()
@classmethod
def setUpClass(self):
super().setUpClass()

View file

@ -583,7 +583,7 @@ class TestDomainManagers(TestDomainOverview):
"""Inviting a user sends them an email, with email as the name."""
# Create a fake user object
email_address = "mayor@igorville.gov"
allowed_email = AllowedEmail.objects.get_or_create(email=email_address)
AllowedEmail.objects.get_or_create(email=email_address)
User.objects.get_or_create(email=email_address, username="fakeuser@fakeymail.com")
# Make sure the user is staff

View file

@ -41,18 +41,7 @@ def send_templated_email(
"""
if not settings.IS_PRODUCTION: # type: ignore
if flag_is_active(None, "disable_email_sending"): # type: ignore
message = "Could not send email. Email sending is disabled due to flag 'disable_email_sending'."
raise EmailSendingError(message)
else:
# Raise an email sending error if these doesn't exist within our whitelist.
# If these emails don't exist, this function can handle that elsewhere.
AllowedEmail = apps.get_model("registrar", "AllowedEmail")
message = "Could not send email. The email '{}' does not exist within the whitelist."
if to_address and not AllowedEmail.is_allowed_email(to_address):
raise EmailSendingError(message.format(to_address))
if bcc_address and not AllowedEmail.is_allowed_email(bcc_address):
raise EmailSendingError(message.format(bcc_address))
_can_send_email(to_address, bcc_address)
template = get_template(template_name)
email_body = template.render(context=context)
@ -113,6 +102,22 @@ def send_templated_email(
raise EmailSendingError("Could not send SES email.") from exc
def _can_send_email(to_address, bcc_address):
"""Raises an error if we cannot send an error"""
if flag_is_active(None, "disable_email_sending"): # type: ignore
message = "Could not send email. Email sending is disabled due to flag 'disable_email_sending'."
raise EmailSendingError(message)
else:
# Raise an email sending error if these doesn't exist within our whitelist.
# If these emails don't exist, this function can handle that elsewhere.
AllowedEmail = apps.get_model("registrar", "AllowedEmail")
message = "Could not send email. The email '{}' does not exist within the whitelist."
if to_address and not AllowedEmail.is_allowed_email(to_address):
raise EmailSendingError(message.format(to_address))
if bcc_address and not AllowedEmail.is_allowed_email(bcc_address):
raise EmailSendingError(message.format(bcc_address))
def wrap_text_and_preserve_paragraphs(text, width):
"""
Wraps text to `width` preserving newlines; splits on '\n', wraps segments, rejoins with '\n'.