Merge pull request #2219 from cisagov/za/2154-enable-disable-email-sending

Ticket #2154: enable/disable email sending
This commit is contained in:
zandercymatics 2024-06-03 13:23:10 -06:00 committed by GitHub
commit 16b4c54770
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 38 additions and 4 deletions

View file

@ -405,3 +405,9 @@ This function is triggered by the post_save event on the User model, designed to
1. For New Users: Upon the creation of a new user, it checks for an existing `Contact` by email. If no matching contact is found, it creates a new Contact using the user's details from Login.gov. If a matching contact is found, it associates this contact with the user. In cases where multiple contacts with the same email exist, it logs a warning and associates the first contact found. 1. For New Users: Upon the creation of a new user, it checks for an existing `Contact` by email. If no matching contact is found, it creates a new Contact using the user's details from Login.gov. If a matching contact is found, it associates this contact with the user. In cases where multiple contacts with the same email exist, it logs a warning and associates the first contact found.
2. For Existing Users: For users logging in subsequent times, the function ensures that any updates from Login.gov are applied to the associated User record. However, it does not alter any existing Contact records. 2. For Existing Users: For users logging in subsequent times, the function ensures that any updates from Login.gov are applied to the associated User record. However, it does not alter any existing Contact records.
## Disable email sending (toggling the disable_email_sending flag)
1. On the app, navigate to `\admin`.
2. Under models, click `Waffle flags`.
3. Click the `disable_email_sending` record. This should exist by default, if not - create one with that name.
4. (Important) Set the field `everyone` to `Yes`. This field overrides all other settings

View file

@ -2305,6 +2305,8 @@ class UserGroupAdmin(AuditedAdmin):
class WaffleFlagAdmin(FlagAdmin): class WaffleFlagAdmin(FlagAdmin):
"""Custom admin implementation of django-waffle's Flag class"""
class Meta: class Meta:
"""Contains meta information about this class""" """Contains meta information about this class"""
@ -2338,6 +2340,6 @@ admin.site.register(models.VerifiedByStaff, VerifiedByStaffAdmin)
# Register our custom waffle implementations # Register our custom waffle implementations
admin.site.register(models.WaffleFlag, WaffleFlagAdmin) admin.site.register(models.WaffleFlag, WaffleFlagAdmin)
# Unregister Sample and Switch from the waffle library # Unregister Switch and Sample from the waffle library
admin.site.unregister(Sample)
admin.site.unregister(Switch) admin.site.unregister(Switch)
admin.site.unregister(Sample)

View file

@ -326,7 +326,7 @@ SERVER_EMAIL = "root@get.gov"
# endregion # endregion
# region: Waffle feature flags-----------------------------------------------------------### # region: Waffle feature flags-----------------------------------------------------------###
# If Waffle encounters a reference to a flag that is not in the database, should Waffle create the flag? # If Waffle encounters a reference to a flag that is not in the database, create the flag automagically.
WAFFLE_CREATE_MISSING_FLAGS = True WAFFLE_CREATE_MISSING_FLAGS = True
# The model that will be used to keep track of flags. Extends AbstractUserFlag. # The model that will be used to keep track of flags. Extends AbstractUserFlag.

View file

@ -3,10 +3,12 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
from django.test import TestCase from django.test import TestCase
from waffle.testutils import override_flag
from registrar.utility import email
from registrar.utility.email import send_templated_email
from .common import completed_domain_request, less_console_noise from .common import completed_domain_request, less_console_noise
from datetime import datetime from datetime import datetime
from registrar.utility import email
import boto3_mocking # type: ignore import boto3_mocking # type: ignore
@ -15,6 +17,24 @@ class TestEmails(TestCase):
self.mock_client_class = MagicMock() self.mock_client_class = MagicMock()
self.mock_client = self.mock_client_class.return_value self.mock_client = self.mock_client_class.return_value
@boto3_mocking.patching
@override_flag("disable_email_sending", active=True)
def test_disable_email_flag(self):
"""Test if the 'disable_email_sending' stops emails from being sent"""
with boto3_mocking.clients.handler_for("sesv2", self.mock_client_class):
expected_message = "Email sending is disabled due to"
with self.assertRaisesRegex(email.EmailSendingError, expected_message):
send_templated_email(
"test content",
"test subject",
"doesnotexist@igorville.com",
context={"domain_request": self},
bcc_address=None,
)
# Assert that an email wasn't sent
self.assertFalse(self.mock_client.send_email.called)
@boto3_mocking.patching @boto3_mocking.patching
def test_submission_confirmation(self): def test_submission_confirmation(self):
"""Submission confirmation email works.""" """Submission confirmation email works."""

View file

@ -8,6 +8,7 @@ from django.template.loader import get_template
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from waffle import flag_is_active
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -35,6 +36,11 @@ def send_templated_email(
Raises EmailSendingError if SES client could not be accessed Raises EmailSendingError if SES client could not be accessed
""" """
if flag_is_active(None, "disable_email_sending") and not settings.IS_PRODUCTION: # type: ignore
message = "Could not send email. Email sending is disabled due to flag 'disable_email_sending'."
raise EmailSendingError(message)
template = get_template(template_name) template = get_template(template_name)
email_body = template.render(context=context) email_body = template.render(context=context)