Skip to content

Commit 0a540ae

Browse files
committed
added test for changing plain text error response to json response
1 parent 884ea4b commit 0a540ae

File tree

7 files changed

+123
-68
lines changed

7 files changed

+123
-68
lines changed

tests/test_application/test_application_functions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ async def homepage(request: Request):
174174
client = TestClient(app, raise_server_exceptions=False)
175175
response = client.get("/")
176176
assert response.status_code == 500
177-
assert "Internal Server Error" in response.text
177+
assert response.json() == {
178+
"detail": "Internal server error",
179+
"status_code": 500,
180+
}
178181
assert app.debug
179182

180183

@@ -199,7 +202,7 @@ def test_app_staticfiles_route(self, tmpdir):
199202

200203
response = client.post("/static/example.txt")
201204
assert response.status_code == 405
202-
assert response.text == "Method Not Allowed"
205+
assert response.json() == {"detail": "Method Not Allowed", "status_code": 405}
203206

204207
def test_app_staticfiles_with_different_static_path(self, tmpdir):
205208
path = os.path.join(tmpdir, "example.txt")

tests/test_exceptions/test_api_exception.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import http
2+
13
import pytest
24

35
from ellar.common import get
@@ -33,23 +35,31 @@ def exception_():
3335
@pytest.mark.parametrize(
3436
"exception, status_code, default_detail",
3537
[
36-
(APIException, APIException.status_code, APIException.default_detail),
38+
(
39+
APIException,
40+
APIException.status_code,
41+
http.HTTPStatus(APIException.status_code).phrase,
42+
),
3743
(
3844
AuthenticationFailed,
3945
AuthenticationFailed.status_code,
40-
AuthenticationFailed.default_detail,
46+
"Incorrect authentication credentials.",
47+
),
48+
(
49+
NotAcceptable,
50+
NotAcceptable.status_code,
51+
http.HTTPStatus(NotAcceptable.status_code).phrase,
4152
),
42-
(NotAcceptable, NotAcceptable.status_code, NotAcceptable.default_detail),
4353
(
4454
NotAuthenticated,
4555
NotAuthenticated.status_code,
46-
NotAuthenticated.default_detail,
56+
http.HTTPStatus(NotAuthenticated.status_code).phrase,
4757
),
48-
(NotFound, NotFound.status_code, NotFound.default_detail),
58+
(NotFound, NotFound.status_code, http.HTTPStatus(NotFound.status_code).phrase),
4959
(
5060
PermissionDenied,
5161
PermissionDenied.status_code,
52-
PermissionDenied.default_detail,
62+
http.HTTPStatus(PermissionDenied.status_code).phrase,
5363
),
5464
],
5565
)
@@ -64,21 +74,17 @@ def test_api_exception(exception, status_code, default_detail):
6474

6575
def test_unsupported_media_type_api_exception():
6676
global _exception_runner
67-
media_type = "application/json/sx"
68-
_exception_runner = ExceptionRunner(UnsupportedMediaType, media_type=media_type)
77+
_exception_runner = ExceptionRunner(UnsupportedMediaType)
6978
response = client.get("/exception")
7079
data = response.json()
7180
assert response.status_code == UnsupportedMediaType.status_code
72-
assert data["detail"] == UnsupportedMediaType.default_detail.format(
73-
media_type=media_type
74-
)
81+
assert data["detail"] == http.HTTPStatus(UnsupportedMediaType.status_code).phrase
7582

7683

7784
def test_method_not_allowed_api_exception():
7885
global _exception_runner
79-
method = "GET"
80-
_exception_runner = ExceptionRunner(MethodNotAllowed, method=method)
86+
_exception_runner = ExceptionRunner(MethodNotAllowed)
8187
response = client.get("/exception")
8288
data = response.json()
8389
assert response.status_code == MethodNotAllowed.status_code
84-
assert data["detail"] == MethodNotAllowed.default_detail.format(method=method)
90+
assert data["detail"] == http.HTTPStatus(MethodNotAllowed.status_code).phrase

tests/test_exceptions/test_custom_exceptions.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_invalid_handler_raise_exception():
8888
]
8989

9090

91-
def test_invalid_iexception_handler_setup_raise_exception():
91+
def test_invalid_exception_handler_setup_raise_exception():
9292
with pytest.raises(AssertionError) as ex:
9393

9494
class InvalidExceptionSetup(IExceptionHandler):
@@ -97,7 +97,33 @@ def catch(
9797
) -> t.Union[Response, t.Any]:
9898
pass
9999

