Add missing unit test + linting

This commit is contained in:
zandercymatics 2024-03-04 14:18:59 -07:00
parent cc1555ab9a
commit 747af88347
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 49 additions and 4 deletions

View file

@ -97,7 +97,7 @@ def less_console_noise(output_stream=None):
class GenericTestHelper(TestCase): class GenericTestHelper(TestCase):
"""A helper class that contains various helper functions for TestCases""" """A helper class that contains various helper functions for TestCases"""
def __init__(self, admin, model=None, url=None, user=None, factory=None, **kwargs): def __init__(self, admin, model=None, url=None, user=None, factory=None, client=None, **kwargs):
""" """
Parameters: Parameters:
admin (ModelAdmin): The Django ModelAdmin instance associated with the model. admin (ModelAdmin): The Django ModelAdmin instance associated with the model.
@ -112,6 +112,7 @@ class GenericTestHelper(TestCase):
self.admin = admin self.admin = admin
self.model = model self.model = model
self.url = url self.url = url
self.client = client
def assert_table_sorted(self, o_index, sort_fields): def assert_table_sorted(self, o_index, sort_fields):
""" """
@ -147,9 +148,7 @@ class GenericTestHelper(TestCase):
dummy_request.user = self.user dummy_request.user = self.user
# Mock a user request # Mock a user request
middleware = SessionMiddleware(lambda req: req) dummy_request = self._mock_user_request_for_factory(dummy_request)
middleware.process_request(dummy_request)
dummy_request.session.save()
expected_sort_order = list(self.model.objects.order_by(*sort_fields)) expected_sort_order = list(self.model.objects.order_by(*sort_fields))
@ -160,6 +159,27 @@ class GenericTestHelper(TestCase):
self.assertEqual(expected_sort_order, returned_sort_order) self.assertEqual(expected_sort_order, returned_sort_order)
def _mock_user_request_for_factory(self, request):
"""Adds sessionmiddleware when using factory to associate session information"""
middleware = SessionMiddleware(lambda req: req)
middleware.process_request(request)
request.session.save()
return request
def get_table_delete_confirmation_page(self, selected_across: str, index: str):
"""
Grabs the response for the delete confirmation page (generated from the actions toolbar).
selected_across and index must both be numbers encoded as str, e.g. "0" rather than 0
"""
response = self.client.post(
self.url,
{"action": "delete_selected", "select_across": selected_across, "index": index, "_selected_action": "23"},
follow=True,
)
print(f"what is the response? {response}")
return response
class MockUserLogin: class MockUserLogin:
def __init__(self, get_response): def __init__(self, get_response):

View file

@ -61,6 +61,16 @@ class TestDomainAdmin(MockEppLib, WebTest):
self.factory = RequestFactory() self.factory = RequestFactory()
self.app.set_user(self.superuser.username) self.app.set_user(self.superuser.username)
self.client.force_login(self.superuser) self.client.force_login(self.superuser)
# Contains some test tools
self.test_helper = GenericTestHelper(
factory=self.factory,
user=self.superuser,
admin=self.admin,
url=reverse("admin:registrar_domain_changelist"),
model=Domain,
client=self.client,
)
super().setUp() super().setUp()
@skip("TODO for another ticket. This test case is grabbing old db data.") @skip("TODO for another ticket. This test case is grabbing old db data.")
@ -244,6 +254,21 @@ class TestDomainAdmin(MockEppLib, WebTest):
content_slice = "When a domain is deleted:" content_slice = "When a domain is deleted:"
self.assertContains(confirmation_page, content_slice) self.assertContains(confirmation_page, content_slice)
def test_custom_delete_confirmation_page_table(self):
"""Tests if we override the delete confirmation page for custom content on the table"""
# Create a ready domain
domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY)
# Get the index. The post expects the index to be encoded as a string
index = f"{domain.id}"
# Simulate selecting a single record, then clicking "Delete selected domains"
response = self.test_helper.get_table_delete_confirmation_page("0", index)
# Check that our content exists
content_slice = "When a domain is deleted:"
self.assertContains(response, content_slice)
def test_short_org_name_in_domains_list(self): def test_short_org_name_in_domains_list(self):
""" """
Make sure the short name is displaying in admin on the list page Make sure the short name is displaying in admin on the list page