Skip to content

Commit 0041465

Browse files
committed
Improve code formatting
1 parent 8c93004 commit 0041465

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

tests/test_middleware.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
from httpx import AsyncClient
32
from fastapi.responses import JSONResponse
4-
from starlette.authentication import AuthenticationError
3+
from httpx import AsyncClient
4+
from jose import jwt
55

66

77
@pytest.mark.anyio
@@ -29,59 +29,68 @@ async def test_middleware_on_logout(get_app):
2929
response = await client.get("/user")
3030
assert response.status_code == 403 # Forbidden
3131

32+
3233
@pytest.mark.anyio
33-
async def test_middleware_do_not_interfer_user_errors(get_app):
34-
app=get_app()
35-
@app.get('/unexpected_error')
34+
async def test_middleware_do_not_interfere_user_errors(get_app):
35+
app = get_app()
36+
37+
@app.get("/unexpected_error")
3638
def my_entry_point():
37-
undefined_id # Intended code error
39+
raise NameError # Intended code error
3840

3941
async with AsyncClient(app=app, base_url="http://test") as client:
4042
with pytest.raises(NameError):
4143
await client.get("/unexpected_error")
4244

45+
4346
@pytest.mark.anyio
4447
async def test_middleware_ignores_custom_exceptions(get_app):
45-
class MyCustomException(Exception): pass
46-
app=get_app()
47-
@app.get('/custom_exception')
48+
class MyCustomException(Exception):
49+
pass
50+
51+
app = get_app()
52+
53+
@app.get("/custom_exception")
4854
def my_entry_point():
4955
raise MyCustomException()
5056

5157
async with AsyncClient(app=app, base_url="http://test") as client:
52-
with pytest.raises(MyCustomException):
58+
with pytest.raises(MyCustomException):
5359
await client.get("/custom_exception")
5460

61+
5562
@pytest.mark.anyio
5663
async def test_middleware_ignores_handled_custom_exceptions(get_app):
57-
class MyHandledException(Exception): pass
58-
app=get_app()
64+
class MyHandledException(Exception):
65+
pass
66+
67+
app = get_app()
68+
5969
@app.exception_handler(MyHandledException)
6070
async def unicorn_exception_handler(request, exc):
6171
return JSONResponse(
6272
status_code=418,
6373
content={"details": "I am a custom Teapot!"},
6474
)
6575

66-
@app.get('/handled_exception')
76+
@app.get("/handled_exception")
6777
def my_entry_point():
6878
raise MyHandledException()
6979

7080
async with AsyncClient(app=app, base_url="http://test") as client:
7181
response = await client.get("/handled_exception")
72-
assert response.status_code == 418 # I am a teapot!
82+
assert response.status_code == 418 # I am a teapot!
7383
assert response.json() == {"details": "I am a custom Teapot!"}
7484

85+
7586
@pytest.mark.anyio
7687
async def test_middleware_reports_invalid_jwt(get_app):
7788
async with AsyncClient(app=get_app(with_ssr=False), base_url="http://test") as client:
7889
await client.get("/auth") # Simulate login
7990
# Insert a bad token instead
80-
from jose import jwt
81-
badtoken=jwt.encode({"bad": "token"}, 'badsecret', 'HS256')
91+
badtoken = jwt.encode({"bad": "token"}, "badsecret", "HS256")
8292
client.cookies.update(dict(Authorization=f"Bearer: {badtoken}"))
8393

8494
response = await client.get("/user")
85-
assert response.status_code == 401 # Not authenticated
95+
assert response.status_code == 401 # Not authenticated
8696
assert response.text == "Signature verification failed."
87-

0 commit comments

Comments
 (0)