From 1b89031a54416a6de07af92ac250ae0fc6fc6766 Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Mon, 24 Jun 2024 14:23:27 -0400 Subject: [PATCH 1/2] unit test on filters for domain json --- .../tests/test_views_domains_json.py | 31 +++++++++++++++++-- src/registrar/views/domains_json.py | 6 ---- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/registrar/tests/test_views_domains_json.py b/src/registrar/tests/test_views_domains_json.py index 4c9fe5908..28a7308f5 100644 --- a/src/registrar/tests/test_views_domains_json.py +++ b/src/registrar/tests/test_views_domains_json.py @@ -3,6 +3,7 @@ from django.urls import reverse from .test_views import TestWithUser from django_webtest import WebTest # type: ignore from django.utils.dateparse import parse_date +from api.tests.common import less_console_noise_decorator class GetDomainsJsonTest(TestWithUser, WebTest): @@ -11,9 +12,9 @@ class GetDomainsJsonTest(TestWithUser, WebTest): self.app.set_user(self.user.username) # Create test domains - self.domain1 = Domain.objects.create(name="example1.com", expiration_date="2024-01-01", state="active") - self.domain2 = Domain.objects.create(name="example2.com", expiration_date="2024-02-01", state="inactive") - self.domain3 = Domain.objects.create(name="example3.com", expiration_date="2024-03-01", state="active") + self.domain1 = Domain.objects.create(name="example1.com", expiration_date="2024-01-01", state="unknown") + self.domain2 = Domain.objects.create(name="example2.com", expiration_date="2024-02-01", state="dns needed") + self.domain3 = Domain.objects.create(name="example3.com", expiration_date="2024-03-01", state="ready") # Create UserDomainRoles UserDomainRole.objects.create(user=self.user, domain=self.domain1) @@ -25,6 +26,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): UserDomainRole.objects.all().delete() UserDomainRole.objects.all().delete() + @less_console_noise_decorator def test_get_domains_json_unauthenticated(self): """for an unauthenticated user, test that the user is redirected for auth""" self.app.reset() @@ -32,6 +34,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): response = self.client.get(reverse("get_domains_json")) self.assertEqual(response.status_code, 302) + @less_console_noise_decorator def test_get_domains_json_authenticated(self): """Test that an authenticated user gets the list of 3 domains.""" response = self.app.get(reverse("get_domains_json")) @@ -102,6 +105,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): ) self.assertEqual(svg_icon_expected, svg_icons[i]) + @less_console_noise_decorator def test_get_domains_json_search(self): """Test search.""" # Define your URL variables as a dictionary @@ -131,6 +135,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): domains[0], ) + @less_console_noise_decorator def test_pagination(self): """Test that pagination is correct in the response""" response = self.app.get(reverse("get_domains_json"), {"page": 1}) @@ -143,6 +148,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): self.assertFalse(data["has_previous"]) self.assertEqual(data["num_pages"], 1) + @less_console_noise_decorator def test_sorting(self): """test that sorting works properly in the response""" response = self.app.get(reverse("get_domains_json"), {"sort_by": "expiration_date", "order": "desc"}) @@ -161,6 +167,7 @@ class GetDomainsJsonTest(TestWithUser, WebTest): expiration_dates = [domain["expiration_date"] for domain in data["domains"]] self.assertEqual(expiration_dates, sorted(expiration_dates)) + @less_console_noise_decorator def test_sorting_by_state_display(self): """test that the state_display sorting works properly""" response = self.app.get(reverse("get_domains_json"), {"sort_by": "state_display", "order": "asc"}) @@ -178,3 +185,21 @@ class GetDomainsJsonTest(TestWithUser, WebTest): # Check if sorted by state_display in descending order states = [domain["state_display"] for domain in data["domains"]] self.assertEqual(states, sorted(states, reverse=True)) + + @less_console_noise_decorator + def test_state_filtering(self): + """Test that different states in request get expected responses.""" + + expected_values = [ + ("unknown", 1), + ("ready", 0), + ("expired", 2), + ("ready,expired", 2), + ("unknown,expired", 3), + ] + for state, num_domains in expected_values: + with self.subTest(state=state, num_domains=num_domains): + response = self.app.get(reverse("get_domains_json"), {"status": state}) + self.assertEqual(response.status_code, 200) + data = response.json + self.assertEqual(len(data["domains"]), num_domains) diff --git a/src/registrar/views/domains_json.py b/src/registrar/views/domains_json.py index 9d6661e75..af2ae2672 100644 --- a/src/registrar/views/domains_json.py +++ b/src/registrar/views/domains_json.py @@ -4,8 +4,6 @@ from registrar.models import UserDomainRole, Domain from django.contrib.auth.decorators import login_required from django.urls import reverse from django.db.models import Q -import logging -logger = logging.getLogger(__name__) @login_required @@ -37,8 +35,6 @@ def get_domains_json(request): if 'unknown' in status_list: status_list.append('dns needed') - logger.debug(f"Submitted status_list: {status_list}") - # Split the status list into normal states and custom states normal_states = [state for state in status_list if state in Domain.State.values] custom_states = [state for state in status_list if state == "expired"] @@ -53,8 +49,6 @@ def get_domains_json(request): expired_domain_ids = [domain.id for domain in objects if domain.state_display() == "Expired"] state_query |= Q(id__in=expired_domain_ids) - logger.debug(f"State query: {state_query}") - # Apply the combined query objects = objects.filter(state_query) From 3f399db985eb96e3395681af4ce27254461a18af Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Mon, 24 Jun 2024 14:29:17 -0400 Subject: [PATCH 2/2] cleaned up json endpoint with comment and formatting --- src/registrar/views/domains_json.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/registrar/views/domains_json.py b/src/registrar/views/domains_json.py index af2ae2672..8b107ca67 100644 --- a/src/registrar/views/domains_json.py +++ b/src/registrar/views/domains_json.py @@ -31,9 +31,11 @@ def get_domains_json(request): if status_param: status_list = status_param.split(",") - # if unknown is in status_list, append 'dns needed' - if 'unknown' in status_list: - status_list.append('dns needed') + # if unknown is in status_list, append 'dns needed' since both + # unknown and dns needed display as DNS Needed, and both are + # searchable via state parameter of 'unknown' + if "unknown" in status_list: + status_list.append("dns needed") # Split the status list into normal states and custom states normal_states = [state for state in status_list if state in Domain.State.values]