diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 0b012f7d8..8a10ba321 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -36,6 +36,7 @@ class ListHeaderAdmin(AuditedAdmin): # Pass the filtered values to the template context extra_context['filters'] = filters extra_context['search_query'] = request.GET.get('q', '') # Assuming the search query parameter is 'q' + logger.debug(f'changelist_view {extra_context}') return super().changelist_view(request, extra_context=extra_context) def get_filters(self, request): diff --git a/src/registrar/config/settings.py b/src/registrar/config/settings.py index 1af5a2295..87d565abf 100644 --- a/src/registrar/config/settings.py +++ b/src/registrar/config/settings.py @@ -94,6 +94,8 @@ INSTALLED_APPS = [ # generic interface for Django models "auditlog", # library to simplify form templating + # it needs to be listed before django.contrib.contenttypes + # for a ContentType query in fixtures.py "django.contrib.contenttypes", # required for CSRF protection and many other things "django.contrib.sessions", diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index 2f2e1190b..a72ca9a37 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -1,6 +1,6 @@ -from django.test import TestCase, RequestFactory +from django.test import TestCase, RequestFactory, Client from django.contrib.admin.sites import AdminSite -from registrar.admin import DomainApplicationAdmin +from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin, AuditedAdmin from registrar.models import DomainApplication, User from .common import completed_application @@ -13,6 +13,8 @@ class TestDomainApplicationAdmin(TestCase): def setUp(self): self.site = AdminSite() self.factory = RequestFactory() + self.admin = ListHeaderAdmin(model=DomainApplication, admin_site=None) + self.client = Client(HTTP_HOST='localhost:8080') @boto3_mocking.patching def test_save_model_sends_email_on_property_change(self): @@ -62,3 +64,45 @@ class TestDomainApplicationAdmin(TestCase): # Cleanup application.delete() + + def test_changelist_view(self): + # Make the request using the Client class + # which handles CSRF + # Follow=True handles the redirect + request = self.client.get('/admin/registrar/domainapplication/', {'param1': 'value1', 'param2': 'value2'}, follow=True, max_redirects=10) + + print(f'request {request}') + + # request = self.factory.get('/admin/registrar/domainapplication/') + # # Set the GET parameters for testing + # request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'} + # # Call the changelist_view method + response = self.admin.changelist_view(request, extra_context={'filters': [{'parameter_name': 'status', 'parameter_value': 'started'}], 'search_query': ''}) + + + print(f'response {response}') + + # Assert that the final response is a successful response (not a redirect) + # self.assertEqual(response.status_code, 200) + + # Assert that the filters and search_query are added to the extra_context + self.assertIn('filters', response.extra_context) + self.assertIn('search_query', response.extra_context) + # Assert the content of filters and search_query + filters = response.extra_context['filters'] + search_query = response.extra_context['search_query'] + self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'}, + {'parameter_name': 'param2', 'parameter_value': 'value2'}]) + self.assertEqual(search_query, 'value of q parameter if present in the request GET') + + def test_get_filters(self): + # Create a mock request object + request = self.factory.get('/admin/yourmodel/') + # Set the GET parameters for testing + request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'} + # Call the get_filters method + filters = self.admin.get_filters(request) + + # Assert the filters extracted from the request GET + self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'}, + {'parameter_name': 'param2', 'parameter_value': 'value2'}])