unit tests

This commit is contained in:
zandercymatics 2024-12-19 14:05:06 -07:00
parent 9e339c2cd9
commit de8d252136
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 159 additions and 1 deletions

View file

@ -1034,6 +1034,10 @@ def completed_domain_request( # noqa
action_needed_reason=None,
portfolio=None,
organization_name=None,
sub_organization=None,
requested_suborganization=None,
suborganization_city=None,
suborganization_state_territory=None,
):
"""A completed domain request."""
if not user:
@ -1098,6 +1102,18 @@ def completed_domain_request( # noqa
if portfolio:
domain_request_kwargs["portfolio"] = portfolio
if sub_organization:
domain_request_kwargs["sub_organization"] = sub_organization
if requested_suborganization:
domain_request_kwargs["requested_suborganization"] = requested_suborganization
if suborganization_city:
domain_request_kwargs["suborganization_city"] = suborganization_city
if suborganization_state_territory:
domain_request_kwargs["suborganization_state_territory"] = suborganization_state_territory
domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs)
if has_other_contacts:

View file

@ -15,6 +15,7 @@ from registrar.models import (
FederalAgency,
AllowedEmail,
Portfolio,
Suborganization,
)
import boto3_mocking
@ -23,6 +24,8 @@ from registrar.utility.errors import FSMDomainRequestError
from .common import (
MockSESClient,
create_user,
create_superuser,
less_console_noise,
completed_domain_request,
set_domain_request_investigators,
@ -1070,3 +1073,142 @@ class TestDomainRequest(TestCase):
)
self.assertEqual(domain_request2.generic_org_type, domain_request2.converted_generic_org_type)
self.assertEqual(domain_request2.federal_agency, domain_request2.converted_federal_agency)
class TestDomainRequestSuborganization(TestCase):
"""Tests for the suborganization fields on domain requests"""
def setUp(self):
super().setUp()
self.user = create_user()
self.superuser = create_superuser()
def tearDown(self):
super().tearDown()
DomainInformation.objects.all().delete()
DomainRequest.objects.all().delete()
Domain.objects.all().delete()
Suborganization.objects.all().delete()
Portfolio.objects.all().delete()
@less_console_noise_decorator
def test_approve_creates_requested_suborganization(self):
"""Test that approving a domain request with a requested suborganization creates it"""
portfolio = Portfolio.objects.create(organization_name="Test Org", creator=self.user)
domain_request = completed_domain_request(
name="test.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
requested_suborganization="Boom",
suborganization_city="Explody town",
suborganization_state_territory=DomainRequest.StateTerritoryChoices.OHIO,
)
domain_request.investigator = self.superuser
domain_request.save()
domain_request.approve()
created_suborg = Suborganization.objects.filter(
name="Boom",
city="Explody town",
state_territory=DomainRequest.StateTerritoryChoices.OHIO,
portfolio=portfolio,
).first()
self.assertIsNotNone(created_suborg)
self.assertEqual(domain_request.sub_organization, created_suborg)
@less_console_noise_decorator
def test_approve_without_requested_suborganization_makes_no_changes(self):
"""Test that approving without a requested suborganization doesn't create one"""
portfolio = Portfolio.objects.create(organization_name="Test Org", creator=self.user)
domain_request = completed_domain_request(
name="test.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
)
domain_request.investigator = self.superuser
domain_request.save()
initial_suborg_count = Suborganization.objects.count()
domain_request.approve()
self.assertEqual(Suborganization.objects.count(), initial_suborg_count)
self.assertIsNone(domain_request.sub_organization)
@less_console_noise_decorator
def test_approve_with_existing_suborganization_makes_no_changes(self):
"""Test that approving with an existing suborganization doesn't create a new one"""
portfolio = Portfolio.objects.create(organization_name="Test Org", creator=self.user)
existing_suborg = Suborganization.objects.create(name="Existing Division", portfolio=portfolio)
domain_request = completed_domain_request(
name="test.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
sub_organization=existing_suborg,
)
domain_request.investigator = self.superuser
domain_request.save()
initial_suborg_count = Suborganization.objects.count()
domain_request.approve()
self.assertEqual(Suborganization.objects.count(), initial_suborg_count)
self.assertEqual(domain_request.sub_organization, existing_suborg)
@less_console_noise_decorator
def test_cleanup_dangling_suborg_with_single_reference(self):
"""Test that a suborganization is deleted when it's only referenced once"""
portfolio = Portfolio.objects.create(organization_name="Test Org", creator=self.user)
suborg = Suborganization.objects.create(name="Test Division", portfolio=portfolio)
domain_request = completed_domain_request(
name="test.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
sub_organization=suborg,
)
domain_request.approve()
# set it back to in review
domain_request.in_review()
domain_request.refresh_from_db()
# Verify the suborganization was deleted
self.assertFalse(Suborganization.objects.filter(id=suborg.id).exists())
self.assertIsNone(domain_request.sub_organization)
@less_console_noise_decorator
def test_cleanup_dangling_suborg_with_multiple_references(self):
"""Test that a suborganization is preserved when it has multiple references"""
portfolio = Portfolio.objects.create(organization_name="Test Org", creator=self.user)
suborg = Suborganization.objects.create(name="Test Division", portfolio=portfolio)
# Create two domain requests using the same suborganization
domain_request1 = completed_domain_request(
name="test1.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
sub_organization=suborg,
)
domain_request2 = completed_domain_request(
name="test2.gov",
portfolio=portfolio,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
sub_organization=suborg,
)
domain_request1.approve()
domain_request2.approve()
# set one back to in review
domain_request1.in_review()
domain_request1.refresh_from_db()
# Verify the suborganization still exists
self.assertTrue(Suborganization.objects.filter(id=suborg.id).exists())
self.assertEqual(domain_request1.sub_organization, suborg)
self.assertEqual(domain_request2.sub_organization, suborg)