100-
assert "exception_type_or_code must be defined" in str(ex.value)
100+
assert "'exception_type_or_code' must be defined" in str(ex.value)
101+
102+
103+
def test_invalid_exception_type_setup_raise_exception():
104+
with pytest.raises(AssertionError) as ex:
105+
106+
class InvalidExceptionSetup(IExceptionHandler):
107+
exception_type_or_code = ""
108+
109+
def catch(
110+
self, ctx: IExecutionContext, exc: t.Any
111+
) -> t.Union[Response, t.Any]:
112+
pass
113+
114+
assert "'exception_type_or_code' must be defined" in str(ex.value)
115+
116+
with pytest.raises(AssertionError) as ex:
117+
118+
class InvalidExceptionSetup2(IExceptionHandler):
119+
exception_type_or_code = InvalidExceptionHandler
120+
121+
def catch(
122+
self, ctx: IExecutionContext, exc: t.Any
123+
) -> t.Union[Response, t.Any]:
124+
pass
125+
126+
assert "'exception_type_or_code' is not a valid type" in str(ex.value)
101127

102128

103129
def test_custom_exception_works():
@@ -167,7 +193,7 @@ def homepage():
167193
client = tm.get_client()
168194
res = client.get("/")
169195
assert res.status_code == 400
170-
assert res.text == "Bad Request"
196+
assert res.json() == {"detail": "Bad Request", "status_code": 400}
171197

172198

173199
@pytest.mark.parametrize("status_code", [204, 304])
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
from ellar.core.exceptions.base import ErrorDetail
2-
3-
4-
class TestErrorDetail:
5-
def test_eq(self):
6-
assert ErrorDetail("msg") == ErrorDetail("msg")
7-
assert ErrorDetail("msg", "code") == ErrorDetail("msg", code="code")
8-
9-
assert ErrorDetail("msg") == "msg"
10-
assert ErrorDetail("msg", "code") == "msg"
11-
12-
def test_ne(self):
13-
assert ErrorDetail("msg1") != ErrorDetail("msg2")
14-
assert ErrorDetail("msg") != ErrorDetail("msg", code="invalid")
15-
16-
assert ErrorDetail("msg1") != "msg2"
17-
assert ErrorDetail("msg1", "code") != "msg2"
18-
19-
def test_repr(self):
20-
assert repr(
21-
ErrorDetail("msg1")
22-
) == "ErrorDetail(string={!r}, code=None)".format("msg1")
23-
assert repr(
24-
ErrorDetail("msg1", "code")
25-
) == "ErrorDetail(string={!r}, code={!r})".format("msg1", "code")
26-
27-
def test_str(self):
28-
assert str(ErrorDetail("msg1")) == "msg1"
29-
assert str(ErrorDetail("msg1", "code")) == "msg1"
30-
31-
def test_hash(self):
32-
assert hash(ErrorDetail("msg")) == hash("msg")
33-
assert hash(ErrorDetail("msg", "code")) == hash("msg")
1+
# from ellar.core.exceptions.base import ErrorDetail
2+
#
3+
#
4+
# class TestErrorDetail:
5+
# def test_eq(self):
6+
# assert ErrorDetail("msg") == ErrorDetail("msg")
7+
# assert ErrorDetail("msg", "code") == ErrorDetail("msg", code="code")
8+
#
9+
# assert ErrorDetail("msg") == "msg"
10+
# assert ErrorDetail("msg", "code") == "msg"
11+
#
12+
# def test_ne(self):
13+
# assert ErrorDetail("msg1") != ErrorDetail("msg2")
14+
# assert ErrorDetail("msg") != ErrorDetail("msg", code="invalid")
15+
#
16+
# assert ErrorDetail("msg1") != "msg2"
17+
# assert ErrorDetail("msg1", "code") != "msg2"
18+
#
19+
# def test_repr(self):
20+
# assert repr(
21+
# ErrorDetail("msg1")
22+
# ) == "ErrorDetail(string={!r}, code=None)".format("msg1")
23+
# assert repr(
24+
# ErrorDetail("msg1", "code")
25+
# ) == "ErrorDetail(string={!r}, code={!r})".format("msg1", "code")
26+
#
27+
# def test_str(self):
28+
# assert str(ErrorDetail("msg1")) == "msg1"
29+
# assert str(ErrorDetail("msg1", "code")) == "msg1"
30+
#
31+
# def test_hash(self):
32+
# assert hash(ErrorDetail("msg")) == hash("msg")
33+
# assert hash(ErrorDetail("msg", "code")) == hash("msg")

tests/test_openapi/test_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ def test_openapi_module_with_route_guards():
106106

107107
response = client.get("openapi.json")
108108
assert response.status_code == 403
109-
assert response.text == "Not Allowed"
109+
assert response.json() == {"detail": "Not Allowed", "status_code": 403}
110110

111111
response = client.get("docs")
112112
assert response.status_code == 403
113-
assert response.text == "Not Allowed"
113+
assert response.json() == {"detail": "Not Allowed", "status_code": 403}
114114

