Add unit tests

This commit is contained in:
zandercymatics 2024-06-25 10:44:10 -06:00
parent b45d5cebbc
commit aa9a7342cc
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -23,6 +23,7 @@ from registrar.utility.constants import BranchChoices
from .common import MockSESClient, less_console_noise, completed_domain_request, set_domain_request_investigators from .common import MockSESClient, less_console_noise, completed_domain_request, set_domain_request_investigators
from django_fsm import TransitionNotAllowed from django_fsm import TransitionNotAllowed
from waffle.testutils import override_flag
# Test comment for push -- will remove # Test comment for push -- will remove
@ -31,29 +32,44 @@ from django_fsm import TransitionNotAllowed
@boto3_mocking.patching @boto3_mocking.patching
class TestDomainRequest(TestCase): class TestDomainRequest(TestCase):
def setUp(self): def setUp(self):
self.dummy_user, _ = Contact.objects.get_or_create(
email="mayor@igorville.com", first_name="Hello", last_name="World"
)
self.dummy_user_2, _ = User.objects.get_or_create(
username="intern@igorville.com", email="intern@igorville.com", first_name="Lava", last_name="World"
)
self.started_domain_request = completed_domain_request( self.started_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED, name="started.gov" status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
) )
self.submitted_domain_request = completed_domain_request( self.submitted_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.SUBMITTED, name="submitted.gov" status=DomainRequest.DomainRequestStatus.SUBMITTED,
name="submitted.gov",
) )
self.in_review_domain_request = completed_domain_request( self.in_review_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.IN_REVIEW, name="in-review.gov" status=DomainRequest.DomainRequestStatus.IN_REVIEW,
name="in-review.gov",
) )
self.action_needed_domain_request = completed_domain_request( self.action_needed_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.ACTION_NEEDED, name="action-needed.gov" status=DomainRequest.DomainRequestStatus.ACTION_NEEDED,
name="action-needed.gov",
) )
self.approved_domain_request = completed_domain_request( self.approved_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.APPROVED, name="approved.gov" status=DomainRequest.DomainRequestStatus.APPROVED,
name="approved.gov",
) )
self.withdrawn_domain_request = completed_domain_request( self.withdrawn_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.WITHDRAWN, name="withdrawn.gov" status=DomainRequest.DomainRequestStatus.WITHDRAWN,
name="withdrawn.gov",
) )
self.rejected_domain_request = completed_domain_request( self.rejected_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.REJECTED, name="rejected.gov" status=DomainRequest.DomainRequestStatus.REJECTED,
name="rejected.gov",
) )
self.ineligible_domain_request = completed_domain_request( self.ineligible_domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.INELIGIBLE, name="ineligible.gov" status=DomainRequest.DomainRequestStatus.INELIGIBLE,
name="ineligible.gov",
) )
# Store all domain request statuses in a variable for ease of use # Store all domain request statuses in a variable for ease of use
@ -197,7 +213,9 @@ class TestDomainRequest(TestCase):
domain_request.submit() domain_request.submit()
self.assertEqual(domain_request.status, domain_request.DomainRequestStatus.SUBMITTED) self.assertEqual(domain_request.status, domain_request.DomainRequestStatus.SUBMITTED)
def check_email_sent(self, domain_request, msg, action, expected_count): def check_email_sent(
self, domain_request, msg, action, expected_count, expected_content=None, expected_email="mayor@igorville.com"
):
"""Check if an email was sent after performing an action.""" """Check if an email was sent after performing an action."""
with self.subTest(msg=msg, action=action): with self.subTest(msg=msg, action=action):
@ -211,19 +229,35 @@ class TestDomainRequest(TestCase):
sent_emails = [ sent_emails = [
email email
for email in MockSESClient.EMAILS_SENT for email in MockSESClient.EMAILS_SENT
if "mayor@igorville.gov" in email["kwargs"]["Destination"]["ToAddresses"] if expected_email in email["kwargs"]["Destination"]["ToAddresses"]
] ]
self.assertEqual(len(sent_emails), expected_count) self.assertEqual(len(sent_emails), expected_count)
if expected_content:
email_content = sent_emails[0]["kwargs"]["Content"]["Simple"]["Body"]["Text"]["Data"]
self.assertIn(expected_content, email_content)
@override_flag("profile_feature", active=False)
def test_submit_from_started_sends_email(self): def test_submit_from_started_sends_email(self):
msg = "Create a domain request and submit it and see if email was sent." msg = "Create a domain request and submit it and see if email was sent."
domain_request = completed_domain_request() domain_request = completed_domain_request(submitter=self.dummy_user, user=self.dummy_user_2)
self.check_email_sent(domain_request, msg, "submit", 1) self.check_email_sent(domain_request, msg, "submit", 1, expected_content="Hello")
@override_flag("profile_feature", active=True)
def test_submit_from_started_sends_email_to_creator(self):
"""Tests if, when the profile feature flag is on, we send an email to the creator"""
msg = "Create a domain request and submit it and see if email was sent when the feature flag is on."
domain_request = completed_domain_request(submitter=self.dummy_user, user=self.dummy_user_2)
self.check_email_sent(
domain_request, msg, "submit", 1, expected_content="Lava", expected_email="intern@igorville.com"
)
def test_submit_from_withdrawn_sends_email(self): def test_submit_from_withdrawn_sends_email(self):
msg = "Create a withdrawn domain request and submit it and see if email was sent." msg = "Create a withdrawn domain request and submit it and see if email was sent."
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.WITHDRAWN) domain_request = completed_domain_request(
self.check_email_sent(domain_request, msg, "submit", 1) status=DomainRequest.DomainRequestStatus.WITHDRAWN, submitter=self.dummy_user
)
self.check_email_sent(domain_request, msg, "submit", 1, expected_content="Hello")
def test_submit_from_action_needed_does_not_send_email(self): def test_submit_from_action_needed_does_not_send_email(self):
msg = "Create a domain request with ACTION_NEEDED status and submit it, check if email was not sent." msg = "Create a domain request with ACTION_NEEDED status and submit it, check if email was not sent."
@ -237,18 +271,24 @@ class TestDomainRequest(TestCase):
def test_approve_sends_email(self): def test_approve_sends_email(self):
msg = "Create a domain request and approve it and see if email was sent." msg = "Create a domain request and approve it and see if email was sent."
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.IN_REVIEW) domain_request = completed_domain_request(
self.check_email_sent(domain_request, msg, "approve", 1) status=DomainRequest.DomainRequestStatus.IN_REVIEW, submitter=self.dummy_user
)
self.check_email_sent(domain_request, msg, "approve", 1, expected_content="Hello")
def test_withdraw_sends_email(self): def test_withdraw_sends_email(self):
msg = "Create a domain request and withdraw it and see if email was sent." msg = "Create a domain request and withdraw it and see if email was sent."
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.IN_REVIEW) domain_request = completed_domain_request(
self.check_email_sent(domain_request, msg, "withdraw", 1) status=DomainRequest.DomainRequestStatus.IN_REVIEW, submitter=self.dummy_user
)
self.check_email_sent(domain_request, msg, "withdraw", 1, expected_content="Hello")
def test_reject_sends_email(self): def test_reject_sends_email(self):
msg = "Create a domain request and reject it and see if email was sent." msg = "Create a domain request and reject it and see if email was sent."
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.APPROVED) domain_request = completed_domain_request(
self.check_email_sent(domain_request, msg, "reject", 1) status=DomainRequest.DomainRequestStatus.APPROVED, submitter=self.dummy_user
)
self.check_email_sent(domain_request, msg, "reject", 1, expected_content="Hello")
def test_reject_with_prejudice_does_not_send_email(self): def test_reject_with_prejudice_does_not_send_email(self):
msg = "Create a domain request and reject it with prejudice and see if email was sent." msg = "Create a domain request and reject it with prejudice and see if email was sent."