-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: HP-2349
- Loading branch information
Showing
5 changed files
with
103 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,7 @@ | ||
import pytest | ||
import requests | ||
from graphql import get_introspection_query, print_schema | ||
from graphql import print_schema | ||
|
||
|
||
def test_graphql_schema_matches_the_reference(gql_schema, snapshot): | ||
actual_schema = print_schema(gql_schema) | ||
|
||
snapshot.assert_match(actual_schema) | ||
|
||
|
||
@pytest.mark.parametrize("enabled", [True, False]) | ||
def test_graphql_schema_introspection_can_be_disabled(live_server, settings, enabled): | ||
settings.ENABLE_GRAPHQL_INTROSPECTION = enabled | ||
url = live_server.url + "/graphql/" | ||
payload = { | ||
"query": get_introspection_query(), | ||
} | ||
|
||
response = requests.post(url, json=payload) | ||
|
||
body = response.json() | ||
if enabled: | ||
assert response.status_code == 200 | ||
assert "errors" not in body | ||
assert "__schema" in body["data"] | ||
else: | ||
assert response.status_code == 400 | ||
assert "data" not in body | ||
error = body["errors"][0]["message"] | ||
assert "__schema" in error | ||
assert "introspection is disabled" in error |
68 changes: 68 additions & 0 deletions
68
open_city_profile/tests/test_graphql_api_validation_rules.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
from graphql import get_introspection_query | ||
|
||
from open_city_profile.tests.graphql_test_helpers import ( | ||
do_graphql_call, | ||
do_graphql_call_as_user, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"enabled, expected_status", | ||
[pytest.param(True, 200, id="enabled"), pytest.param(False, 400, id="disabled")], | ||
) | ||
def test_graphql_schema_introspection_can_be_disabled( | ||
live_server, settings, enabled, expected_status | ||
): | ||
settings.ENABLE_GRAPHQL_INTROSPECTION = enabled | ||
|
||
data, errors = do_graphql_call( | ||
live_server, query=get_introspection_query(), expected_status=expected_status | ||
) | ||
|
||
if enabled: | ||
assert errors is None | ||
assert "__schema" in data | ||
else: | ||
assert data is None | ||
error = errors[0]["message"] | ||
assert "__schema" in error | ||
assert "introspection is disabled" in error | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"depth_limit, expected_status, success", | ||
[ | ||
pytest.param(4, 200, True, id="within-limit"), | ||
pytest.param(3, 400, False, id="over-limit"), | ||
], | ||
) | ||
def test_graphql_query_depth_can_be_limited( | ||
live_server, settings, user, depth_limit, expected_status, success | ||
): | ||
settings.GRAPHQL_QUERY_DEPTH_LIMIT = depth_limit | ||
query = """ | ||
{ | ||
myProfile { | ||
emails { | ||
edges { | ||
node { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
data, errors = do_graphql_call_as_user( | ||
live_server, user, query=query, expected_status=expected_status | ||
) | ||
|
||
if success: | ||
assert errors is None | ||
assert "myProfile" in data | ||
else: | ||
assert data is None | ||
error = errors[0]["message"] | ||
assert "exceeds maximum operation depth" in error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters