Skip to content

Commit ea45de0

Browse files
authored
Make use of http.HTTPStatus for response status code checks (#1487)
1 parent eac113e commit ea45de0

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

graphene_django/tests/test_views.py

+44-43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from http import HTTPStatus
23
from unittest.mock import patch
34

45
import pytest
@@ -37,7 +38,7 @@ def jl(**kwargs):
3738

3839
def test_graphiql_is_enabled(client):
3940
response = client.get(url_string(), HTTP_ACCEPT="text/html")
40-
assert response.status_code == 200
41+
assert response.status_code == HTTPStatus.OK
4142
assert response["Content-Type"].split(";")[0] == "text/html"
4243

4344

@@ -46,7 +47,7 @@ def test_qfactor_graphiql(client):
4647
url_string(query="{test}"),
4748
HTTP_ACCEPT="application/json;q=0.8, text/html;q=0.9",
4849
)
49-
assert response.status_code == 200
50+
assert response.status_code == HTTPStatus.OK
5051
assert response["Content-Type"].split(";")[0] == "text/html"
5152

5253

@@ -55,15 +56,15 @@ def test_qfactor_json(client):
5556
url_string(query="{test}"),
5657
HTTP_ACCEPT="text/html;q=0.8, application/json;q=0.9",
5758
)
58-
assert response.status_code == 200
59+
assert response.status_code == HTTPStatus.OK
5960
assert response["Content-Type"].split(";")[0] == "application/json"
6061
assert response_json(response) == {"data": {"test": "Hello World"}}
6162

6263

6364
def test_allows_get_with_query_param(client):
6465
response = client.get(url_string(query="{test}"))
6566

66-
assert response.status_code == 200
67+
assert response.status_code == HTTPStatus.OK
6768
assert response_json(response) == {"data": {"test": "Hello World"}}
6869

6970

@@ -75,7 +76,7 @@ def test_allows_get_with_variable_values(client):
7576
)
7677
)
7778

78-
assert response.status_code == 200
79+
assert response.status_code == HTTPStatus.OK
7980
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
8081

8182

@@ -94,7 +95,7 @@ def test_allows_get_with_operation_name(client):
9495
)
9596
)
9697

97-
assert response.status_code == 200
98+
assert response.status_code == HTTPStatus.OK
9899
assert response_json(response) == {
99100
"data": {"test": "Hello World", "shared": "Hello Everyone"}
100101
}
@@ -103,7 +104,7 @@ def test_allows_get_with_operation_name(client):
103104
def test_reports_validation_errors(client):
104105
response = client.get(url_string(query="{ test, unknownOne, unknownTwo }"))
105106

