From 3fc82207c81c4e55ac9710ed4288f6ebc3caf1fa Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 13 May 2024 10:10:23 -0700 Subject: [PATCH 1/4] 16078 make GraphQL NumberFilter optional --- netbox/netbox/graphql/filter_mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/netbox/graphql/filter_mixins.py b/netbox/netbox/graphql/filter_mixins.py index bfb9585637..322435c720 100644 --- a/netbox/netbox/graphql/filter_mixins.py +++ b/netbox/netbox/graphql/filter_mixins.py @@ -87,7 +87,7 @@ def map_strawberry_type(field): pass elif issubclass(type(field), django_filters.NumberFilter): should_create_function = True - attr_type = int + attr_type = int | None elif issubclass(type(field), django_filters.ModelMultipleChoiceFilter): should_create_function = True attr_type = List[str] | None From 27a2bc21ad4e7ba413e39694e8cd99d22c30bb02 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 13 May 2024 12:55:50 -0700 Subject: [PATCH 2/4] 16078 add tests for graphql filtering --- netbox/ipam/tests/test_api.py | 3 +++ netbox/utilities/testing/api.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index 16ee7bbf0c..20ba35c6b6 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -648,6 +648,9 @@ class IPAddressTest(APIViewTestCases.APIViewTestCase): bulk_update_data = { 'description': 'New description', } + graphql_filter = { + 'address': '192.168.0.1/24', + } @classmethod def setUpTestData(cls): diff --git a/netbox/utilities/testing/api.py b/netbox/utilities/testing/api.py index 4802c3ffe0..de01372dda 100644 --- a/netbox/utilities/testing/api.py +++ b/netbox/utilities/testing/api.py @@ -443,8 +443,8 @@ def _get_graphql_base_name(self): def _build_query(self, name, **filters): type_class = get_graphql_type_for_model(self.model) if filters: - filter_string = ', '.join(f'{k}:{v}' for k, v in filters.items()) - filter_string = f'({filter_string})' + filter_string = ', '.join(f'{k}: "{v}"' for k, v in filters.items()) + filter_string = f'(filters: {{{filter_string}}})' else: filter_string = '' @@ -550,6 +550,31 @@ def test_graphql_list_objects(self): self.assertNotIn('errors', data) self.assertGreater(len(data['data'][field_name]), 0) + @override_settings(LOGIN_REQUIRED=True) + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user']) + def test_graphql_filter_objects(self): + if not hasattr(self, 'graphql_filter'): + return + + url = reverse('graphql') + field_name = f'{self._get_graphql_base_name()}_list' + query = self._build_query(field_name, **self.graphql_filter) + + # Add object-level permission + obj_perm = ObjectPermission( + name='Test permission', + actions=['view'] + ) + obj_perm.save() + obj_perm.users.add(self.user) + obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model)) + + response = self.client.post(url, data={'query': query}, format="json", **self.header) + self.assertHttpStatus(response, status.HTTP_200_OK) + data = json.loads(response.content) + self.assertNotIn('errors', data) + self.assertGreater(len(data['data'][field_name]), 0) + class APIViewTestCase( GetObjectViewTestCase, ListObjectsViewTestCase, From b3dd5576cd25405197600752f257daf5476635c2 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 13 May 2024 13:14:45 -0700 Subject: [PATCH 3/4] 16078 add tests for graphql filtering --- netbox/utilities/testing/api.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/netbox/utilities/testing/api.py b/netbox/utilities/testing/api.py index de01372dda..929900af0b 100644 --- a/netbox/utilities/testing/api.py +++ b/netbox/utilities/testing/api.py @@ -440,13 +440,8 @@ def _get_graphql_base_name(self): base_name = self.model._meta.verbose_name.lower().replace(' ', '_') return getattr(self, 'graphql_base_name', base_name) - def _build_query(self, name, **filters): + def _build_query_with_filter(self, name, filter_string): type_class = get_graphql_type_for_model(self.model) - if filters: - filter_string = ', '.join(f'{k}: "{v}"' for k, v in filters.items()) - filter_string = f'(filters: {{{filter_string}}})' - else: - filter_string = '' # Compile list of fields to include fields_string = '' @@ -492,6 +487,24 @@ def _build_query(self, name, **filters): return query + def _build_filtered_query(self, name, **filters): + if filters: + filter_string = ', '.join(f'{k}: "{v}"' for k, v in filters.items()) + filter_string = f'(filters: {{{filter_string}}})' + else: + filter_string = '' + + return self._build_query_with_filter(name, filter_string) + + def _build_query(self, name, **filters): + if filters: + filter_string = ', '.join(f'{k}:{v}' for k, v in filters.items()) + filter_string = f'({filter_string})' + else: + filter_string = '' + + return self._build_query_with_filter(name, filter_string) + @override_settings(LOGIN_REQUIRED=True) @override_settings(EXEMPT_VIEW_PERMISSIONS=['*', 'auth.user']) def test_graphql_get_object(self): @@ -558,7 +571,7 @@ def test_graphql_filter_objects(self): url = reverse('graphql') field_name = f'{self._get_graphql_base_name()}_list' - query = self._build_query(field_name, **self.graphql_filter) + query = self._build_filtered_query(field_name, **self.graphql_filter) # Add object-level permission obj_perm = ObjectPermission( From 9265c8a4e4bc7c72a9254be225d060d86321fff8 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 13 May 2024 14:10:18 -0700 Subject: [PATCH 4/4] 16078 add tests for graphql filtering --- netbox/utilities/testing/api.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/netbox/utilities/testing/api.py b/netbox/utilities/testing/api.py index 929900af0b..563bd84b57 100644 --- a/netbox/utilities/testing/api.py +++ b/netbox/utilities/testing/api.py @@ -441,6 +441,10 @@ def _get_graphql_base_name(self): return getattr(self, 'graphql_base_name', base_name) def _build_query_with_filter(self, name, filter_string): + """ + Called by either _build_query or _build_filtered_query - construct the actual + query given a name and filter string + """ type_class = get_graphql_type_for_model(self.model) # Compile list of fields to include @@ -488,6 +492,9 @@ def _build_query_with_filter(self, name, filter_string): return query def _build_filtered_query(self, name, **filters): + """ + Create a filtered query: i.e. ip_address_list(filters: {address: "1.1.1.1/24"}){. + """ if filters: filter_string = ', '.join(f'{k}: "{v}"' for k, v in filters.items()) filter_string = f'(filters: {{{filter_string}}})' @@ -497,6 +504,9 @@ def _build_filtered_query(self, name, **filters): return self._build_query_with_filter(name, filter_string) def _build_query(self, name, **filters): + """ + Create a normal query - unfiltered or with a string query: i.e. site(name: "aaa"){. + """ if filters: filter_string = ', '.join(f'{k}:{v}' for k, v in filters.items()) filter_string = f'({filter_string})'