-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_graphql_ide.py
More file actions
216 lines (173 loc) · 6.43 KB
/
test_graphql_ide.py
File metadata and controls
216 lines (173 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from typing import Union
from typing_extensions import Literal
from urllib.parse import quote
import pytest
from .clients.base import HttpClient
@pytest.mark.parametrize(
"header_value",
[
"text/html",
],
)
@pytest.mark.parametrize(
"graphql_ide_and_title",
[
("graphiql", "GraphiQL"),
("apollo-sandbox", "Apollo Sandbox"),
("pathfinder", "GraphQL Pathfinder"),
],
)
async def test_renders_graphql_ide(
header_value: str,
http_client_class: type[HttpClient],
graphql_ide_and_title: (
tuple[Literal["graphiql"], Literal["GraphiQL"]]
| tuple[Literal["apollo-sandbox"], Literal["Apollo Sandbox"]]
| tuple[Literal["pathfinder"], Literal["GraphQL Pathfinder"]]
),
):
graphql_ide, title = graphql_ide_and_title
http_client = http_client_class(graphql_ide=graphql_ide)
response = await http_client.get("/graphql", headers={"Accept": header_value})
content_type = response.headers.get(
"content-type", response.headers.get("Content-Type", "")
)
assert response.status_code == 200
assert "text/html" in content_type
assert f"<title>{title}</title>" in response.text
if graphql_ide == "apollo-sandbox":
assert "embeddable-sandbox.cdn.apollographql" in response.text
if graphql_ide == "pathfinder":
assert "@pathfinder-ide/react" in response.text
if graphql_ide == "graphiql":
assert "unpkg.com/graphiql" in response.text
@pytest.mark.parametrize(
"header_value",
[
"text/html",
],
)
async def test_renders_graphql_ide_deprecated(
header_value: str, http_client_class: type[HttpClient]
):
with pytest.deprecated_call(
match=r"The `graphiql` argument is deprecated in favor of `graphql_ide`"
):
http_client = http_client_class(graphiql=True)
response = await http_client.get("/graphql", headers={"Accept": header_value})
content_type = response.headers.get(
"content-type", response.headers.get("Content-Type", "")
)
assert response.status_code == 200
assert "text/html" in content_type
assert "<title>GraphiQL</title>" in response.text
assert "https://unpkg.com/graphiql" in response.text
async def test_does_not_render_graphiql_if_wrong_accept(
http_client_class: type[HttpClient],
):
http_client = http_client_class()
response = await http_client.get("/graphql", headers={"Accept": "text/xml"})
assert response.status_code != 200
@pytest.mark.parametrize("graphql_ide", [False, None])
async def test_renders_graphiql_disabled(
http_client_class: type[HttpClient],
graphql_ide: Union[bool, None],
):
http_client = http_client_class(graphql_ide=graphql_ide)
response = await http_client.get("/graphql", headers={"Accept": "text/html"})
assert response.status_code != 200
async def test_renders_graphiql_disabled_deprecated(
http_client_class: type[HttpClient],
):
with pytest.deprecated_call(
match=r"The `graphiql` argument is deprecated in favor of `graphql_ide`"
):
http_client = http_client_class(graphiql=False)
response = await http_client.get("/graphql", headers={"Accept": "text/html"})
assert response.status_code != 200
@pytest.mark.parametrize(
"header_value",
[
"text/html",
],
)
@pytest.mark.parametrize(
"graphql_ide_and_title",
[
("graphiql", "GraphiQL"),
# ("apollo-sandbox", "Apollo Sandbox"),
# ("pathfinder", "GraphQL Pathfinder"),
],
)
async def test_renders_graphql_ide_with_variables(
header_value: str,
http_client_class: type[HttpClient],
graphql_ide_and_title: (
tuple[Literal["graphiql"], Literal["GraphiQL"]]
| tuple[Literal["apollo-sandbox"], Literal["Apollo Sandbox"]]
| tuple[Literal["pathfinder"], Literal["GraphQL Pathfinder"]]
),
):
graphql_ide, title = graphql_ide_and_title
http_client = http_client_class(graphql_ide=graphql_ide)
query = "query { __typename }"
query_encoded = quote(query)
response = await http_client.get(
f"/graphql?query={query_encoded}", headers={"Accept": header_value}
)
content_type = response.headers.get(
"content-type", response.headers.get("Content-Type", "")
)
assert response.status_code == 200
assert "text/html" in content_type
assert f"<title>{title}</title>" in response.text
assert "__typename" in response.text
if graphql_ide == "apollo-sandbox":
assert "embeddable-sandbox.cdn.apollographql" in response.text
if graphql_ide == "pathfinder":
assert "@pathfinder-ide/react" in response.text
if graphql_ide == "graphiql":
assert "unpkg.com/graphiql" in response.text
async def test_renders_graphql_ide_with_operation_name(
http_client_class: type[HttpClient],
):
http_client = http_client_class(graphql_ide="graphiql")
query = "query TestOp { __typename }"
query_encoded = quote(query)
operation_name = "TestOp"
operation_name_encoded = quote(operation_name)
response = await http_client.get(
f"/graphql?query={query_encoded}&operationName={operation_name_encoded}",
headers={"Accept": "text/html"},
)
assert response.status_code == 200
assert 'operationName: "TestOp"' in response.text
async def test_renders_graphql_ide_without_html_escaping(
http_client_class: type[HttpClient],
):
http_client = http_client_class(graphql_ide="graphiql")
# Use a query with a quoted string arg to ensure " characters are present
# in the raw value
query = '{ field(arg: "value") }'
query_encoded = quote(query)
response = await http_client.get(
f"/graphql?query={query_encoded}",
headers={"Accept": "text/html"},
)
assert response.status_code == 200
# Verify JSON values are not escaped (e.g. " instead of ")
assert "&#" not in response.text
async def test_renders_graphql_ide_with_script_tag_in_query(
http_client_class: type[HttpClient],
):
http_client = http_client_class(graphql_ide="graphiql")
query = "{ field } </script>"
query_encoded = quote(query)
response = await http_client.get(
f"/graphql?query={query_encoded}",
headers={"Accept": "text/html"},
)
assert response.status_code == 200
# The < and > in the query must be escaped as \u003c and \u003e so the
# HTML parser doesn't see a literal </script> and close the tag early.
assert "\\u003c/script\\u003e" in response.text