From 8a75c8fcd6e1946e75d679b9098688d82bea23a2 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:52:34 -0700 Subject: [PATCH] Finish test cases --- src/registrar/tests/test_views.py | 89 +++++++++++++++++++++++++----- src/registrar/views/application.py | 6 +- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/src/registrar/tests/test_views.py b/src/registrar/tests/test_views.py index 7f47a40f2..28074dd76 100644 --- a/src/registrar/tests/test_views.py +++ b/src/registrar/tests/test_views.py @@ -89,6 +89,10 @@ class LoggedInTests(TestWithUser): super().setUp() self.client.force_login(self.user) + def tearDown(self): + super().tearDown() + Contact.objects.all().delete() + def test_home_lists_domain_applications(self): response = self.client.get("/") self.assertNotContains(response, "igorville.gov") @@ -96,8 +100,8 @@ class LoggedInTests(TestWithUser): application = DomainApplication.objects.create(creator=self.user, requested_domain=site) response = self.client.get("/") - # count = 5 because of screenreader content - self.assertContains(response, "igorville.gov", count=5) + # count = 7 because of screenreader content + self.assertContains(response, "igorville.gov", count=7) # clean up application.delete() @@ -184,7 +188,7 @@ class LoggedInTests(TestWithUser): def test_home_deletes_domain_application_and_orphans(self): """Tests if delete for DomainApplication deletes orphaned Contact objects""" - + # Create the site and contacts to delete (orphaned) contact = Contact.objects.create( first_name="Henry", @@ -202,22 +206,26 @@ class LoggedInTests(TestWithUser): ) # Attach a user object to a contact (should not be deleted) - contact_user, _ = Contact.objects.get_or_create( - user=self.user - ) + contact_user, _ = Contact.objects.get_or_create(user=self.user) site = DraftDomain.objects.create(name="igorville.gov") application = DomainApplication.objects.create( - creator=self.user, requested_domain=site, status=DomainApplication.ApplicationStatus.WITHDRAWN, - authorizing_official=contact, submitter=contact_user + creator=self.user, + requested_domain=site, + status=DomainApplication.ApplicationStatus.WITHDRAWN, + authorizing_official=contact, + submitter=contact_user, ) application.other_contacts.set([contact_2]) - + # Create a second application to attach contacts to site_2 = DraftDomain.objects.create(name="teaville.gov") application_2 = DomainApplication.objects.create( - creator=self.user, requested_domain=site_2, status=DomainApplication.ApplicationStatus.STARTED, - authorizing_official=contact_2, submitter=contact_shared + creator=self.user, + requested_domain=site_2, + status=DomainApplication.ApplicationStatus.STARTED, + authorizing_official=contact_2, + submitter=contact_shared, ) application_2.other_contacts.set([contact_shared]) @@ -239,7 +247,7 @@ class LoggedInTests(TestWithUser): current_user = Contact.objects.filter(id=contact_user.id).get() except Contact.DoesNotExist: self.fail("contact_user (a non-orphaned contact) was deleted") - + self.assertEqual(current_user, contact_user) try: edge_case = Contact.objects.filter(id=contact_2.id).get() @@ -248,8 +256,61 @@ class LoggedInTests(TestWithUser): self.assertEqual(edge_case, contact_2) - # clean up - application.delete() + def test_home_deletes_domain_application_and_shared_orphans(self): + """Test the edge case for an object that will become orphaned after a delete + (but is not an orphan at the time of deletion)""" + + # Create the site and contacts to delete (orphaned) + contact = Contact.objects.create( + first_name="Henry", + last_name="Mcfakerson", + ) + contact_shared = Contact.objects.create( + first_name="Relative", + last_name="Aether", + ) + + # Create two non-orphaned contacts + contact_2 = Contact.objects.create( + first_name="Saturn", + last_name="Mars", + ) + + # Attach a user object to a contact (should not be deleted) + contact_user, _ = Contact.objects.get_or_create(user=self.user) + + site = DraftDomain.objects.create(name="igorville.gov") + application = DomainApplication.objects.create( + creator=self.user, + requested_domain=site, + status=DomainApplication.ApplicationStatus.WITHDRAWN, + authorizing_official=contact, + submitter=contact_user, + ) + application.other_contacts.set([contact_2]) + + # Create a second application to attach contacts to + site_2 = DraftDomain.objects.create(name="teaville.gov") + application_2 = DomainApplication.objects.create( + creator=self.user, + requested_domain=site_2, + status=DomainApplication.ApplicationStatus.STARTED, + authorizing_official=contact_2, + submitter=contact_shared, + ) + application_2.other_contacts.set([contact_shared]) + + home_page = self.client.get("/") + self.assertContains(home_page, "teaville.gov") + + # Trigger the delete logic + response = self.client.post(reverse("application-delete", kwargs={"pk": application_2.pk}), follow=True) + + self.assertNotContains(response, "teaville.gov") + + # Check if the orphaned contact was deleted + orphan = Contact.objects.filter(id=contact_shared.id) + self.assertFalse(orphan.exists()) def test_application_form_view(self): response = self.client.get("/request/", follow=True) diff --git a/src/registrar/views/application.py b/src/registrar/views/application.py index 9a96824c7..b58deecc4 100644 --- a/src/registrar/views/application.py +++ b/src/registrar/views/application.py @@ -675,8 +675,8 @@ class DomainApplicationDeleteView(DomainApplicationPermissionDeleteView): # Check if the desired item still exists in the DB if check_db: - ao = self._get_contacts_by_id([ao.id]).first() - submitter = self._get_contacts_by_id([submitter.id]).first() + ao = self._get_contacts_by_id([ao.id]).first() if ao is not None else None + submitter = self._get_contacts_by_id([submitter.id]).first() if submitter is not None else None other_contacts = self._get_contacts_by_id(other_contact_ids) # Pair each contact with its related name @@ -698,7 +698,7 @@ class DomainApplicationDeleteView(DomainApplicationPermissionDeleteView): """Given a list of objects, return a list of which items were duplicates""" # Gets the occurence count object_dict = defaultdict(int) - for contact, _ in objects: + for contact, _related in objects: object_dict[contact] += 1 duplicates = [item for item, count in object_dict.items() if count > 1]