-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_error.py
410 lines (327 loc) · 14.6 KB
/
test_error.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Copyright (C) DATADVANCE, 2010-2023
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Check different error cases."""
# NOTE: In this file we use `strict_ordering=True` to simplify testing.
import uuid
import graphene
import pytest
import channels_graphql_ws
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_syntax_error(gql, subprotocol):
"""Test that server responds correctly on syntax error(s)."""
print("Establish & initialize WebSocket GraphQL connection.")
client = gql(query=Query, subprotocol=subprotocol)
await client.connect_and_init()
print(
"Check that query syntax error leads to the `data` response "
"with `errors` array."
)
msg_id = await client.start(query="This produces a syntax error!")
errors, data = await client.receive_error(msg_id)
assert data is None
assert len(errors) == 1, "Single error expected!"
assert (
"message" in errors[0] and "locations" in errors[0]
), "Response missing mandatory fields!"
assert errors[0]["locations"] == [{"line": 1, "column": 1}]
if subprotocol == "graphql-ws":
await client.receive_complete(msg_id)
print("Check multiple errors in the `data` message.")
msg_id = await client.start(
query="""
query { projects { path wrong_field } }
query a { projects }
{ wrong_name }
"""
)
errors, data = await client.receive_error(msg_id)
assert data is None
assert len(errors) == 5, f"Five errors expected, but {len(errors)} errors received!"
assert errors[0]["message"] == errors[3]["message"]
assert "locations" in errors[2], "The `locations` field expected"
assert "locations" in errors[4], "The `locations` field expected"
if subprotocol == "graphql-ws":
await client.receive_complete(msg_id)
print("Disconnect and wait the application to finish gracefully.")
await client.finalize()
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_resolver_error(gql, subprotocol):
"""Test that server responds correctly when error in resolver.
Check that server responds with message of type `next`/`data` with
`errors` array , when the exception in a resolver was raised.
"""
print("Establish & initialize WebSocket GraphQL connection.")
client = gql(query=Query, subprotocol=subprotocol)
await client.connect_and_init()
print(
"Check that syntax error leads to the `next`/`data`"
"response with `errors` array."
)
msg_id = await client.start(
query="query op_name { value(issue_error: true) }", operation_name="op_name"
)
with pytest.raises(channels_graphql_ws.GraphqlWsResponseError) as exc_info:
await client.receive_next(msg_id)
payload = exc_info.value.response["payload"]
assert payload["data"]["value"] is None
assert len(payload["errors"]) == 1, "Single error expected!"
assert payload["errors"][0]["message"] == Query.VALUE
assert "locations" in payload["errors"][0]
assert (
"extensions" not in payload["errors"][0]
), "For syntax error there should be no 'extensions'."
await client.receive_complete(msg_id)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
async def test_connection_init_timeout_error_graphql_transport_ws(gql):
"""Test how server works with `connection_init_wait_timeout`.
Server must close WebSocket connection with code 4408 if connection
was not initialized (client don't send `connection_init` message)
after `connection_init_wait_timeout` seconds.
"""
print("Establish WebSocket GraphQL connection.")
client = gql(
consumer_attrs={"strict_ordering": True, "connection_init_wait_timeout": 3},
)
await client.connect_and_init(connect_only=True)
print(
"Wait until server close connection because client don't send"
"`connection_init` message within 3 seconds."
)
await client.wait_disconnect(assert_code=4408)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
async def test_connection_unauthorized_error_graphql_transport_ws(gql):
"""Test how server handles `subscribe` before `connection_ack`.
Server must close WebSocket connection with code 4401 if client
trying to subscribe before connection acknowledgment (before server
send `connection_ack` message).
"""
print("Establish WebSocket GraphQL connection.")
client = gql(
query=Query,
consumer_attrs={"strict_ordering": True},
)
await client.connect_and_init(connect_only=True)
print("Send `subscribe` message.")
await client.start(query="query op_name { value }", operation_name="op_name")
await client.wait_disconnect(assert_code=4401)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_wrong_message_type_error(gql, subprotocol):
"""Test how server handles request with wrong message type.
With the `graphql-transport-ws` subprotocol, server must close
WebSocket connection with code 4400 when client send request with
the wrong message type. With the `graphql-ws` subprotocol server
must send `error` message.
"""
print("Establish WebSocket GraphQL connection.")
client = gql(consumer_attrs={"strict_ordering": True}, subprotocol=subprotocol)
await client.connect_and_init(connect_only=True)
print("Send message with wrong type.")
msg_id = await client.send_raw_message(
{"type": "wrong_type__(ツ)_/¯", "variables": {}, "operationName": ""}
)
if subprotocol == "graphql-transport-ws":
await client.wait_disconnect(assert_code=4400)
else:
with pytest.raises(channels_graphql_ws.GraphqlWsResponseError) as exc_info:
await client.receive(assert_id=msg_id, assert_type="error")
payload = exc_info.value.response["payload"]
assert len(payload["errors"]) == 1, "Multiple errors received instead of one!"
assert isinstance(payload["errors"][0], dict), "Error must be of dict type!"
assert isinstance(
payload["errors"][0]["message"], str
), "Error's message must be of str type!"
assert (
payload["errors"][0]["extensions"]["code"] == "Exception"
), "Error must have 'code' field."
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
async def test_many_init_requests_error_graphql_transport_ws(gql):
"""Test how server handles more than 1 `connection_init` messages.
Server must close WebSocket connection with code 4429 if client send
more than 1 `connection_init` messages.
"""
print("Establish WebSocket GraphQL connection.")
client = gql(
consumer_attrs={"strict_ordering": True},
)
await client.connect_and_init()
print("Send second `connection_init` message.")
await client.send_raw_message({"type": "connection_init", "payload": ""})
await client.wait_disconnect(assert_code=4429)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
async def test_subscriber_already_exists_error_graphql_transport_ws(gql):
"""Test how server handles subscription with same id.
Server must close WebSocket connection with code 4409 if client sent
`subscribe` message with id of subscription that already exists.
"""
print("Establish WebSocket GraphQL connection.")
client = gql(
subscription=Subscription,
consumer_attrs={"strict_ordering": True},
)
await client.connect_and_init()
print("Send two `subscribe` messages with same id.")
for _ in range(2):
await client.start(
query="""
subscription { test_subscription (switch: "NONE\") { ok } }
""",
msg_id="NOT_UNIQUE_ID",
)
await client.wait_disconnect(assert_code=4409)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_connection_error(gql, subprotocol):
"""Test that server disconnects user when `on_connect` raises error.
With the `graphql-transport-ws` subprotocol, server must close the
connection with code 4403 when `on_connect` raises `RuntimeError`.
With the `graphql-ws` subprotocol server must send a proper error
message (with type `connection_error`) and then disconnect.
"""
print("Establish WebSocket GraphQL connection.")
def on_connect(self, payload):
del self, payload
raise RuntimeError("Connection rejected!")
client = gql(
consumer_attrs={"strict_ordering": True, "on_connect": on_connect},
subprotocol=subprotocol,
)
await client.connect_and_init(connect_only=True)
print("Try to initialize the connection.")
await client.send_raw_message({"type": "connection_init"})
if subprotocol == "graphql-ws":
resp = await client.receive(assert_type="connection_error")
assert resp["message"] == "RuntimeError: Connection rejected!"
assert (
resp["extensions"]["code"] == "RuntimeError"
), "Error should have 'extensions' with 'code'."
await client.wait_disconnect()
else:
await client.wait_disconnect(assert_code=4403)
print("Disconnect and wait the application to finish gracefully.")
await client.assert_no_messages()
await client.finalize()
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_subscribe_return_value(gql, subprotocol):
"""Assure the return value of the `subscribe` method is checked.
- Check there is no error when `subscribe` returns nothing, a list
or a tuple.
- Check there is an `AssertionError` when `subscribe` returns
a dict, a string or an empty string.
"""
print("Check there is no error when `subscribe` returns nothing, list, or tuple.")
for result_type in ["NONE", "LIST", "TUPLE"]:
client = gql(subscription=Subscription, subprotocol=subprotocol)
await client.connect_and_init()
await client.start(
query=f"""
subscription {{ test_subscription (switch: "{result_type}\") {{ ok }} }}
"""
)
await client.assert_no_messages("Subscribe responded with a message!")
await client.finalize()
print("Check there is an error when `subscribe` returns string or dict.")
for result_type in ["STR", "DICT", "EMPTYSTR"]:
client = gql(subscription=Subscription, subprotocol=subprotocol)
await client.connect_and_init()
msg_id = await client.start(
query=f"""
subscription {{ test_subscription (switch: "{result_type}") {{ ok }} }}
"""
)
errors, _ = await client.receive_error(msg_id)
assert "AssertionError" in errors[0]["message"], (
"There is no error in response"
" to the wrong type of the `subscribe` result!"
)
assert (
errors[0]["extensions"]["code"] == "AssertionError"
), "Error should have 'extensions' with 'code'."
await client.finalize()
# ---------------------------------------------------------------------- GRAPHQL BACKEND
class Query(graphene.ObjectType):
"""Root GraphQL query."""
VALUE = str(uuid.uuid4().hex)
value = graphene.String(args={"issue_error": graphene.Boolean(default_value=False)})
def resolve_value(self, info, issue_error):
"""Resolver to return predefined value which can be tested."""
del info
assert self is None, "Root `self` expected to be `None`!"
if issue_error:
raise RuntimeError(Query.VALUE)
return Query.VALUE
class TestSubscription(channels_graphql_ws.Subscription):
"""Test subscription with a "special" `subscribe` method.
The `subscribe` method returns values of different types
depending on the subscription parameter `switch`.
"""
# Returning different values (even `None`) from the `subscribe`
# method is the main idea of this test. Make Pylint ignore this.
# pylint: disable=inconsistent-return-statements
ok = graphene.Boolean()
class Arguments:
"""Argument which controls `subscribe` result value."""
switch = graphene.String()
@staticmethod
def subscribe(root, info, switch):
"""This returns nothing which must be OK."""
del root, info
if switch == "NONE":
return None
if switch == "LIST":
return ["group"]
if switch == "TUPLE":
return ("group",)
if switch == "STR":
return "group"
if switch == "DICT":
return "group"
if switch == "EMPTYSTR":
return ""
@staticmethod
def publish(payload, info):
"""We will never get here in this test."""
del payload, info
assert False # raises AssertionError exception
class Subscription(graphene.ObjectType):
"""Root subscription."""
test_subscription = TestSubscription.Field()