1
1
import json
2
+ from http import HTTPStatus
2
3
from unittest .mock import patch
3
4
4
5
import pytest
@@ -37,7 +38,7 @@ def jl(**kwargs):
37
38
38
39
def test_graphiql_is_enabled (client ):
39
40
response = client .get (url_string (), HTTP_ACCEPT = "text/html" )
40
- assert response .status_code == 200
41
+ assert response .status_code == HTTPStatus . OK
41
42
assert response ["Content-Type" ].split (";" )[0 ] == "text/html"
42
43
43
44
@@ -46,7 +47,7 @@ def test_qfactor_graphiql(client):
46
47
url_string (query = "{test}" ),
47
48
HTTP_ACCEPT = "application/json;q=0.8, text/html;q=0.9" ,
48
49
)
49
- assert response .status_code == 200
50
+ assert response .status_code == HTTPStatus . OK
50
51
assert response ["Content-Type" ].split (";" )[0 ] == "text/html"
51
52
52
53
@@ -55,15 +56,15 @@ def test_qfactor_json(client):
55
56
url_string (query = "{test}" ),
56
57
HTTP_ACCEPT = "text/html;q=0.8, application/json;q=0.9" ,
57
58
)
58
- assert response .status_code == 200
59
+ assert response .status_code == HTTPStatus . OK
59
60
assert response ["Content-Type" ].split (";" )[0 ] == "application/json"
60
61
assert response_json (response ) == {"data" : {"test" : "Hello World" }}
61
62
62
63
63
64
def test_allows_get_with_query_param (client ):
64
65
response = client .get (url_string (query = "{test}" ))
65
66
66
- assert response .status_code == 200
67
+ assert response .status_code == HTTPStatus . OK
67
68
assert response_json (response ) == {"data" : {"test" : "Hello World" }}
68
69
69
70
@@ -75,7 +76,7 @@ def test_allows_get_with_variable_values(client):
75
76
)
76
77
)
77
78
78
- assert response .status_code == 200
79
+ assert response .status_code == HTTPStatus . OK
79
80
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
80
81
81
82
@@ -94,7 +95,7 @@ def test_allows_get_with_operation_name(client):
94
95
)
95
96
)
96
97
97
- assert response .status_code == 200
98
+ assert response .status_code == HTTPStatus . OK
98
99
assert response_json (response ) == {
99
100
"data" : {"test" : "Hello World" , "shared" : "Hello Everyone" }
100
101
}
@@ -103,7 +104,7 @@ def test_allows_get_with_operation_name(client):
103
104
def test_reports_validation_errors (client ):
104
105
response = client .get (url_string (query = "{ test, unknownOne, unknownTwo }" ))
105
106
106
- assert response .status_code == 400
107
+ assert response .status_code == HTTPStatus . BAD_REQUEST
107
108
assert response_json (response ) == {
108
109
"errors" : [
109
110
{
@@ -128,7 +129,7 @@ def test_errors_when_missing_operation_name(client):
128
129
)
129
130
)
130
131
131
- assert response .status_code == 400
132
+ assert response .status_code == HTTPStatus . BAD_REQUEST
132
133
assert response_json (response ) == {
133
134
"errors" : [
134
135
{
@@ -146,7 +147,7 @@ def test_errors_when_sending_a_mutation_via_get(client):
146
147
"""
147
148
)
148
149
)
149
- assert response .status_code == 405
150
+ assert response .status_code == HTTPStatus . METHOD_NOT_ALLOWED
150
151
assert response_json (response ) == {
151
152
"errors" : [
152
153
{"message" : "Can only perform a mutation operation from a POST request." }
@@ -165,7 +166,7 @@ def test_errors_when_selecting_a_mutation_within_a_get(client):
165
166
)
166
167
)
167
168
168
- assert response .status_code == 405
169
+ assert response .status_code == HTTPStatus . METHOD_NOT_ALLOWED
169
170
assert response_json (response ) == {
170
171
"errors" : [
171
172
{"message" : "Can only perform a mutation operation from a POST request." }
@@ -184,14 +185,14 @@ def test_allows_mutation_to_exist_within_a_get(client):
184
185
)
185
186
)
186
187
187
- assert response .status_code == 200
188
+ assert response .status_code == HTTPStatus . OK
188
189
assert response_json (response ) == {"data" : {"test" : "Hello World" }}
189
190
190
191
191
192
def test_allows_post_with_json_encoding (client ):
192
193
response = client .post (url_string (), j (query = "{test}" ), "application/json" )
193
194
194
- assert response .status_code == 200
195
+ assert response .status_code == HTTPStatus . OK
195
196
assert response_json (response ) == {"data" : {"test" : "Hello World" }}
196
197
197
198
@@ -200,7 +201,7 @@ def test_batch_allows_post_with_json_encoding(client):
200
201
batch_url_string (), jl (id = 1 , query = "{test}" ), "application/json"
201
202
)
202
203
203
- assert response .status_code == 200
204
+ assert response .status_code == HTTPStatus . OK
204
205
assert response_json (response ) == [
205
206
{"id" : 1 , "data" : {"test" : "Hello World" }, "status" : 200 }
206
207
]
@@ -209,7 +210,7 @@ def test_batch_allows_post_with_json_encoding(client):
209
210
def test_batch_fails_if_is_empty (client ):
210
211
response = client .post (batch_url_string (), "[]" , "application/json" )
211
212
212
- assert response .status_code == 400
213
+ assert response .status_code == HTTPStatus . BAD_REQUEST
213
214
assert response_json (response ) == {
214
215
"errors" : [{"message" : "Received an empty list in the batch request." }]
215
216
}
@@ -222,7 +223,7 @@ def test_allows_sending_a_mutation_via_post(client):
222
223
"application/json" ,
223
224
)
224
225
225
- assert response .status_code == 200
226
+ assert response .status_code == HTTPStatus . OK
226
227
assert response_json (response ) == {"data" : {"writeTest" : {"test" : "Hello World" }}}
227
228
228
229
@@ -233,7 +234,7 @@ def test_allows_post_with_url_encoding(client):
233
234
"application/x-www-form-urlencoded" ,
234
235
)
235
236
236
- assert response .status_code == 200
237
+ assert response .status_code == HTTPStatus . OK
237
238
assert response_json (response ) == {"data" : {"test" : "Hello World" }}
238
239
239
240
@@ -247,7 +248,7 @@ def test_supports_post_json_query_with_string_variables(client):
247
248
"application/json" ,
248
249
)
249
250
250
- assert response .status_code == 200
251
+ assert response .status_code == HTTPStatus . OK
251
252
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
252
253
253
254
@@ -262,7 +263,7 @@ def test_batch_supports_post_json_query_with_string_variables(client):
262
263
"application/json" ,
263
264
)
264
265
265
- assert response .status_code == 200
266
+ assert response .status_code == HTTPStatus . OK
266
267
assert response_json (response ) == [
267
268
{"id" : 1 , "data" : {"test" : "Hello Dolly" }, "status" : 200 }
268
269
]
@@ -278,7 +279,7 @@ def test_supports_post_json_query_with_json_variables(client):
278
279
"application/json" ,
279
280
)
280
281
281
- assert response .status_code == 200
282
+ assert response .status_code == HTTPStatus . OK
282
283
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
283
284
284
285
@@ -293,7 +294,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):
293
294
"application/json" ,
294
295
)
295
296
296
- assert response .status_code == 200
297
+ assert response .status_code == HTTPStatus . OK
297
298
assert response_json (response ) == [
298
299
{"id" : 1 , "data" : {"test" : "Hello Dolly" }, "status" : 200 }
299
300
]
@@ -311,7 +312,7 @@ def test_supports_post_url_encoded_query_with_string_variables(client):
311
312
"application/x-www-form-urlencoded" ,
312
313
)
313
314
314
- assert response .status_code == 200
315
+ assert response .status_code == HTTPStatus . OK
315
316
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
316
317
317
318
@@ -322,7 +323,7 @@ def test_supports_post_json_quey_with_get_variable_values(client):
322
323
"application/json" ,
323
324
)
324
325
325
- assert response .status_code == 200
326
+ assert response .status_code == HTTPStatus . OK
326
327
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
327
328
328
329
@@ -333,7 +334,7 @@ def test_post_url_encoded_query_with_get_variable_values(client):
333
334
"application/x-www-form-urlencoded" ,
334
335
)
335
336
336
- assert response .status_code == 200
337
+ assert response .status_code == HTTPStatus . OK
337
338
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
338
339
339
340
@@ -344,7 +345,7 @@ def test_supports_post_raw_text_query_with_get_variable_values(client):
344
345
"application/graphql" ,
345
346
)
346
347
347
- assert response .status_code == 200
348
+ assert response .status_code == HTTPStatus . OK
348
349
assert response_json (response ) == {"data" : {"test" : "Hello Dolly" }}
349
350
350
351
@@ -365,7 +366,7 @@ def test_allows_post_with_operation_name(client):
365
366
"application/json" ,
366
367
)
367
368
368
- assert response .status_code == 200
369
+ assert response .status_code == HTTPStatus . OK
369
370
assert response_json (response ) == {
370
371
"data" : {"test" : "Hello World" , "shared" : "Hello Everyone" }
371
372
}
@@ -389,7 +390,7 @@ def test_batch_allows_post_with_operation_name(client):
389
390
"application/json" ,
390
391
)
391
392
392
- assert response .status_code == 200
393
+ assert response .status_code == HTTPStatus . OK
393
394
assert response_json (response ) == [
394
395
{
395
396
"id" : 1 ,
@@ -413,7 +414,7 @@ def test_allows_post_with_get_operation_name(client):
413
414
"application/graphql" ,
414
415
)
415
416
416
- assert response .status_code == 200
417
+ assert response .status_code == HTTPStatus . OK
417
418
assert response_json (response ) == {
418
419
"data" : {"test" : "Hello World" , "shared" : "Hello Everyone" }
419
420
}
@@ -430,7 +431,7 @@ def test_inherited_class_with_attributes_works(client):
430
431
431
432
# Check graphiql works
432
433
response = client .get (url_string (inherited_url ), HTTP_ACCEPT = "text/html" )
433
- assert response .status_code == 200
434
+ assert response .status_code == HTTPStatus . OK
434
435
435
436
436
437
@pytest .mark .urls ("graphene_django.tests.urls_pretty" )
@@ -452,7 +453,7 @@ def test_supports_pretty_printing_by_request(client):
452
453
453
454
def test_handles_field_errors_caught_by_graphql (client ):
454
455
response = client .get (url_string (query = "{thrower}" ))
455
- assert response .status_code == 200
456
+ assert response .status_code == HTTPStatus . OK
456
457
assert response_json (response ) == {
457
458
"data" : None ,
458
459
"errors" : [
@@ -467,7 +468,7 @@ def test_handles_field_errors_caught_by_graphql(client):
467
468
468
469
def test_handles_syntax_errors_caught_by_graphql (client ):
469
470
response = client .get (url_string (query = "syntaxerror" ))
470
- assert response .status_code == 400
471
+ assert response .status_code == HTTPStatus . BAD_REQUEST
471
472
assert response_json (response ) == {
472
473
"errors" : [
473
474
{
@@ -481,7 +482,7 @@ def test_handles_syntax_errors_caught_by_graphql(client):
481
482
def test_handles_errors_caused_by_a_lack_of_query (client ):
482
483
response = client .get (url_string ())
483
484
484
- assert response .status_code == 400
485
+ assert response .status_code == HTTPStatus . BAD_REQUEST
485
486
assert response_json (response ) == {
486
487
"errors" : [{"message" : "Must provide query string." }]
487
488
}
@@ -490,7 +491,7 @@ def test_handles_errors_caused_by_a_lack_of_query(client):
490
491
def test_handles_not_expected_json_bodies (client ):
491
492
response = client .post (url_string (), "[]" , "application/json" )
492
493
493
- assert response .status_code == 400
494
+ assert response .status_code == HTTPStatus . BAD_REQUEST
494
495
assert response_json (response ) == {
495
496
"errors" : [{"message" : "The received data is not a valid JSON query." }]
496
497
}
@@ -499,7 +500,7 @@ def test_handles_not_expected_json_bodies(client):
499
500
def test_handles_invalid_json_bodies (client ):
500
501
response = client .post (url_string (), "[oh}" , "application/json" )
501
502
502
- assert response .status_code == 400
503
+ assert response .status_code == HTTPStatus . BAD_REQUEST
503
504
assert response_json (response ) == {
504
505
"errors" : [{"message" : "POST body sent invalid JSON." }]
505
506
}
@@ -514,14 +515,14 @@ def mocked_read(*args):
514
515
valid_json = json .dumps ({"foo" : "bar" })
515
516
response = client .post (url_string (), valid_json , "application/json" )
516
517
517
- assert response .status_code == 400
518
+ assert response .status_code == HTTPStatus . BAD_REQUEST
518
519
assert response_json (response ) == {"errors" : [{"message" : "foo-bar" }]}
519
520
520
521
521
522
def test_handles_incomplete_json_bodies (client ):
522
523
response = client .post (url_string (), '{"query":' , "application/json" )
523
524
524
- assert response .status_code == 400
525
+ assert response .status_code == HTTPStatus . BAD_REQUEST
525
526
assert response_json (response ) == {
526
527
"errors" : [{"message" : "POST body sent invalid JSON." }]
527
528
}
@@ -533,7 +534,7 @@ def test_handles_plain_post_text(client):
533
534
"query helloWho($who: String){ test(who: $who) }" ,
534
535
"text/plain" ,
535
536
)
536
- assert response .status_code == 400
537
+ assert response .status_code == HTTPStatus . BAD_REQUEST
537
538
assert response_json (response ) == {
538
539
"errors" : [{"message" : "Must provide query string." }]
539
540
}
@@ -545,15 +546,15 @@ def test_handles_poorly_formed_variables(client):
545
546
query = "query helloWho($who: String){ test(who: $who) }" , variables = "who:You"
546
547
)
547
548
)
548
- assert response .status_code == 400
549
+ assert response .status_code == HTTPStatus . BAD_REQUEST
549
550
assert response_json (response ) == {
550
551
"errors" : [{"message" : "Variables are invalid JSON." }]
551
552
}
552
553
553
554
554
555
def test_handles_unsupported_http_methods (client ):
555
556
response = client .put (url_string (query = "{test}" ))
556
- assert response .status_code == 405
557
+ assert response .status_code == HTTPStatus . METHOD_NOT_ALLOWED
557
558
assert response ["Allow" ] == "GET, POST"
558
559
assert response_json (response ) == {
559
560
"errors" : [{"message" : "GraphQL only supports GET and POST requests." }]
@@ -563,7 +564,7 @@ def test_handles_unsupported_http_methods(client):
563
564
def test_passes_request_into_context_request (client ):
564
565
response = client .get (url_string (query = "{request}" , q = "testing" ))
565
566
566
- assert response .status_code == 200
567
+ assert response .status_code == HTTPStatus . OK
567
568
assert response_json (response ) == {"data" : {"request" : "testing" }}
568
569
569
570
@@ -857,7 +858,7 @@ def test_allow_introspection(client):
857
858
response = client .post (
858
859
url_string ("/graphql/" , query = "{__schema {queryType {name}}}" )
859
860
)
860
- assert response .status_code == 200
861
+ assert response .status_code == HTTPStatus . OK
861
862
862
863
assert response_json (response ) == {
863
864
"data" : {"__schema" : {"queryType" : {"name" : "QueryRoot" }}}
@@ -869,7 +870,7 @@ def test_allow_introspection(client):
869
870
def test_validation_disallow_introspection (client , url ):
870
871
response = client .post (url_string (url , query = "{__schema {queryType {name}}}" ))
871
872
872
- assert response .status_code == 400
873
+ assert response .status_code == HTTPStatus . BAD_REQUEST
873
874
874
875
json_response = response_json (response )
875
876
assert "data" not in json_response
@@ -888,7 +889,7 @@ def test_validation_disallow_introspection(client, url):
888
889
def test_within_max_validation_errors (client , url ):
889
890
response = client .post (url_string (url , query = QUERY_WITH_TWO_INTROSPECTIONS ))
890
891
891
- assert response .status_code == 400
892
+ assert response .status_code == HTTPStatus . BAD_REQUEST
892
893
893
894
json_response = response_json (response )
894
895
assert "data" not in json_response
@@ -913,7 +914,7 @@ def test_within_max_validation_errors(client, url):
913
914
def test_exceeds_max_validation_errors (client , url ):
914
915
response = client .post (url_string (url , query = QUERY_WITH_TWO_INTROSPECTIONS ))
915
916
916
- assert response .status_code == 400
917
+ assert response .status_code == HTTPStatus . BAD_REQUEST
917
918
918
919
json_response = response_json (response )
919
920
assert "data" not in json_response
0 commit comments