Revise to show ellipsis with numbers, unit tests

This commit is contained in:
Rachid Mrad 2024-01-25 14:37:29 -05:00
parent a9a256127a
commit 6c8e1fec59
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 85 additions and 13 deletions

View file

@ -491,11 +491,14 @@ class ContactAdmin(ListHeaderAdmin):
if related_objects: if related_objects:
message = "" message = ""
for url, obj in related_objects: for i, (url, obj) in enumerate(related_objects):
if i < 5:
escaped_obj = escape(obj) escaped_obj = escape(obj)
message += f"Joined to {obj.__class__.__name__}: <a href='{url}'>{escaped_obj}</a><br/>" message += f"Joined to {obj.__class__.__name__}: <a href='{url}'>{escaped_obj}</a><br/>"
# message_html is considered safe html. It is generated from a finite list of strings if len(related_objects) > 5:
# which are generated from django objects. And a django object, which is escaped related_objects_over_five = len(related_objects) - 5
message += f"And {related_objects_over_five} more..."
message_html = mark_safe(message) # nosec message_html = mark_safe(message) # nosec
messages.warning( messages.warning(
request, request,

View file

@ -526,6 +526,7 @@ def completed_application(
has_anything_else=True, has_anything_else=True,
status=DomainApplication.ApplicationStatus.STARTED, status=DomainApplication.ApplicationStatus.STARTED,
user=False, user=False,
submitter=False,
name="city.gov", name="city.gov",
): ):
"""A completed domain application.""" """A completed domain application."""
@ -541,7 +542,8 @@ def completed_application(
domain, _ = DraftDomain.objects.get_or_create(name=name) domain, _ = DraftDomain.objects.get_or_create(name=name)
alt, _ = Website.objects.get_or_create(website="city1.gov") alt, _ = Website.objects.get_or_create(website="city1.gov")
current, _ = Website.objects.get_or_create(website="city.com") current, _ = Website.objects.get_or_create(website="city.com")
you, _ = Contact.objects.get_or_create( if not submitter:
submitter, _ = Contact.objects.get_or_create(
first_name="Testy2", first_name="Testy2",
last_name="Tester2", last_name="Tester2",
title="Admin Tester", title="Admin Tester",
@ -567,7 +569,7 @@ def completed_application(
zipcode="10002", zipcode="10002",
authorizing_official=ao, authorizing_official=ao,
requested_domain=domain, requested_domain=domain,
submitter=you, submitter=submitter,
creator=user, creator=user,
status=status, status=status,
) )

View file

@ -1735,5 +1735,72 @@ class ContactAdminTest(TestCase):
self.assertEqual(readonly_fields, expected_fields) 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: <a href='/admin/registrar/domainapplication/1/change/'>city1.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/2/change/'>city2.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/3/change/'>city3.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/4/change/'>city4.gov</a><br/>"
"Joined to User: <a href='/admin/registrar/user/2/change/'>staff@example.com</a><br/>",
)
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: <a href='/admin/registrar/domainapplication/1/change/'>city1.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/2/change/'>city2.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/3/change/'>city3.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/4/change/'>city4.gov</a><br/>"
"Joined to DomainApplication: <a href='/admin/registrar/domainapplication/5/change/'>city5.gov</a><br/>"
"And 1 more...",
)
def tearDown(self): def tearDown(self):
DomainApplication.objects.all().delete()
Contact.objects.all().delete()
User.objects.all().delete() User.objects.all().delete()