-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codemod for disabling GraphQL introspection (#542)
* Initial implementation of disable-grapql-introspection * Refactored resolve methods and added tests * fixup! Refactored resolve methods and added tests * Bugfixes and tests * Sonar version of codemod * fixup! Sonar version of codemod * Refactoring * Apply suggestions from code review Co-authored-by: Dan D'Avella <[email protected]> --------- Co-authored-by: Dan D'Avella <[email protected]>
- Loading branch information
1 parent
17ed312
commit 989cd69
Showing
16 changed files
with
925 additions
and
11 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
integration_tests/sonar/test_sonar_disable_graphql_introspection.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,63 @@ | ||
from codemodder.codemods.test import SonarIntegrationTest | ||
from core_codemods.disable_graphql_introspection import ( | ||
DisableGraphQLIntrospectionTransform, | ||
) | ||
from core_codemods.sonar.sonar_disable_graphql_introspection import ( | ||
SonarDisableGraphQLIntrospection, | ||
) | ||
|
||
|
||
class TestSonarDisableGraphQLIntrospection(SonarIntegrationTest): | ||
codemod = SonarDisableGraphQLIntrospection | ||
code_path = "tests/samples/disable_graphql_introspection.py" | ||
expected_new_code = """\ | ||
from graphql_server.flask import GraphQLView | ||
from flask import Flask | ||
from graphql import ( | ||
GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString) | ||
from graphql.validation import NoSchemaIntrospectionCustomRule | ||
schema = GraphQLSchema( | ||
query=GraphQLObjectType( | ||
name='RootQueryType', | ||
fields={ | ||
'hello': GraphQLField( | ||
GraphQLString, | ||
resolve=lambda obj, info: 'world') | ||
})) | ||
app = Flask(__name__) | ||
app.add_url_rule("/api", | ||
view_func=GraphQLView.as_view( | ||
name="api", | ||
schema=schema, | ||
validation_rules = [NoSchemaIntrospectionCustomRule,]), | ||
) | ||
""" | ||
|
||
# fmt: off | ||
expected_diff = ( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -2,6 +2,7 @@\n""" | ||
""" from flask import Flask\n""" | ||
""" from graphql import (\n""" | ||
""" GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString)\n""" | ||
"""+from graphql.validation import NoSchemaIntrospectionCustomRule\n""" | ||
""" \n""" | ||
""" schema = GraphQLSchema(\n""" | ||
""" query=GraphQLObjectType(\n""" | ||
"""@@ -18,5 +19,5 @@\n""" | ||
""" view_func=GraphQLView.as_view(\n""" | ||
""" name="api",\n""" | ||
""" schema=schema,\n""" | ||
"""- ),\n""" | ||
"""+ validation_rules = [NoSchemaIntrospectionCustomRule,]),\n""" | ||
""" )\n""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "18" | ||
change_description = DisableGraphQLIntrospectionTransform.change_description | ||
num_changed_files = 1 |
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,84 @@ | ||
from codemodder.codemods.test.integration_utils import BaseIntegrationTest | ||
from core_codemods.disable_graphql_introspection import ( | ||
DisableGraphQLIntrospection, | ||
DisableGraphQLIntrospectionTransform, | ||
) | ||
|
||
|
||
class TestDisableGraphQLIntrospection(BaseIntegrationTest): | ||
codemod = DisableGraphQLIntrospection | ||
original_code = """ | ||
from graphql_server.flask import GraphQLView | ||
from flask import Flask | ||
from graphql import ( | ||
GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString) | ||
schema = GraphQLSchema( | ||
query=GraphQLObjectType( | ||
name='RootQueryType', | ||
fields={ | ||
'hello': GraphQLField( | ||
GraphQLString, | ||
resolve=lambda obj, info: 'world') | ||
})) | ||
app = Flask(__name__) | ||
app.add_url_rule("/api", | ||
view_func=GraphQLView.as_view( | ||
name="api", | ||
schema=schema, | ||
), | ||
) | ||
""" | ||
expected_new_code = """ | ||
from graphql_server.flask import GraphQLView | ||
from flask import Flask | ||
from graphql import ( | ||
GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString) | ||
from graphql.validation import NoSchemaIntrospectionCustomRule | ||
schema = GraphQLSchema( | ||
query=GraphQLObjectType( | ||
name='RootQueryType', | ||
fields={ | ||
'hello': GraphQLField( | ||
GraphQLString, | ||
resolve=lambda obj, info: 'world') | ||
})) | ||
app = Flask(__name__) | ||
app.add_url_rule("/api", | ||
view_func=GraphQLView.as_view( | ||
name="api", | ||
schema=schema, | ||
validation_rules = [NoSchemaIntrospectionCustomRule,]), | ||
) | ||
""" | ||
|
||
# fmt: off | ||
expected_diff = ( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -2,6 +2,7 @@\n""" | ||
""" from flask import Flask\n""" | ||
""" from graphql import (\n""" | ||
""" GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString)\n""" | ||
"""+from graphql.validation import NoSchemaIntrospectionCustomRule\n""" | ||
""" \n""" | ||
""" schema = GraphQLSchema(\n""" | ||
""" query=GraphQLObjectType(\n""" | ||
"""@@ -18,5 +19,5 @@\n""" | ||
""" view_func=GraphQLView.as_view(\n""" | ||
""" name="api",\n""" | ||
""" schema=schema,\n""" | ||
"""- ),\n""" | ||
"""+ validation_rules = [NoSchemaIntrospectionCustomRule,]),\n""" | ||
""" )""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "18" | ||
change_description = DisableGraphQLIntrospectionTransform.change_description | ||
num_changed_files = 1 |
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
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
Oops, something went wrong.