mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-26 04:28:39 +02:00
Finish test cases
This commit is contained in:
parent
a119cab680
commit
8a75c8fcd6
2 changed files with 78 additions and 17 deletions
|
@ -89,6 +89,10 @@ class LoggedInTests(TestWithUser):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.client.force_login(self.user)
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
super().tearDown()
|
||||||
|
Contact.objects.all().delete()
|
||||||
|
|
||||||
def test_home_lists_domain_applications(self):
|
def test_home_lists_domain_applications(self):
|
||||||
response = self.client.get("/")
|
response = self.client.get("/")
|
||||||
self.assertNotContains(response, "igorville.gov")
|
self.assertNotContains(response, "igorville.gov")
|
||||||
|
@ -96,8 +100,8 @@ class LoggedInTests(TestWithUser):
|
||||||
application = DomainApplication.objects.create(creator=self.user, requested_domain=site)
|
application = DomainApplication.objects.create(creator=self.user, requested_domain=site)
|
||||||
response = self.client.get("/")
|
response = self.client.get("/")
|
||||||
|
|
||||||
# count = 5 because of screenreader content
|
# count = 7 because of screenreader content
|
||||||
self.assertContains(response, "igorville.gov", count=5)
|
self.assertContains(response, "igorville.gov", count=7)
|
||||||
|
|
||||||
# clean up
|
# clean up
|
||||||
application.delete()
|
application.delete()
|
||||||
|
@ -184,7 +188,7 @@ class LoggedInTests(TestWithUser):
|
||||||
|
|
||||||
def test_home_deletes_domain_application_and_orphans(self):
|
def test_home_deletes_domain_application_and_orphans(self):
|
||||||
"""Tests if delete for DomainApplication deletes orphaned Contact objects"""
|
"""Tests if delete for DomainApplication deletes orphaned Contact objects"""
|
||||||
|
|
||||||
# Create the site and contacts to delete (orphaned)
|
# Create the site and contacts to delete (orphaned)
|
||||||
contact = Contact.objects.create(
|
contact = Contact.objects.create(
|
||||||
first_name="Henry",
|
first_name="Henry",
|
||||||
|
@ -202,22 +206,26 @@ class LoggedInTests(TestWithUser):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Attach a user object to a contact (should not be deleted)
|
# Attach a user object to a contact (should not be deleted)
|
||||||
contact_user, _ = Contact.objects.get_or_create(
|
contact_user, _ = Contact.objects.get_or_create(user=self.user)
|
||||||
user=self.user
|
|
||||||
)
|
|
||||||
|
|
||||||
site = DraftDomain.objects.create(name="igorville.gov")
|
site = DraftDomain.objects.create(name="igorville.gov")
|
||||||
application = DomainApplication.objects.create(
|
application = DomainApplication.objects.create(
|
||||||
creator=self.user, requested_domain=site, status=DomainApplication.ApplicationStatus.WITHDRAWN,
|
creator=self.user,
|
||||||
authorizing_official=contact, submitter=contact_user
|
requested_domain=site,
|
||||||
|
status=DomainApplication.ApplicationStatus.WITHDRAWN,
|
||||||
|
authorizing_official=contact,
|
||||||
|
submitter=contact_user,
|
||||||
)
|
)
|
||||||
application.other_contacts.set([contact_2])
|
application.other_contacts.set([contact_2])
|
||||||
|
|
||||||
# Create a second application to attach contacts to
|
# Create a second application to attach contacts to
|
||||||
site_2 = DraftDomain.objects.create(name="teaville.gov")
|
site_2 = DraftDomain.objects.create(name="teaville.gov")
|
||||||
application_2 = DomainApplication.objects.create(
|
application_2 = DomainApplication.objects.create(
|
||||||
creator=self.user, requested_domain=site_2, status=DomainApplication.ApplicationStatus.STARTED,
|
creator=self.user,
|
||||||
authorizing_official=contact_2, submitter=contact_shared
|
requested_domain=site_2,
|
||||||
|
status=DomainApplication.ApplicationStatus.STARTED,
|
||||||
|
authorizing_official=contact_2,
|
||||||
|
submitter=contact_shared,
|
||||||
)
|
)
|
||||||
application_2.other_contacts.set([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()
|
current_user = Contact.objects.filter(id=contact_user.id).get()
|
||||||
except Contact.DoesNotExist:
|
except Contact.DoesNotExist:
|
||||||
self.fail("contact_user (a non-orphaned contact) was deleted")
|
self.fail("contact_user (a non-orphaned contact) was deleted")
|
||||||
|
|
||||||
self.assertEqual(current_user, contact_user)
|
self.assertEqual(current_user, contact_user)
|
||||||
try:
|
try:
|
||||||
edge_case = Contact.objects.filter(id=contact_2.id).get()
|
edge_case = Contact.objects.filter(id=contact_2.id).get()
|
||||||
|
@ -248,8 +256,61 @@ class LoggedInTests(TestWithUser):
|
||||||
|
|
||||||
self.assertEqual(edge_case, contact_2)
|
self.assertEqual(edge_case, contact_2)
|
||||||
|
|
||||||
# clean up
|
def test_home_deletes_domain_application_and_shared_orphans(self):
|
||||||
application.delete()
|
"""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):
|
def test_application_form_view(self):
|
||||||
response = self.client.get("/request/", follow=True)
|
response = self.client.get("/request/", follow=True)
|
||||||
|
|
|
@ -675,8 +675,8 @@ class DomainApplicationDeleteView(DomainApplicationPermissionDeleteView):
|
||||||
|
|
||||||
# Check if the desired item still exists in the DB
|
# Check if the desired item still exists in the DB
|
||||||
if check_db:
|
if check_db:
|
||||||
ao = self._get_contacts_by_id([ao.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()
|
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)
|
other_contacts = self._get_contacts_by_id(other_contact_ids)
|
||||||
|
|
||||||
# Pair each contact with its related name
|
# 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"""
|
"""Given a list of objects, return a list of which items were duplicates"""
|
||||||
# Gets the occurence count
|
# Gets the occurence count
|
||||||
object_dict = defaultdict(int)
|
object_dict = defaultdict(int)
|
||||||
for contact, _ in objects:
|
for contact, _related in objects:
|
||||||
object_dict[contact] += 1
|
object_dict[contact] += 1
|
||||||
|
|
||||||
duplicates = [item for item, count in object_dict.items() if count > 1]
|
duplicates = [item for item, count in object_dict.items() if count > 1]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue