Skip to content

Commit cb8b445

Browse files
committed
GH-42: Covering jwt cases and modify others
I added a case of exception that should be handled: as auth errors: jwt validation errors Using same approach as expiration check, but maybe it should be a HttpException in order to be properly handled both by fastapi and users. Also unexpected errors are not transformed to 500 either on the test setup. Maybe because we are not using Fastapi TestClient.
1 parent 1cef8b8 commit cb8b445

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

tests/test_middleware.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22
from httpx import AsyncClient
3+
from fastapi.responses import JSONResponse
4+
from fastapi_oauth2.exceptions import OAuth2AuthenticationError
35

46

57
@pytest.mark.anyio
@@ -31,40 +33,54 @@ async def test_middleware_on_logout(get_app):
3133
async def test_middleware_do_not_interfer_user_errors(get_app):
3234
app=get_app()
3335
@app.get('/unexpected_error')
34-
def unexpected():
36+
def my_entry_point():
3537
undefined_id # Intended code error
3638

3739
async with AsyncClient(app=app, base_url="http://test") as client:
38-
response = await client.get("/unexpected_error")
39-
assert response.status_code == 500 # Internal server error
40+
with pytest.raises(NameError):
41+
await client.get("/unexpected_error")
4042

4143
@pytest.mark.anyio
4244
async def test_middleware_ignores_custom_exceptions(get_app):
4345
class MyCustomException(Exception): pass
4446
app=get_app()
4547
@app.get('/custom_exception')
46-
def custom_exception():
48+
def my_entry_point():
4749
raise MyCustomException()
4850

4951
async with AsyncClient(app=app, base_url="http://test") as client:
50-
response = await client.get("/custom_exception")
51-
assert response.status_code == 500 # Internal server error
52+
with pytest.raises(MyCustomException):
53+
await client.get("/custom_exception")
5254

5355
@pytest.mark.anyio
5456
async def test_middleware_ignores_handled_custom_exceptions(get_app):
55-
class MyCustomException(Exception): pass
57+
class MyHandledException(Exception): pass
5658
app=get_app()
57-
@app.exception_handler(MyCustomException)
59+
@app.exception_handler(MyHandledException)
5860
async def unicorn_exception_handler(request, exc):
5961
return JSONResponse(
6062
status_code=418,
61-
content={"message": f"I am a Teapot!"},
63+
content={"details": "I am a custom Teapot!"},
6264
)
6365

64-
@app.get('/custom_exception')
65-
def custom_exception():
66-
raise MyCustomException()
66+
@app.get('/handled_exception')
67+
def my_entry_point():
68+
raise MyHandledException()
6769

6870
async with AsyncClient(app=app, base_url="http://test") as client:
69-
response = await client.get("/custom_exception")
71+
response = await client.get("/handled_exception")
7072
assert response.status_code == 418 # I am a teapot!
73+
assert response.json() == {"details": "I am a custom Teapot!"}
74+
75+
@pytest.mark.anyio
76+
async def test_middleware_reports_invalid_jwt(get_app):
77+
async with AsyncClient(app=get_app(with_ssr=False), base_url="http://test") as client:
78+
await client.get("/auth") # Simulate login
79+
# Insert a bad token instead
80+
from jose import jwt
81+
badtoken=jwt.encode({"bad": "token"}, 'badsecret', 'HS256')
82+
client.cookies.update(dict(Authorization=f"Bearer: {badtoken}"))
83+
84+
with pytest.raises(OAuth2AuthenticationError, match="401: Signature verification failed.") as ctx:
85+
response = await client.get("/user")
86+

0 commit comments

Comments
 (0)