diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 48a5b61c3..0fe3a2c38 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -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,16 +1930,27 @@ 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 - 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: - messages.warning(request, error_message) + # 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: + messages.warning(request, error_message) def _handle_status_change(self, request, obj, original_obj): """ diff --git a/src/registrar/tests/test_admin_request.py b/src/registrar/tests/test_admin_request.py index d82826f33..46b7c22f2 100644 --- a/src/registrar/tests/test_admin_request.py +++ b/src/registrar/tests/test_admin_request.py @@ -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() diff --git a/src/registrar/tests/test_views_domain.py b/src/registrar/tests/test_views_domain.py index 0f8b59995..ae3689703 100644 --- a/src/registrar/tests/test_views_domain.py +++ b/src/registrar/tests/test_views_domain.py @@ -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 diff --git a/src/registrar/utility/email.py b/src/registrar/utility/email.py index d77de3ed0..533bd9e99 100644 --- a/src/registrar/utility/email.py +++ b/src/registrar/utility/email.py @@ -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'.