106-
assert response.status_code == 400
107+
assert response.status_code == HTTPStatus.BAD_REQUEST
107108
assert response_json(response) == {
108109
"errors": [
109110
{
@@ -128,7 +129,7 @@ def test_errors_when_missing_operation_name(client):
128129
)
129130
)
130131

131-
assert response.status_code == 400
132+
assert response.status_code == HTTPStatus.BAD_REQUEST
132133
assert response_json(response) == {
133134
"errors": [
134135
{
@@ -146,7 +147,7 @@ def test_errors_when_sending_a_mutation_via_get(client):
146147
"""
147148
)
148149
)
149-
assert response.status_code == 405
150+
assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
150151
assert response_json(response) == {
151152
"errors": [
152153
{"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):
165166
)
166167
)
167168

168-
assert response.status_code == 405
169+
assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
169170
assert response_json(response) == {
170171
"errors": [
171172
{"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):
184185
)
185186
)
186187

187-
assert response.status_code == 200
188+
assert response.status_code == HTTPStatus.OK
188189
assert response_json(response) == {"data": {"test": "Hello World"}}
189190

190191

191192
def test_allows_post_with_json_encoding(client):
192193
response = client.post(url_string(), j(query="{test}"), "application/json")
193194

194-
assert response.status_code == 200
195+
assert response.status_code == HTTPStatus.OK
195196
assert response_json(response) == {"data": {"test": "Hello World"}}
196197

197198

@@ -200,7 +201,7 @@ def test_batch_allows_post_with_json_encoding(client):
200201
batch_url_string(), jl(id=1, query="{test}"), "application/json"
201202
)
202203

203-
assert response.status_code == 200
204+
assert response.status_code == HTTPStatus.OK
204205
assert response_json(response) == [
205206
{"id": 1, "data": {"test": "Hello World"}, "status": 200}
206207
]
@@ -209,7 +210,7 @@ def test_batch_allows_post_with_json_encoding(client):
209210
def test_batch_fails_if_is_empty(client):
210211
response = client.post(batch_url_string(), "[]", "application/json")
211212

212-
assert response.status_code == 400
213+
assert response.status_code == HTTPStatus.BAD_REQUEST
213214
assert response_json(response) == {
214215
"errors": [{"message": "Received an empty list in the batch request."}]
215216
}
@@ -222,7 +223,7 @@ def test_allows_sending_a_mutation_via_post(client):
222223
"application/json",
223224
)
224225

225-
assert response.status_code == 200
226+
assert response.status_code == HTTPStatus.OK
226227
assert response_json(response) == {"data": {"writeTest": {"test": "Hello World"}}}
227228

228229

@@ -233,7 +234,7 @@ def test_allows_post_with_url_encoding(client):
233234
"application/x-www-form-urlencoded",
234235
)
235236

236-
assert response.status_code == 200
237+
assert response.status_code == HTTPStatus.OK
237238
assert response_json(response) == {"data": {"test": "Hello World"}}
238239

239240

@@ -247,7 +248,7 @@ def test_supports_post_json_query_with_string_variables(client):
247248
"application/json",
248249
)
249250

250-
assert response.status_code == 200
251+
assert response.status_code == HTTPStatus.OK
251252
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
252253

253254

@@ -262,7 +263,7 @@ def test_batch_supports_post_json_query_with_string_variables(client):
262263
"application/json",
263264
)
264265

265-
assert response.status_code == 200
266+
assert response.status_code == HTTPStatus.OK
266267
assert response_json(response) == [
267268
{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200}
268269
]
@@ -278,7 +279,7 @@ def test_supports_post_json_query_with_json_variables(client):
278279
"application/json",
279280
)
280281

281-
assert response.status_code == 200
282+
assert response.status_code == HTTPStatus.OK
282283
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
283284

284285

@@ -293,7 +294,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):
293294
"application/json",
294295
)
295296

296-
assert response.status_code == 200
297+
assert response.status_code == HTTPStatus.OK
297298
assert response_json(response) == [
298299
{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200}
299300
]
@@ -311,7 +312,7 @@ def test_supports_post_url_encoded_query_with_string_variables(client):
311312
"application/x-www-form-urlencoded",
312313
)
313314

314-
assert response.status_code == 200
315+
assert response.status_code == HTTPStatus.OK
315316
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
316317

317318

@@ -322,7 +323,7 @@ def test_supports_post_json_quey_with_get_variable_values(client):
322323
"application/json",
323324
)
324325

325-
assert response.status_code == 200
326+
assert response.status_code == HTTPStatus.OK
326327
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
327328

328329

@@ -333,7 +334,7 @@ def test_post_url_encoded_query_with_get_variable_values(client):
333334
"application/x-www-form-urlencoded",
334335
)
335336

336-
assert response.status_code == 200
337+
assert response.status_code == HTTPStatus.OK
337338
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
338339

339340

@@ -344,7 +345,7 @@ def test_supports_post_raw_text_query_with_get_variable_values(client):
344345
"application/graphql",
345346
)
346347

347-
assert response.status_code == 200
348+
assert response.status_code == HTTPStatus.OK
348349
assert response_json(response) == {"data": {"test": "Hello Dolly"}}
349350

350351

@@ -365,7 +366,7 @@ def test_allows_post_with_operation_name(client):
365366
"application/json",
366367
)
367368

368-
assert response.status_code == 200
369+
assert response.status_code == HTTPStatus.OK
369370
assert response_json(response) == {
370371
"data": {"test": "Hello World", "shared": "Hello Everyone"}
371372
}
@@ -389,7 +390,7 @@ def test_batch_allows_post_with_operation_name(client):
389390
"application/json",
390391
)
391392

392-
assert response.status_code == 200
393+
assert response.status_code == HTTPStatus.OK
393394
assert response_json(response) == [
394395
{
395396
"id": 1,
@@ -413,7 +414,7 @@ def test_allows_post_with_get_operation_name(client):
413414
"application/graphql",
414415
)
415416

416-
assert response.status_code == 200
417+
assert response.status_code == HTTPStatus.OK
417418
assert response_json(response) == {
418419
"data": {"test": "Hello World", "shared": "Hello Everyone"}
419420
}
@@ -430,7 +431,7 @@ def test_inherited_class_with_attributes_works(client):
430431

431432
# Check graphiql works
432433
response = client.get(url_string(inherited_url), HTTP_ACCEPT="text/html")
433-
assert response.status_code == 200
434+
assert response.status_code == HTTPStatus.OK
434435

435436

436437
@pytest.mark.urls("graphene_django.tests.urls_pretty")
@@ -452,7 +453,7 @@ def test_supports_pretty_printing_by_request(client):
452453

453454
def test_handles_field_errors_caught_by_graphql(client):
454455
response = client.get(url_string(query="{thrower}"))
455-
assert response.status_code == 200
456+
assert response.status_code == HTTPStatus.OK
456457
assert response_json(response) == {
457458
"data": None,
458459
"errors": [
@@ -467,7 +468,7 @@ def test_handles_field_errors_caught_by_graphql(client):
467468

468469
def test_handles_syntax_errors_caught_by_graphql(client):
469470
response = client.get(url_string(query="syntaxerror"))
470-
assert response.status_code == 400
471+
assert response.status_code == HTTPStatus.BAD_REQUEST
471472
assert response_json(response) == {
472473
"errors": [
473474
{
@@ -481,7 +482,7 @@ def test_handles_syntax_errors_caught_by_graphql(client):
481482
def test_handles_errors_caused_by_a_lack_of_query(client):
482483
response = client.get(url_string())
483484

484-
assert response.status_code == 400
485+
assert response.status_code == HTTPStatus.BAD_REQUEST
485486
assert response_json(response) == {
486487
"errors": [{"message": "Must provide query string."}]
487488
}
@@ -490,7 +491,7 @@ def test_handles_errors_caused_by_a_lack_of_query(client):
490491
def test_handles_not_expected_json_bodies(client):
491492
response = client.post(url_string(), "[]", "application/json")
492493

493-
assert response.status_code == 400
494+
assert response.status_code == HTTPStatus.BAD_REQUEST
494495
assert response_json(response) == {
495496
"errors": [{"message": "The received data is not a valid JSON query."}]
496497
}
@@ -499,7 +500,7 @@ def test_handles_not_expected_json_bodies(client):
499500
def test_handles_invalid_json_bodies(client):
500501
response = client.post(url_string(), "[oh}", "application/json")
501502

502-
assert response.status_code == 400
503+
assert response.status_code == HTTPStatus.BAD_REQUEST
503504
assert response_json(response) == {
504505
"errors": [{"message": "POST body sent invalid JSON."}]
505506
}
@@ -514,14 +515,14 @@ def mocked_read(*args):
514515
valid_json = json.dumps({"foo": "bar"})
515516
response = client.post(url_string(), valid_json, "application/json")
516517

517-
assert response.status_code == 400
518+
assert response.status_code == HTTPStatus.BAD_REQUEST
518519
assert response_json(response) == {"errors": [{"message": "foo-bar"}]}
519520

520521

521522
def test_handles_incomplete_json_bodies(client):
522523
response = client.post(url_string(), '{"query":', "application/json")
523524

524-
assert response.status_code == 400
525+
assert response.status_code == HTTPStatus.BAD_REQUEST
525526
assert response_json(response) == {
526527
"errors": [{"message": "POST body sent invalid JSON."}]
527528
}
@@ -533,7 +534,7 @@ def test_handles_plain_post_text(client):
533534
"query helloWho($who: String){ test(who: $who) }",
534535
"text/plain",
535536
)
536-
assert response.status_code == 400
537+
assert response.status_code == HTTPStatus.BAD_REQUEST
537538
assert response_json(response) == {
538539
"errors": [{"message": "Must provide query string."}]
539540
}
@@ -545,15 +546,15 @@ def test_handles_poorly_formed_variables(client):
545546
query="query helloWho($who: String){ test(who: $who) }", variables="who:You"
546547
)
547548
)
548-
assert response.status_code == 400
549+
assert response.status_code == HTTPStatus.BAD_REQUEST
549550
assert response_json(response) == {
550551
"errors": [{"message": "Variables are invalid JSON."}]
551552
}
552553

553554

554555
def test_handles_unsupported_http_methods(client):
555556
response = client.put(url_string(query="{test}"))
556-
assert response.status_code == 405
557+
assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
557558
assert response["Allow"] == "GET, POST"
558559
assert response_json(response) == {
559560
"errors": [{"message": "GraphQL only supports GET and POST requests."}]
@@ -563,7 +564,7 @@ def test_handles_unsupported_http_methods(client):
563564
def test_passes_request_into_context_request(client):
564565
response = client.get(url_string(query="{request}", q="testing"))
565566

566-
assert response.status_code == 200
567+
assert response.status_code == HTTPStatus.OK
567568
assert response_json(response) == {"data": {"request": "testing"}}
568569

569570

@@ -857,7 +858,7 @@ def test_allow_introspection(client):
857858
response = client.post(
858859
url_string("/graphql/", query="{__schema {queryType {name}}}")
859860
)
860-
assert response.status_code == 200
861+
assert response.status_code == HTTPStatus.OK
861862

862863
assert response_json(response) == {
863864
"data": {"__schema": {"queryType": {"name": "QueryRoot"}}}
@@ -869,7 +870,7 @@ def test_allow_introspection(client):
869870
def test_validation_disallow_introspection(client, url):
870871
response = client.post(url_string(url, query="{__schema {queryType {name}}}"))
871872

872-
assert response.status_code == 400
873+
assert response.status_code == HTTPStatus.BAD_REQUEST
873874

874875
json_response = response_json(response)
875876
assert "data" not in json_response
@@ -888,7 +889,7 @@ def test_validation_disallow_introspection(client, url):
888889
def test_within_max_validation_errors(client, url):
889890
response = client.post(url_string(url, query=QUERY_WITH_TWO_INTROSPECTIONS))
890891

891-
assert response.status_code == 400
892+
assert response.status_code == HTTPStatus.BAD_REQUEST
892893

893894
json_response = response_json(response)
894895
assert "data" not in json_response
@@ -913,7 +914,7 @@ def test_within_max_validation_errors(client, url):
913914
def test_exceeds_max_validation_errors(client, url):
914915
response = client.post(url_string(url, query=QUERY_WITH_TWO_INTROSPECTIONS))
915916

916-
assert response.status_code == 400
917+
assert response.status_code == HTTPStatus.BAD_REQUEST
917918

918919
json_response = response_json(response)
919920
assert "data" not in json_response

0 commit comments

Comments
 (0)