fix tests

This commit is contained in:
zandercymatics 2024-08-23 13:34:33 -06:00
parent 86f520c3f6
commit 866204cb8d
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 40 additions and 4 deletions

View file

@ -1951,11 +1951,8 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
allowed = models.AllowedEmail.is_allowed_email(email) allowed = models.AllowedEmail.is_allowed_email(email)
error_message = f"Could not send email. The email '{email}' does not exist within the whitelist." error_message = f"Could not send email. The email '{email}' does not exist within the whitelist."
success_message = f"An email to '{email}' was sent!"
if not allowed: if not allowed:
messages.warning(request, error_message) messages.warning(request, error_message)
else:
messages.success(request, success_message)
def _handle_status_change(self, request, obj, original_obj): def _handle_status_change(self, request, obj, original_obj):
""" """

View file

@ -22,6 +22,7 @@ from registrar.models import (
Contact, Contact,
Website, Website,
SeniorOfficial, SeniorOfficial,
AllowedEmail,
) )
from .common import ( from .common import (
MockSESClient, MockSESClient,
@ -52,6 +53,10 @@ class TestDomainRequestAdmin(MockEppLib):
tests have available staffuser, superuser, client, admin and test_helper tests have available staffuser, superuser, client, admin and test_helper
""" """
@classmethod
def tearDownClass(cls):
super().tearDownClass()
@classmethod @classmethod
def setUpClass(self): def setUpClass(self):
super().setUpClass() super().setUpClass()
@ -84,6 +89,7 @@ class TestDomainRequestAdmin(MockEppLib):
def tearDownClass(self): def tearDownClass(self):
super().tearDownClass() super().tearDownClass()
User.objects.all().delete() User.objects.all().delete()
AllowedEmail.objects.all().delete()
@less_console_noise_decorator @less_console_noise_decorator
def test_domain_request_senior_official_is_alphabetically_sorted(self): def test_domain_request_senior_official_is_alphabetically_sorted(self):
@ -597,7 +603,8 @@ class TestDomainRequestAdmin(MockEppLib):
): ):
"""Helper method for the email test cases. """Helper method for the email test cases.
email_index is the index of the email in mock_client.""" email_index is the index of the email in mock_client."""
allowed_email, _ = AllowedEmail.objects.get_or_create(email=email_address)
allowed_bcc_email, _ = AllowedEmail.objects.get_or_create(email=bcc_email_address)
with less_console_noise(): with less_console_noise():
# Access the arguments passed to send_email # Access the arguments passed to send_email
call_args = self.mock_client.EMAILS_SENT call_args = self.mock_client.EMAILS_SENT
@ -624,6 +631,9 @@ class TestDomainRequestAdmin(MockEppLib):
if bcc_email_address: if bcc_email_address:
bcc_email = kwargs["Destination"]["BccAddresses"][0] bcc_email = kwargs["Destination"]["BccAddresses"][0]
self.assertEqual(bcc_email, bcc_email_address) self.assertEqual(bcc_email, bcc_email_address)
allowed_email.delete()
allowed_bcc_email.delete()
@override_settings(IS_PRODUCTION=True) @override_settings(IS_PRODUCTION=True)
@less_console_noise_decorator @less_console_noise_decorator
@ -1686,6 +1696,8 @@ class TestDomainRequestAdmin(MockEppLib):
# Patch Domain.is_active and django.contrib.messages.error simultaneously # Patch Domain.is_active and django.contrib.messages.error simultaneously
stack.enter_context(patch.object(Domain, "is_active", custom_is_active)) stack.enter_context(patch.object(Domain, "is_active", custom_is_active))
stack.enter_context(patch.object(messages, "error")) stack.enter_context(patch.object(messages, "error"))
stack.enter_context(patch.object(messages, "warning"))
stack.enter_context(patch.object(messages, "success"))
domain_request.status = another_state domain_request.status = another_state

View file

@ -7,6 +7,7 @@ from waffle.testutils import override_flag
from registrar.utility import email from registrar.utility import email
from registrar.utility.email import send_templated_email from registrar.utility.email import send_templated_email
from .common import completed_domain_request from .common import completed_domain_request
from registrar.models import AllowedEmail
from api.tests.common import less_console_noise_decorator from api.tests.common import less_console_noise_decorator
from datetime import datetime from datetime import datetime
@ -14,9 +15,32 @@ import boto3_mocking # type: ignore
class TestEmails(TestCase): class TestEmails(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
allowed_emails = [
AllowedEmail(email="doesnotexist@igorville.com"),
AllowedEmail(email="testy@town.com"),
AllowedEmail(email="mayor@igorville.gov"),
AllowedEmail(email="testy2@town.com"),
AllowedEmail(email="cisaRep@igorville.gov"),
AllowedEmail(email="sender@example.com"),
AllowedEmail(email="recipient@example.com"),
]
AllowedEmail.objects.bulk_create(allowed_emails)
@classmethod
def tearDownClass(cls):
super().tearDownClass()
AllowedEmail.objects.all().delete()
def setUp(self): def setUp(self):
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
def tearDown(self):
super().tearDown()
@boto3_mocking.patching @boto3_mocking.patching
@override_flag("disable_email_sending", active=True) @override_flag("disable_email_sending", active=True)

View file

@ -27,6 +27,7 @@ from registrar.models import (
Domain, Domain,
DomainInformation, DomainInformation,
DomainInvitation, DomainInvitation,
AllowedEmail,
Contact, Contact,
PublicContact, PublicContact,
Host, Host,
@ -460,6 +461,7 @@ class TestDomainManagers(TestDomainOverview):
"""Inviting a non-existent user sends them an email.""" """Inviting a non-existent user sends them an email."""
# make sure there is no user with this email # make sure there is no user with this email
email_address = "mayor@igorville.gov" email_address = "mayor@igorville.gov"
allowed_email, _ = AllowedEmail.objects.get_or_create(email=email_address)
User.objects.filter(email=email_address).delete() User.objects.filter(email=email_address).delete()
self.domain_information, _ = DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain) self.domain_information, _ = DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain)
@ -479,6 +481,7 @@ class TestDomainManagers(TestDomainOverview):
Destination={"ToAddresses": [email_address]}, Destination={"ToAddresses": [email_address]},
Content=ANY, Content=ANY,
) )
allowed_email.delete()
@boto3_mocking.patching @boto3_mocking.patching
@less_console_noise_decorator @less_console_noise_decorator