Add unit tests

This commit is contained in:
zandercymatics 2024-06-04 14:44:19 -06:00
parent d120f31581
commit 805b7ca6c7
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 106 additions and 28 deletions

View file

@ -1444,20 +1444,25 @@ class TestDomainRequestAdmin(MockEppLib):
# The results are filtered by "status in [submitted,in review,action needed]"
self.assertContains(response, "status in [submitted,in review,action needed]", count=1)
def transition_state_and_send_email(self, domain_request, status, rejection_reason=None):
@less_console_noise_decorator
def transition_state_and_send_email(self, domain_request, status, rejection_reason=None, action_needed_reason=None):
"""Helper method for the email test cases."""
with boto3_mocking.clients.handler_for("sesv2", self.mock_client):
with less_console_noise():
# Create a mock request
request = self.factory.post("/admin/registrar/domainrequest/{}/change/".format(domain_request.pk))
# Create a mock request
request = self.factory.post("/admin/registrar/domainrequest/{}/change/".format(domain_request.pk))
# Modify the domain request's properties
domain_request.status = status
# Modify the domain request's properties
domain_request.status = status
if rejection_reason:
domain_request.rejection_reason = rejection_reason
# Use the model admin's save_model method
self.admin.save_model(request, domain_request, form=None, change=True)
if action_needed_reason:
domain_request.action_needed_reason = action_needed_reason
# Use the model admin's save_model method
self.admin.save_model(request, domain_request, form=None, change=True)
def assert_email_is_accurate(
self, expected_string, email_index, email_address, test_that_no_bcc=False, bcc_email_address=""
@ -1492,6 +1497,64 @@ class TestDomainRequestAdmin(MockEppLib):
bcc_email = kwargs["Destination"]["BccAddresses"][0]
self.assertEqual(bcc_email, bcc_email_address)
def test_action_needed_sends_reason_email(self):
"""When an action needed reason is set, an email is sent out."""
# Ensure there is no user with this email
EMAIL = "mayor@igorville.gov"
User.objects.filter(email=EMAIL).delete()
in_review = DomainRequest.DomainRequestStatus.IN_REVIEW
action_needed = DomainRequest.DomainRequestStatus.ACTION_NEEDED
# Create a sample domain request
domain_request = completed_domain_request(status=in_review)
# Test the email sent out for already_has_domains
already_has_domains = DomainRequest.ActionNeededReasons.ALREADY_HAS_DOMAINS
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=already_has_domains)
self.assert_email_is_accurate("ORGANIZATION ALREADY HAS A .GOV DOMAIN", 0, EMAIL, True)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 1)
# Revert back to in review to reset for the next assert
domain_request.status = DomainRequest.DomainRequestStatus.IN_REVIEW
domain_request.save()
# Test the email sent out for bad_name
bad_name = DomainRequest.ActionNeededReasons.BAD_NAME
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=bad_name)
self.assert_email_is_accurate("DOMAIN NAME DOES NOT MEET .GOV REQUIREMENTS", 1, EMAIL, True)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 2)
# Revert back to in review to reset for the next assert
domain_request.status = DomainRequest.DomainRequestStatus.IN_REVIEW
domain_request.save()
# Test the email sent out for eligibility_unclear
eligibility_unclear = DomainRequest.ActionNeededReasons.ELIGIBILITY_UNCLEAR
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=eligibility_unclear)
self.assert_email_is_accurate("ORGANIZATION MAY NOT MEET ELIGIBILITY REQUIREMENTS", 2, EMAIL, True)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 3)
# Revert back to in review to reset for the next assert
domain_request.status = DomainRequest.DomainRequestStatus.IN_REVIEW
domain_request.save()
# Test the email sent out for questionable_ao
questionable_ao = DomainRequest.ActionNeededReasons.QUESTIONABLE_AUTHORIZING_OFFICIAL
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=questionable_ao)
self.assert_email_is_accurate("AUTHORIZING OFFICIAL DOES NOT MEET ELIGIBILITY REQUIREMENTS", 3, EMAIL, True)
self.assertEqual(len(self.mock_client.EMAILS_SENT), 4)
# Revert back to in review to reset for the next assert
domain_request.status = DomainRequest.DomainRequestStatus.IN_REVIEW
domain_request.save()
# Assert that no other emails are sent on OTHER
other = DomainRequest.ActionNeededReasons.OTHER
self.transition_state_and_send_email(domain_request, action_needed, action_needed_reason=other)
# Should be unchanged from before
self.assertEqual(len(self.mock_client.EMAILS_SENT), 4)
def test_save_model_sends_submitted_email(self):
"""When transitioning to submitted from started or withdrawn on a domain request,
an email is sent out.