From 6c8e1fec5986cbfe5196fe739e48b5dd13cc656b Mon Sep 17 00:00:00 2001 From: Rachid Mrad Date: Thu, 25 Jan 2024 14:37:29 -0500 Subject: [PATCH] Revise to show ellipsis with numbers, unit tests --- src/registrar/admin.py | 13 +++--- src/registrar/tests/common.py | 18 +++++---- src/registrar/tests/test_admin.py | 67 +++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 7b196bfa2..3d71fb710 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -491,11 +491,14 @@ class ContactAdmin(ListHeaderAdmin): if related_objects: message = "" - for url, obj in related_objects: - escaped_obj = escape(obj) - message += f"Joined to {obj.__class__.__name__}: {escaped_obj}
" - # message_html is considered safe html. It is generated from a finite list of strings - # which are generated from django objects. And a django object, which is escaped + for i, (url, obj) in enumerate(related_objects): + if i < 5: + escaped_obj = escape(obj) + message += f"Joined to {obj.__class__.__name__}: {escaped_obj}
" + if len(related_objects) > 5: + related_objects_over_five = len(related_objects) - 5 + message += f"And {related_objects_over_five} more..." + message_html = mark_safe(message) # nosec messages.warning( request, diff --git a/src/registrar/tests/common.py b/src/registrar/tests/common.py index 80ec5ef3d..023e5319e 100644 --- a/src/registrar/tests/common.py +++ b/src/registrar/tests/common.py @@ -526,6 +526,7 @@ def completed_application( has_anything_else=True, status=DomainApplication.ApplicationStatus.STARTED, user=False, + submitter=False, name="city.gov", ): """A completed domain application.""" @@ -541,13 +542,14 @@ def completed_application( domain, _ = DraftDomain.objects.get_or_create(name=name) alt, _ = Website.objects.get_or_create(website="city1.gov") current, _ = Website.objects.get_or_create(website="city.com") - you, _ = Contact.objects.get_or_create( - first_name="Testy2", - last_name="Tester2", - title="Admin Tester", - email="mayor@igorville.gov", - phone="(555) 555 5556", - ) + if not submitter: + submitter, _ = Contact.objects.get_or_create( + first_name="Testy2", + last_name="Tester2", + title="Admin Tester", + email="mayor@igorville.gov", + phone="(555) 555 5556", + ) other, _ = Contact.objects.get_or_create( first_name="Testy", last_name="Tester", @@ -567,7 +569,7 @@ def completed_application( zipcode="10002", authorizing_official=ao, requested_domain=domain, - submitter=you, + submitter=submitter, creator=user, status=status, ) diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index f7b1ef06e..0a40f85fb 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -1735,5 +1735,72 @@ class ContactAdminTest(TestCase): self.assertEqual(readonly_fields, expected_fields) + def test_change_view_for_joined_contact_five_or_less(self): + """Create a contact, join it to 4 domain requests. The 5th join will be a user. + Assert that the warning on the contact form lists 5 joins.""" + + self.client.force_login(self.superuser) + + # Create an instance of the model + contact, _ = Contact.objects.get_or_create(user=self.staffuser) + + # join it to 4 domain requests. The 5th join will be a user. + completed_application(submitter=contact, name="city1.gov") + completed_application(submitter=contact, name="city2.gov") + completed_application(submitter=contact, name="city3.gov") + completed_application(submitter=contact, name="city4.gov") + + with patch("django.contrib.messages.warning") as mock_warning: + # Use the test client to simulate the request + response = self.client.get(reverse("admin:registrar_contact_change", args=[contact.pk])) + + logger.info(mock_warning) + + # Assert that the error message was called with the correct argument + # Note: The 5th join will be a user. + mock_warning.assert_called_once_with( + response.wsgi_request, + "Joined to DomainApplication: city1.gov
" + "Joined to DomainApplication: city2.gov
" + "Joined to DomainApplication: city3.gov
" + "Joined to DomainApplication: city4.gov
" + "Joined to User: staff@example.com
", + ) + + def test_change_view_for_joined_contact_five_or_more(self): + """Create a contact, join it to 5 domain requests. The 6th join will be a user. + Assert that the warning on the contact form lists 5 joins and a '1 more' ellispsis.""" + + self.client.force_login(self.superuser) + + # Create an instance of the model + # join it to 5 domain requests. The 6th join will be a user. + contact, _ = Contact.objects.get_or_create(user=self.staffuser) + completed_application(submitter=contact, name="city1.gov") + completed_application(submitter=contact, name="city2.gov") + completed_application(submitter=contact, name="city3.gov") + completed_application(submitter=contact, name="city4.gov") + completed_application(submitter=contact, name="city5.gov") + + with patch("django.contrib.messages.warning") as mock_warning: + # Use the test client to simulate the request + response = self.client.get(reverse("admin:registrar_contact_change", args=[contact.pk])) + + logger.info(mock_warning) + + # Assert that the error message was called with the correct argument + # Note: The 6th join will be a user. + mock_warning.assert_called_once_with( + response.wsgi_request, + "Joined to DomainApplication: city1.gov
" + "Joined to DomainApplication: city2.gov
" + "Joined to DomainApplication: city3.gov
" + "Joined to DomainApplication: city4.gov
" + "Joined to DomainApplication: city5.gov
" + "And 1 more...", + ) + def tearDown(self): + DomainApplication.objects.all().delete() + Contact.objects.all().delete() User.objects.all().delete()