From 8eeffc77b5cdf68c090d695c8c2fbe79029727ea Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Fri, 24 Nov 2023 05:09:02 +0800 Subject: [PATCH] Allow setting validation_rules in class def --- graphene_django/tests/test_views.py | 23 +++++++++++------------ graphene_django/tests/urls_validation.py | 5 +++++ graphene_django/views.py | 2 +- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/graphene_django/tests/test_views.py b/graphene_django/tests/test_views.py index ac4c924..f718dd3 100644 --- a/graphene_django/tests/test_views.py +++ b/graphene_django/tests/test_views.py @@ -829,6 +829,8 @@ def test_query_errors_non_atomic(set_rollback_mock, client): set_rollback_mock.assert_not_called() +validation_urls = ["/graphql/validation/", "/graphql/validation/alternative/"] + query_with_two_introspections = """ query Instrospection { queryType: __schema { @@ -856,22 +858,20 @@ def test_allow_introspection(client): } +@pytest.mark.parametrize("url", validation_urls) @pytest.mark.urls("graphene_django.tests.urls_validation") -def test_validation_disallow_introspection(client): - response = client.post( - url_string("/graphql/validation/", query="{__schema {queryType {name}}}") - ) +def test_validation_disallow_introspection(client, url): + response = client.post(url_string(url, query="{__schema {queryType {name}}}")) assert response.status_code == 400 assert introspection_disallow_error_message in response.content.decode() +@pytest.mark.parametrize("url", validation_urls) @pytest.mark.urls("graphene_django.tests.urls_validation") @patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 2) -def test_within_max_validation_errors(client): - response = client.post( - url_string("/graphql/validation/", query=query_with_two_introspections) - ) +def test_within_max_validation_errors(client, url): + response = client.post(url_string(url, query=query_with_two_introspections)) assert response.status_code == 400 @@ -881,12 +881,11 @@ def test_within_max_validation_errors(client): assert max_validation_errors_exceeded_message not in text_response +@pytest.mark.parametrize("url", validation_urls) @pytest.mark.urls("graphene_django.tests.urls_validation") @patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 1) -def test_exceeds_max_validation_errors(client): - response = client.post( - url_string("/graphql/validation/", query=query_with_two_introspections) - ) +def test_exceeds_max_validation_errors(client, url): + response = client.post(url_string(url, query=query_with_two_introspections)) assert response.status_code == 400 assert max_validation_errors_exceeded_message in response.content.decode().lower() diff --git a/graphene_django/tests/urls_validation.py b/graphene_django/tests/urls_validation.py index e8dab36..91eafa1 100644 --- a/graphene_django/tests/urls_validation.py +++ b/graphene_django/tests/urls_validation.py @@ -10,7 +10,12 @@ class View(GraphQLView): schema = schema +class NoIntroSpectionView(View): + validation_rules = (DisableIntrospection,) + + urlpatterns = [ path("graphql/", View.as_view()), path("graphql/validation/", View.as_view(validation_rules=(DisableIntrospection,))), + path("graphql/validation/alternative/", NoIntroSpectionView.as_view()), ] diff --git a/graphene_django/views.py b/graphene_django/views.py index fca9194..1ec6598 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -137,7 +137,7 @@ class GraphQLView(View): ), "A Schema is required to be provided to GraphQLView." assert not all((graphiql, batch)), "Use either graphiql or batch processing" - self.validation_rules = validation_rules + self.validation_rules = validation_rules or self.validation_rules # noinspection PyUnusedLocal def get_root_value(self, request):