fixed up some additional cleanups/tearDowns

This commit is contained in:
David Kennedy 2024-07-26 17:47:21 -04:00
parent e12115b596
commit 587b268e38
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
6 changed files with 136 additions and 122 deletions

View file

@ -6,7 +6,6 @@ from api.tests.common import less_console_noise_decorator
from django.urls import reverse
from registrar.admin import (
DomainAdmin,
DomainRequestAdminForm,
DomainInvitationAdmin,
ListHeaderAdmin,
MyUserAdmin,
@ -108,78 +107,6 @@ class TestFsmModelResource(TestCase):
fsm_field_mock.save.assert_not_called()
class TestDomainRequestAdminForm(TestCase):
def test_form_choices(self):
with less_console_noise():
# Create a test domain request with an initial state of started
domain_request = completed_domain_request()
# Create a form instance with the test domain request
form = DomainRequestAdminForm(instance=domain_request)
# Verify that the form choices match the available transitions for started
expected_choices = [("started", "Started"), ("submitted", "Submitted")]
self.assertEqual(form.fields["status"].widget.choices, expected_choices)
# cleanup
domain_request.delete()
def test_form_no_rejection_reason(self):
with less_console_noise():
# Create a test domain request with an initial state of started
domain_request = completed_domain_request()
# Create a form instance with the test domain request
form = DomainRequestAdminForm(instance=domain_request)
form = DomainRequestAdminForm(
instance=domain_request,
data={
"status": DomainRequest.DomainRequestStatus.REJECTED,
"rejection_reason": None,
},
)
self.assertFalse(form.is_valid())
self.assertIn("rejection_reason", form.errors)
rejection_reason = form.errors.get("rejection_reason")
self.assertEqual(rejection_reason, ["A reason is required for this status."])
# cleanup
domain_request.delete()
def test_form_choices_when_no_instance(self):
with less_console_noise():
# Create a form instance without an instance
form = DomainRequestAdminForm()
# Verify that the form choices show all choices when no instance is provided;
# this is necessary to show all choices when creating a new domain
# request in django admin;
# note that FSM ensures that no domain request exists with invalid status,
# so don't need to test for invalid status
self.assertEqual(
form.fields["status"].widget.choices,
DomainRequest._meta.get_field("status").choices,
)
def test_form_choices_when_ineligible(self):
with less_console_noise():
# Create a form instance with a domain request with ineligible status
ineligible_domain_request = DomainRequest(status="ineligible")
# Attempt to create a form with the ineligible domain request
# The form should not raise an error, but choices should be the
# full list of possible choices
form = DomainRequestAdminForm(instance=ineligible_domain_request)
self.assertEqual(
form.fields["status"].widget.choices,
DomainRequest._meta.get_field("status").choices,
)
class TestDomainInvitationAdmin(TestCase):
"""Tests for the DomainInvitationAdmin class as super user
@ -1123,7 +1050,7 @@ class TestMyUserAdmin(MockDbForSharedTests):
domain_deleted, _ = Domain.objects.get_or_create(
name="domain_deleted.gov", state=Domain.State.DELETED, deleted=timezone.make_aware(datetime(2024, 4, 2))
)
UserDomainRole.objects.get_or_create(
role, _ = UserDomainRole.objects.get_or_create(
user=self.meoward_user, domain=domain_deleted, role=UserDomainRole.Roles.MANAGER
)
@ -1178,14 +1105,25 @@ class TestMyUserAdmin(MockDbForSharedTests):
self.assertNotContains(response, expected_href)
# Must clean up within test since MockDB is shared across tests for performance reasons
domain_request_started.delete()
domain_request_submitted.delete()
domain_request_in_review.delete()
domain_request_withdrawn.delete()
domain_request_approved.delete()
domain_request_rejected.delete()
domain_request_ineligible.delete()
domain_request_started_id = domain_request_started.id
domain_request_submitted_id = domain_request_submitted.id
domain_request_in_review_id = domain_request_in_review.id
domain_request_withdrawn_id = domain_request_withdrawn.id
domain_request_approved_id = domain_request_approved.id
domain_request_rejected_id = domain_request_rejected.id
domain_request_ineligible_id = domain_request_ineligible.id
domain_request_ids = [
domain_request_started_id,
domain_request_submitted_id,
domain_request_in_review_id,
domain_request_withdrawn_id,
domain_request_approved_id,
domain_request_rejected_id,
domain_request_ineligible_id,
]
DomainRequest.objects.filter(id__in=domain_request_ids).delete()
domain_deleted.delete()
role.delete()
class AuditedAdminTest(TestCase):
@ -1574,7 +1512,7 @@ class TestContactAdmin(TestCase):
super().setUpClass()
cls.site = AdminSite()
cls.factory = RequestFactory()
cls.admin = ContactAdmin(model=get_user_model(), admin_site=None)
cls.admin = ContactAdmin(model=Contact, admin_site=None)
cls.superuser = create_superuser()
cls.staffuser = create_user()

View file

@ -1,7 +1,7 @@
from datetime import datetime
from django.utils import timezone
import re
from django.test import RequestFactory, Client, override_settings
from django.test import RequestFactory, Client, TestCase, override_settings
from django.contrib.admin.sites import AdminSite
from contextlib import ExitStack
from api.tests.common import less_console_noise_decorator
@ -9,6 +9,7 @@ from django.contrib import messages
from django.urls import reverse
from registrar.admin import (
DomainRequestAdmin,
DomainRequestAdminForm,
MyUserAdmin,
AuditedAdmin,
)
@ -1998,3 +1999,75 @@ class TestDomainRequestAdmin(MockEppLib):
# Check if response contains expected_html
self.assertIn(expected_html, response_content)
class TestDomainRequestAdminForm(TestCase):
def test_form_choices(self):
with less_console_noise():
# Create a test domain request with an initial state of started
domain_request = completed_domain_request()
# Create a form instance with the test domain request
form = DomainRequestAdminForm(instance=domain_request)
# Verify that the form choices match the available transitions for started
expected_choices = [("started", "Started"), ("submitted", "Submitted")]
self.assertEqual(form.fields["status"].widget.choices, expected_choices)
# cleanup
domain_request.delete()
def test_form_no_rejection_reason(self):
with less_console_noise():
# Create a test domain request with an initial state of started
domain_request = completed_domain_request()
# Create a form instance with the test domain request
form = DomainRequestAdminForm(instance=domain_request)
form = DomainRequestAdminForm(
instance=domain_request,
data={
"status": DomainRequest.DomainRequestStatus.REJECTED,
"rejection_reason": None,
},
)
self.assertFalse(form.is_valid())
self.assertIn("rejection_reason", form.errors)
rejection_reason = form.errors.get("rejection_reason")
self.assertEqual(rejection_reason, ["A reason is required for this status."])
# cleanup
domain_request.delete()
def test_form_choices_when_no_instance(self):
with less_console_noise():
# Create a form instance without an instance
form = DomainRequestAdminForm()
# Verify that the form choices show all choices when no instance is provided;
# this is necessary to show all choices when creating a new domain
# request in django admin;
# note that FSM ensures that no domain request exists with invalid status,
# so don't need to test for invalid status
self.assertEqual(
form.fields["status"].widget.choices,
DomainRequest._meta.get_field("status").choices,
)
def test_form_choices_when_ineligible(self):
with less_console_noise():
# Create a form instance with a domain request with ineligible status
ineligible_domain_request = DomainRequest(status="ineligible")
# Attempt to create a form with the ineligible domain request
# The form should not raise an error, but choices should be the
# full list of possible choices
form = DomainRequestAdminForm(instance=ineligible_domain_request)
self.assertEqual(
form.fields["status"].widget.choices,
DomainRequest._meta.get_field("status").choices,
)

View file

@ -1677,6 +1677,7 @@ class TestDomainRequestIncomplete(TestCase):
)
alt, _ = Website.objects.get_or_create(website="MeowardMeowardMeoward1.gov")
current, _ = Website.objects.get_or_create(website="MeowardMeowardMeoward.com")
self.amtrak, _ = FederalAgency.objects.get_or_create(agency="AMTRAK")
self.domain_request = DomainRequest.objects.create(
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
federal_type="executive",
@ -1709,6 +1710,7 @@ class TestDomainRequestIncomplete(TestCase):
super().tearDown()
DomainRequest.objects.all().delete()
Contact.objects.all().delete()
self.amtrak.delete()
@classmethod
def tearDownClass(cls):

View file

@ -68,8 +68,8 @@ class TestWithUser(MockEppLib):
def tearDown(self):
# delete any domain requests too
super().tearDown()
DomainRequest.objects.all().delete()
DomainInformation.objects.all().delete()
# DomainRequest.objects.all().delete()
# DomainInformation.objects.all().delete()
@classmethod
def tearDownClass(cls):
@ -163,13 +163,6 @@ class HomeTests(TestWithUser):
super().setUp()
self.client.force_login(self.user)
# def tearDown(self):
# super().tearDown()
# Contact.objects.all().delete()
# UserDomainRole.objects.all().delete()
# Domain.objects.all().delete()
# DomainRequest.objects.all().delete()
@less_console_noise_decorator
def test_empty_domain_table(self):
response = self.client.get("/")
@ -533,6 +526,8 @@ class FinishUserProfileTests(TestWithUser, WebTest):
def tearDown(self):
super().tearDown()
DomainRequest.objects.all().delete()
DomainInformation.objects.all().delete()
self.user.title = self.initial_user_title
self.user.save()
PublicContact.objects.filter(domain=self.domain).delete()
@ -918,9 +913,10 @@ class UserProfileTests(TestWithUser, WebTest):
PublicContact.objects.filter(domain=self.domain).delete()
self.role.delete()
self.domain.delete()
Contact.objects.all().delete()
DraftDomain.objects.all().delete()
DomainRequest.objects.all().delete()
DraftDomain.objects.all().delete()
Contact.objects.all().delete()
DomainInformation.objects.all().delete()
@less_console_noise_decorator
def error_500_main_nav_with_profile_feature_turned_on(self):

View file

@ -24,7 +24,6 @@ class GetDomainsJsonTest(TestWithUser, WebTest):
def tearDown(self):
super().tearDown()
UserDomainRole.objects.all().delete()
UserDomainRole.objects.all().delete()
@less_console_noise_decorator
def test_get_domains_json_unauthenticated(self):

View file

@ -37,9 +37,16 @@ class DomainRequestTests(TestWithUser, WebTest):
def setUp(self):
super().setUp()
self.federal_agency, _ = FederalAgency.objects.get_or_create(agency="General Services Administration")
self.app.set_user(self.user.username)
self.TITLES = DomainRequestWizard.TITLES
def tearDown(self):
super().tearDown()
DomainRequest.objects.all().delete()
DomainInformation.objects.all().delete()
self.federal_agency.delete()
@less_console_noise_decorator
def test_domain_request_form_intro_acknowledgement(self):
"""Tests that user is presented with intro acknowledgement page"""
@ -231,9 +238,7 @@ class DomainRequestTests(TestWithUser, WebTest):
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
org_contact_page = federal_result.follow()
org_contact_form = org_contact_page.forms[0]
# federal agency so we have to fill in federal_agency
federal_agency, _ = FederalAgency.objects.get_or_create(agency="General Services Administration")
org_contact_form["organization_contact-federal_agency"] = federal_agency.id
org_contact_form["organization_contact-federal_agency"] = self.federal_agency.id
org_contact_form["organization_contact-organization_name"] = "Testorg"
org_contact_form["organization_contact-address_line1"] = "address 1"
org_contact_form["organization_contact-address_line2"] = "address 2"
@ -589,9 +594,7 @@ class DomainRequestTests(TestWithUser, WebTest):
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
org_contact_page = federal_result.follow()
org_contact_form = org_contact_page.forms[0]
# federal agency so we have to fill in federal_agency
federal_agency, _ = FederalAgency.objects.get_or_create(agency="General Services Administration")
org_contact_form["organization_contact-federal_agency"] = federal_agency.id
org_contact_form["organization_contact-federal_agency"] = self.federal_agency.id
org_contact_form["organization_contact-organization_name"] = "Testorg"
org_contact_form["organization_contact-address_line1"] = "address 1"
org_contact_form["organization_contact-address_line2"] = "address 2"
@ -2499,9 +2502,7 @@ class DomainRequestTests(TestWithUser, WebTest):
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
org_contact_page = federal_result.follow()
org_contact_form = org_contact_page.forms[0]
# federal agency so we have to fill in federal_agency
federal_agency, _ = FederalAgency.objects.get_or_create(agency="General Services Administration")
org_contact_form["organization_contact-federal_agency"] = federal_agency.id
org_contact_form["organization_contact-federal_agency"] = self.federal_agency.id
org_contact_form["organization_contact-organization_name"] = "Testorg"
org_contact_form["organization_contact-address_line1"] = "address 1"
org_contact_form["organization_contact-address_line2"] = "address 2"
@ -2572,9 +2573,7 @@ class DomainRequestTests(TestWithUser, WebTest):
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
org_contact_page = federal_result.follow()
org_contact_form = org_contact_page.forms[0]
# federal agency so we have to fill in federal_agency
federal_agency, _ = FederalAgency.objects.get_or_create(agency="General Services Administration")
org_contact_form["organization_contact-federal_agency"] = federal_agency.id
org_contact_form["organization_contact-federal_agency"] = self.federal_agency.id
org_contact_form["organization_contact-organization_name"] = "Testorg"
org_contact_form["organization_contact-address_line1"] = "address 1"
org_contact_form["organization_contact-address_line2"] = "address 2"
@ -2833,6 +2832,11 @@ class DomainRequestTestDifferentStatuses(TestWithUser, WebTest):
self.app.set_user(self.user.username)
self.client.force_login(self.user)
def tearDown(self):
super().tearDown()
DomainRequest.objects.all().delete()
DomainInformation.objects.all().delete()
@less_console_noise_decorator
def test_domain_request_status(self):
"""Checking domain request status page"""
@ -2965,6 +2969,8 @@ class TestWizardUnlockingSteps(TestWithUser, WebTest):
def tearDown(self):
super().tearDown()
DomainRequest.objects.all().delete()
DomainInformation.objects.all().delete()
@less_console_noise_decorator
def test_unlocked_steps_empty_domain_request(self):