Skip to content

Commit dfbb8bf

Browse files
committed
Allow setting validation_rules in class def
1 parent 9a56a2b commit dfbb8bf

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

Diff for: graphene_django/tests/test_views.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ def test_query_errors_non_atomic(set_rollback_mock, client):
829829
set_rollback_mock.assert_not_called()
830830

831831

832+
validation_urls = ["/graphql/validation/", "/graphql/validation/alternative/"]
833+
832834
query_with_two_introspections = """
833835
query Instrospection {
834836
queryType: __schema {
@@ -856,22 +858,20 @@ def test_allow_introspection(client):
856858
}
857859

858860

861+
@pytest.mark.parametrize("url", validation_urls)
859862
@pytest.mark.urls("graphene_django.tests.urls_validation")
860-
def test_validation_disallow_introspection(client):
861-
response = client.post(
862-
url_string("/graphql/validation/", query="{__schema {queryType {name}}}")
863-
)
863+
def test_validation_disallow_introspection(client, url):
864+
response = client.post(url_string(url, query="{__schema {queryType {name}}}"))
864865

865866
assert response.status_code == 400
866867
assert introspection_disallow_error_message in response.content.decode()
867868

868869

870+
@pytest.mark.parametrize("url", validation_urls)
869871
@pytest.mark.urls("graphene_django.tests.urls_validation")
870872
@patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 2)
871-
def test_within_max_validation_errors(client):
872-
response = client.post(
873-
url_string("/graphql/validation/", query=query_with_two_introspections)
874-
)
873+
def test_within_max_validation_errors(client, url):
874+
response = client.post(url_string(url, query=query_with_two_introspections))
875875

876876
assert response.status_code == 400
877877

@@ -881,12 +881,11 @@ def test_within_max_validation_errors(client):
881881
assert max_validation_errors_exceeded_message not in text_response
882882

883883

884+
@pytest.mark.parametrize("url", validation_urls)
884885
@pytest.mark.urls("graphene_django.tests.urls_validation")
885886
@patch("graphene_django.settings.graphene_settings.MAX_VALIDATION_ERRORS", 1)
886-
def test_exceeds_max_validation_errors(client):
887-
response = client.post(
888-
url_string("/graphql/validation/", query=query_with_two_introspections)
889-
)
887+
def test_exceeds_max_validation_errors(client, url):
888+
response = client.post(url_string(url, query=query_with_two_introspections))
890889

891890
assert response.status_code == 400
892891
assert max_validation_errors_exceeded_message in response.content.decode().lower()

Diff for: graphene_django/tests/urls_validation.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ class View(GraphQLView):
1010
schema = schema
1111

1212

13+
class NoIntroSpectionView(GraphQLView):
14+
schema = schema
15+
validation_rules = (DisableIntrospection,)
16+
17+
1318
urlpatterns = [
1419
path("graphql/", View.as_view()),
1520
path("graphql/validation/", View.as_view(validation_rules=(DisableIntrospection,))),
21+
path("graphql/validation/alternative/", NoIntroSpectionView.as_view()),
1622
]

Diff for: graphene_django/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(
137137
), "A Schema is required to be provided to GraphQLView."
138138
assert not all((graphiql, batch)), "Use either graphiql or batch processing"
139139

140-
self.validation_rules = validation_rules
140+
self.validation_rules = validation_rules or self.validation_rules
141141

142142
# noinspection PyUnusedLocal
143143
def get_root_value(self, request):

0 commit comments

Comments
 (0)