Skip to content

Commit 3ca1244

Browse files
committed
Add tests for validation rules
1 parent 918b590 commit 3ca1244

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

graphene_django/tests/test_views.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,66 @@ def test_query_errors_atomic_request(set_rollback_mock, client):
827827
def test_query_errors_non_atomic(set_rollback_mock, client):
828828
client.get(url_string(query="force error"))
829829
set_rollback_mock.assert_not_called()
830+
831+
832+
query_with_two_introspections = """
833+
query Instrospection {
834+
queryType: __schema {
835+
queryType {name}
836+
}
837+
mutationType: __schema {
838+
mutationType {name}
839+
}
840+
}
841+
"""
842+
843+
introspection_disallow_error_message = "introspection is disabled"
844+
max_validation_errors_exceeded_message = "too many validation errors"
845+
846+
847+
@pytest.mark.urls("graphene_django.tests.urls_validation")
848+
def test_allow_introspection(client):
849+
response = client.post(
850+
url_string("/graphql/", query="{__schema {queryType {name}}}")
851+
)
852+
assert response.status_code == 200
853+
854+
assert response_json(response) == {
855+
"data": {"__schema": {"queryType": {"name": "QueryRoot"}}}
856+
}
857+
858+
859+
@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+
)
864+
865+
assert response.status_code == 400
866+
assert introspection_disallow_error_message in response.content.decode()
867+
868+
869+
@pytest.mark.urls("graphene_django.tests.urls_validation")
870+
@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+
)
875+
876+
assert response.status_code == 400
877+
878+
text_response = response.content.decode().lower()
879+
880+
assert text_response.count(introspection_disallow_error_message) == 2
881+
assert max_validation_errors_exceeded_message not in text_response
882+
883+
884+
@pytest.mark.urls("graphene_django.tests.urls_validation")
885+
@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+
)
890+
891+
assert response.status_code == 400
892+
assert max_validation_errors_exceeded_message in response.content.decode().lower()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.urls import path
2+
3+
from graphene.validation import DisableIntrospection
4+
5+
from ..views import GraphQLView
6+
from .schema_view import schema
7+
8+
9+
class View(GraphQLView):
10+
schema = schema
11+
12+
13+
urlpatterns = [
14+
path("graphql/", View.as_view()),
15+
path("graphql/validation/", View.as_view(validation_rules=(DisableIntrospection,))),
16+
]

0 commit comments

Comments
 (0)