From b50eaf91f623c42bb57a06b49ed43b21c7e07385 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:52:31 -0700 Subject: [PATCH] Unit tests --- src/registrar/forms/domain.py | 4 +- src/registrar/tests/test_views_domain.py | 213 ++++++++++++++++++++--- src/registrar/views/domain.py | 6 +- 3 files changed, 193 insertions(+), 30 deletions(-) diff --git a/src/registrar/forms/domain.py b/src/registrar/forms/domain.py index 3dd2c72ac..5eb043742 100644 --- a/src/registrar/forms/domain.py +++ b/src/registrar/forms/domain.py @@ -1,4 +1,5 @@ """Forms for domain management.""" + import logging from django import forms from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator @@ -264,8 +265,7 @@ class AuthorizingOfficialContactForm(ContactForm): if disable_maxlength: # Remove the maxlength dialog if "maxlength" in field.widget.attrs: - field.widget.attrs.pop('maxlength', None) - + field.widget.attrs.pop("maxlength", None) def save(self, commit=True): """ diff --git a/src/registrar/tests/test_views_domain.py b/src/registrar/tests/test_views_domain.py index 8cc3b16a0..2a861a878 100644 --- a/src/registrar/tests/test_views_domain.py +++ b/src/registrar/tests/test_views_domain.py @@ -1021,6 +1021,144 @@ class TestDomainAuthorizingOfficial(TestDomainOverview): self.assertEqual("Testy2", self.domain_information.authorizing_official.first_name) self.assertEqual(ao_pk, self.domain_information.authorizing_official.id) + def assert_all_form_fields_have_expected_values(self, form, test_cases, test_for_disabled=False): + """ + Asserts that each specified form field has the expected value and, optionally, checks if the field is disabled. + + This method iterates over a list of tuples, where each + tuple contains a field name and the expected value for that field. + It uses subtests to isolate each assertion, allowing multiple field + checks within a single test method without stopping at the first failure. + + Example usage: + test_cases = [ + ("first_name", "John"), + ("last_name", "Doe"), + ("email", "john.doe@example.com"), + ] + self.assert_all_form_fields_have_expected_values(my_form, test_cases, test_for_disabled=True) + """ + for field_name, expected_value in test_cases: + with self.subTest(field_name=field_name, expected_value=expected_value): + # Test that each field has the value we expect + self.assertEqual(expected_value, form[field_name].value) + + if test_for_disabled: + # Test for disabled on each field + self.assertTrue("disabled" in form[field_name].attrs) + + def test_domain_edit_authorizing_official_federal(self): + """Tests that no edit can occur when the underlying domain is federal""" + + # Set the org type to federal + self.domain_information.organization_type = DomainInformation.OrganizationChoices.FEDERAL + self.domain_information.save() + + # Add an AO. We can do this at the model level, just not the form level. + self.domain_information.authorizing_official = Contact( + first_name="Apple", last_name="Tester", title="CIO", email="nobody@igorville.gov" + ) + self.domain_information.authorizing_official.save() + self.domain_information.save() + + ao_page = self.app.get(reverse("domain-authorizing-official", kwargs={"pk": self.domain.id})) + session_id = self.app.cookies[settings.SESSION_COOKIE_NAME] + self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id) + + # Test if the form is populating data correctly + ao_form = ao_page.forms[0] + + test_cases = [ + ("first_name", "Apple"), + ("last_name", "Tester"), + ("title", "CIO"), + ("email", "nobody@igorville.gov"), + ] + self.assert_all_form_fields_have_expected_values(ao_form, test_cases, test_for_disabled=True) + + # Attempt to change data on each field. Because this domain is federal, + # this should not succeed. + ao_form["first_name"] = "Orange" + ao_form["last_name"] = "Smoothie" + ao_form["title"] = "Cat" + ao_form["email"] = "somebody@igorville.gov" + + submission = ao_form.submit() + + # A 302 indicates this page underwent a redirect. + self.assertEqual(submission.status_code, 302) + + followed_submission = submission.follow() + + # Test the returned form for data accuracy. These values should be unchanged. + new_form = followed_submission.forms[0] + self.assert_all_form_fields_have_expected_values(new_form, test_cases, test_for_disabled=True) + + # refresh domain information. Test these values in the DB. + self.domain_information.refresh_from_db() + + # All values should be unchanged. These are defined manually for code clarity. + self.assertEqual("Apple", self.domain_information.authorizing_official.first_name) + self.assertEqual("Tester", self.domain_information.authorizing_official.last_name) + self.assertEqual("CIO", self.domain_information.authorizing_official.title) + self.assertEqual("nobody@igorville.gov", self.domain_information.authorizing_official.email) + + def test_domain_edit_authorizing_official_tribal(self): + """Tests that no edit can occur when the underlying domain is tribal""" + + # Set the org type to federal + self.domain_information.organization_type = DomainInformation.OrganizationChoices.TRIBAL + self.domain_information.save() + + # Add an AO. We can do this at the model level, just not the form level. + self.domain_information.authorizing_official = Contact( + first_name="Apple", last_name="Tester", title="CIO", email="nobody@igorville.gov" + ) + self.domain_information.authorizing_official.save() + self.domain_information.save() + + ao_page = self.app.get(reverse("domain-authorizing-official", kwargs={"pk": self.domain.id})) + session_id = self.app.cookies[settings.SESSION_COOKIE_NAME] + self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id) + + # Test if the form is populating data correctly + ao_form = ao_page.forms[0] + + test_cases = [ + ("first_name", "Apple"), + ("last_name", "Tester"), + ("title", "CIO"), + ("email", "nobody@igorville.gov"), + ] + self.assert_all_form_fields_have_expected_values(ao_form, test_cases, test_for_disabled=True) + + # Attempt to change data on each field. Because this domain is federal, + # this should not succeed. + ao_form["first_name"] = "Orange" + ao_form["last_name"] = "Smoothie" + ao_form["title"] = "Cat" + ao_form["email"] = "somebody@igorville.gov" + + submission = ao_form.submit() + + # A 302 indicates this page underwent a redirect. + self.assertEqual(submission.status_code, 302) + + followed_submission = submission.follow() + + # Test the returned form for data accuracy. These values should be unchanged. + new_form = followed_submission.forms[0] + self.assert_all_form_fields_have_expected_values(new_form, test_cases, test_for_disabled=True) + + # refresh domain information. Test these values in the DB. + self.domain_information.refresh_from_db() + + # All values should be unchanged. These are defined manually for code clarity. + self.assertEqual("Apple", self.domain_information.authorizing_official.first_name) + self.assertEqual("Tester", self.domain_information.authorizing_official.last_name) + self.assertEqual("CIO", self.domain_information.authorizing_official.title) + self.assertEqual("nobody@igorville.gov", self.domain_information.authorizing_official.email) + def test_domain_edit_authorizing_official_creates_new(self): """When editing an authorizing official for domain information and AO IS joined to another object""" @@ -1087,7 +1225,7 @@ class TestDomainOrganization(TestDomainOverview): self.assertContains(success_result_page, "Not igorville") self.assertContains(success_result_page, "Faketown") - + def test_domain_org_name_address_form_tribal(self): """ Submitting a change to organization_name is blocked for tribal domains @@ -1113,11 +1251,11 @@ class TestDomainOrganization(TestDomainOverview): # Check the value of the input field organization_name_input = form.fields["organization_name"][0] self.assertEqual(organization_name_input.value, "Town of Igorville") - + # Check if the input field is disabled self.assertTrue("disabled" in organization_name_input.attrs) self.assertEqual(organization_name_input.attrs.get("disabled"), "") - + session_id = self.app.cookies[settings.SESSION_COOKIE_NAME] org_name_page.form["organization_name"] = "Not igorville" @@ -1133,12 +1271,22 @@ class TestDomainOrganization(TestDomainOverview): self.assertContains(success_result_page, "Town of Igorville") self.assertNotContains(success_result_page, "Not igorville") + # Do another check on the form itself + form = success_result_page.forms[0] + # Check the value of the input field + organization_name_input = form.fields["organization_name"][0] + self.assertEqual(organization_name_input.value, "Town of Igorville") + + # Check if the input field is disabled + self.assertTrue("disabled" in organization_name_input.attrs) + self.assertEqual(organization_name_input.attrs.get("disabled"), "") + # Check for the value we want to update self.assertContains(success_result_page, "Faketown") - - def test_domain_org_name_address_form_federal(self): + + def test_domain_org_name_address_form_federal_disabled(self): """ - Submitting a change to federal_agency is blocked for federal domains + Tests if the federal_agency field is readonly """ # Set the current domain to a tribal organization with a preset value. # Save first, so we can test if saving is unaffected (it should be). @@ -1155,33 +1303,48 @@ class TestDomainOrganization(TestDomainOverview): self.assertEqual(self.domain_information.organization_type, federal_org_type) org_name_page = self.app.get(reverse("domain-org-name-address", kwargs={"pk": self.domain.id})) + print(f"what is the org name page? {org_name_page}") form = org_name_page.forms[0] # Check the value of the input field - federal_agency_input = form.fields["federal_agency"][0] - self.assertEqual(federal_agency_input.value, "AMTRAK") - - # Check if the input field is disabled - self.assertTrue("disabled" in federal_agency_input.attrs) - self.assertEqual(federal_agency_input.attrs.get("disabled"), "") - - session_id = self.app.cookies[settings.SESSION_COOKIE_NAME] + organization_name_input = form.fields["federal_agency"][0] - org_name_page.form["organization_name"] = "Not igorville" - org_name_page.form["city"] = "Faketown" + # Check if the input field is disabled. + # Webtest has some issues dealing with Selects, so we can't + # directly test the value but we can test its attributes. + # This is done in another test. + self.assertTrue("disabled" in organization_name_input.attrs) + self.assertEqual(organization_name_input.attrs.get("disabled"), "") - self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id) + def test_federal_agency_submit_blocked(self): + """ + Submitting a change to federal_agency is blocked for federal domains + """ + # Set the current domain to a tribal organization with a preset value. + # Save first, so we can test if saving is unaffected (it should be). + federal_org_type = DomainInformation.OrganizationChoices.FEDERAL + self.domain_information.organization_type = federal_org_type + self.domain_information.save() - # Make the change. The org name should be unchanged, but city should be modifiable. - success_result_page = org_name_page.form.submit() - self.assertEqual(success_result_page.status_code, 200) + old_federal_agency_value = ("AMTRAK", "AMTRAK") + try: + # Add a federal agency. Defined as a tuple since this list may change order. + self.domain_information.federal_agency = old_federal_agency_value + self.domain_information.save() + except ValueError as err: + self.fail(f"A ValueError was caught during the test: {err}") - # Check for the old and new value - self.assertContains(success_result_page, "Town of Igorville") - self.assertNotContains(success_result_page, "Not igorville") + self.assertEqual(self.domain_information.organization_type, federal_org_type) - # Check for the value we want to update - self.assertContains(success_result_page, "Faketown") + new_value = ("Department of State", "Department of State") + self.client.post( + reverse("domain-org-name-address", kwargs={"pk": self.domain.id}), + { + "federal_agency": new_value, + }, + ) + self.assertEqual(self.domain_information.federal_agency, old_federal_agency_value) + self.assertNotEqual(self.domain_information.federal_agency, new_value) class TestDomainContactInformation(TestDomainOverview): diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 07b695356..34628bc88 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -135,7 +135,7 @@ class DomainFormBaseView(DomainBaseView, FormMixin): # superclass has the redirect return super().form_invalid(form) - + def get_domain_info_from_domain(self) -> DomainInformation | None: """ Grabs the underlying domain_info object based off of self.object.name. @@ -147,7 +147,7 @@ class DomainFormBaseView(DomainBaseView, FormMixin): current_domain_info = _domain_info.get() else: logger.error("Could get domain_info. No domain info exists, or duplicates exist.") - + return current_domain_info @@ -237,7 +237,7 @@ class DomainAuthorizingOfficialView(DomainFormBaseView): domain_info = self.get_domain_info_from_domain() invalid_fields = [DomainApplication.OrganizationChoices.FEDERAL, DomainApplication.OrganizationChoices.TRIBAL] is_federal_or_tribal = domain_info and (domain_info.organization_type in invalid_fields) - + form_kwargs["disable_fields"] = is_federal_or_tribal return form_kwargs