Allow setting validation_rules in class def

This commit is contained in:
Kien Dang 2023-11-24 05:09:02 +08:00
parent 9a56a2b751
commit 8eeffc77b5
3 changed files with 17 additions and 13 deletions

View File

@ -829,6 +829,8 @@ def test_query_errors_non_atomic(set_rollback_mock, client):
set_rollback_mock.assert_not_called() set_rollback_mock.assert_not_called()
validation_urls = ["/graphql/validation/", "/graphql/validation/alternative/"]
query_with_two_introspections = """ query_with_two_introspections = """
query Instrospection { query Instrospection {
queryType: __schema { 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") @pytest.mark.urls("graphene_django.tests.urls_validation")
def test_validation_disallow_introspection(client): def test_validation_disallow_introspection(client, url):
response = client.post( response = client.post(url_string(url, query="{__schema {queryType {name}}}"))
url_string("/graphql/validation/", query="{__schema {queryType {name}}}")
)
assert response.status_code == 400 assert response.status_code == 400
assert introspection_disallow_error_message in response.content.decode() assert introspection_disallow_error_message in response.content.decode()
@pytest.mark.parametrize("url", validation_urls)
@pytest.mark.urls("graphene_django.tests.urls_validation") @pytest.mark.urls("graphene_django.tests.urls_validation")
@patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 2) @patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 2)
def test_within_max_validation_errors(client): def test_within_max_validation_errors(client, url):
response = client.post( response = client.post(url_string(url, query=query_with_two_introspections))
url_string("/graphql/validation/", query=query_with_two_introspections)
)
assert response.status_code == 400 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 assert max_validation_errors_exceeded_message not in text_response
@pytest.mark.parametrize("url", validation_urls)
@pytest.mark.urls("graphene_django.tests.urls_validation") @pytest.mark.urls("graphene_django.tests.urls_validation")
@patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 1) @patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 1)
def test_exceeds_max_validation_errors(client): def test_exceeds_max_validation_errors(client, url):
response = client.post( response = client.post(url_string(url, query=query_with_two_introspections))
url_string("/graphql/validation/", query=query_with_two_introspections)
)
assert response.status_code == 400 assert response.status_code == 400
assert max_validation_errors_exceeded_message in response.content.decode().lower() assert max_validation_errors_exceeded_message in response.content.decode().lower()

View File

@ -10,7 +10,12 @@ class View(GraphQLView):
schema = schema schema = schema
class NoIntroSpectionView(View):
validation_rules = (DisableIntrospection,)
urlpatterns = [ urlpatterns = [
path("graphql/", View.as_view()), path("graphql/", View.as_view()),
path("graphql/validation/", View.as_view(validation_rules=(DisableIntrospection,))), path("graphql/validation/", View.as_view(validation_rules=(DisableIntrospection,))),
path("graphql/validation/alternative/", NoIntroSpectionView.as_view()),
] ]

View File

@ -137,7 +137,7 @@ class GraphQLView(View):
), "A Schema is required to be provided to GraphQLView." ), "A Schema is required to be provided to GraphQLView."
assert not all((graphiql, batch)), "Use either graphiql or batch processing" 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 # noinspection PyUnusedLocal
def get_root_value(self, request): def get_root_value(self, request):