115115
response = client.get("redoc")
116116
assert response.status_code == 403
117-
assert response.text == "Not Allowed"
117+
assert response.json() == {"detail": "Not Allowed", "status_code": 403}

tests/test_routing/test_path.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,32 @@ def test_get_path(path, expected_status, expected_response):
256256
("/path/param-starlette-str/foobar", 200, "foobar"),
257257
("/path/param-starlette-int/0", 200, 0),
258258
("/path/param-starlette-int/42", 200, 42),
259-
("/path/param-starlette-int/42.5", 404, b"Not Found"),
260-
("/path/param-starlette-int/-1", 404, b"Not Found"),
261-
("/path/param-starlette-int/True", 404, b"Not Found"),
262-
("/path/param-starlette-int/foobar", 404, b"Not Found"),
259+
(
260+
"/path/param-starlette-int/42.5",
261+
404,
262+
{"detail": "Not Found", "status_code": 404},
263+
),
264+
(
265+
"/path/param-starlette-int/-1",
266+
404,
267+
{"detail": "Not Found", "status_code": 404},
268+
),
269+
(
270+
"/path/param-starlette-int/True",
271+
404,
272+
{"detail": "Not Found", "status_code": 404},
273+
),
274+
(
275+
"/path/param-starlette-int/foobar",
276+
404,
277+
{"detail": "Not Found", "status_code": 404},
278+
),
263279
("/path/param-starlette-int-str/42", 200, "42"),
264-
("/path/param-starlette-int-str/42.5", 404, b"Not Found"),
280+
(
281+
"/path/param-starlette-int-str/42.5",
282+
404,
283+
{"detail": "Not Found", "status_code": 404},
284+
),
265285
(
266286
"/path/param-starlette-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2a",
267287
200,
@@ -270,7 +290,7 @@ def test_get_path(path, expected_status, expected_response):
270290
(
271291
"/path/param-starlette-uuid/31ea378c-c052-4b4c-bf0b-679ce5cfcc2",
272292
404,
273-
b"Not Found",
293+
{"detail": "Not Found", "status_code": 404},
274294
),
275295
],
276296
)

tests/test_staticfiles.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_staticfiles_post(tmpdir):
7979

8080
response = client.post("/example.txt")
8181
assert response.status_code == 405
82-
assert response.text == "Method Not Allowed"
82+
assert response.json() == {"detail": "Method Not Allowed", "status_code": 405}
8383

8484

8585
def test_staticfiles_with_directory_returns_404(tmpdir):
@@ -93,7 +93,7 @@ def test_staticfiles_with_directory_returns_404(tmpdir):
9393

9494
response = client.get("/")
9595
assert response.status_code == 404
96-
assert response.text == "Not Found"
96+
assert response.json() == {"detail": "Not Found", "status_code": 404}
9797

9898

9999
def test_staticfiles_with_missing_file_returns_404(tmpdir):
@@ -107,7 +107,7 @@ def test_staticfiles_with_missing_file_returns_404(tmpdir):
107107

108108
response = client.get("/404.txt")
109109
assert response.status_code == 404
110-
assert response.text == "Not Found"
110+
assert response.json() == {"detail": "Not Found", "status_code": 404}
111111

112112

113113
def test_staticfiles_instantiated_with_missing_directory(tmpdir):
@@ -375,7 +375,7 @@ def test_staticfiles_with_invalid_dir_permissions_returns_401(tmpdir):
375375

376376
response = client.get("/example.txt")
377377
assert response.status_code == 401
378-
assert response.text == "Unauthorized"
378+
assert response.json() == {"detail": "Unauthorized", "status_code": 401}
379379

380380

381381
def test_staticfiles_with_missing_dir_returns_404(tmpdir):
@@ -389,7 +389,7 @@ def test_staticfiles_with_missing_dir_returns_404(tmpdir):
389389

390390
response = client.get("/foo/example.txt")
391391
assert response.status_code == 404
392-
assert response.text == "Not Found"
392+
assert response.json() == {"detail": "Not Found", "status_code": 404}
393393

394394

395395
def test_staticfiles_access_file_as_dir_returns_404(tmpdir):
@@ -403,7 +403,7 @@ def test_staticfiles_access_file_as_dir_returns_404(tmpdir):
403403

404404
response = client.get("/example.txt/foo")
405405
assert response.status_code == 404
406-
assert response.text == "Not Found"
406+
assert response.json() == {"detail": "Not Found", "status_code": 404}
407407

408408

409409
def test_staticfiles_unhandled_os_error_returns_500(tmpdir, monkeypatch):
@@ -422,4 +422,4 @@ def mock_timeout(*args, **kwargs):
422422

423423
response = client.get("/example.txt")
424424
assert response.status_code == 500
425-
assert response.text == "Internal Server Error"
425+
assert response.json() == {"detail": "Internal server error", "status_code": 500}

0 commit comments

Comments
 (0)