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

@ -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: <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):
DomainApplication.objects.all().delete()
Contact.objects.all().delete()
User.objects.all().delete()