Skip to content

Commit 8330e6c

Browse files
committed
Make the app fixture callable with default oauth security engine
1 parent 8a2cf61 commit 8330e6c

File tree

3 files changed

+51
-45
lines changed

3 files changed

+51
-45
lines changed

tests/conftest.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,49 @@ def backends():
3838

3939

4040
@pytest.fixture
41-
def app():
42-
oauth2 = OAuth2()
43-
application = FastAPI()
44-
app_router = APIRouter()
41+
def get_app():
42+
def fixture_wrapper(authentication: OAuth2 = None):
43+
if not authentication:
44+
authentication = OAuth2()
4545

46-
@app_router.get("/user")
47-
def user(request: Request, _: str = Depends(oauth2)):
48-
return request.user
46+
oauth2 = authentication
47+
application = FastAPI()
48+
app_router = APIRouter()
4949

50-
@app_router.get("/auth")
51-
def auth(request: Request):
52-
access_token = request.auth.jwt_create({
53-
"name": "test",
54-
"sub": "test",
55-
"id": "test",
50+
@app_router.get("/user")
51+
def user(request: Request, _: str = Depends(oauth2)):
52+
return request.user
53+
54+
@app_router.get("/auth")
55+
def auth(request: Request):
56+
access_token = request.auth.jwt_create({
57+
"name": "test",
58+
"sub": "test",
59+
"id": "test",
60+
})
61+
response = Response()
62+
response.set_cookie(
63+
"Authorization",
64+
value=f"Bearer {access_token}",
65+
max_age=request.auth.expires,
66+
expires=request.auth.expires,
67+
httponly=request.auth.http,
68+
)
69+
return response
70+
71+
application.include_router(app_router)
72+
application.include_router(oauth2_router)
73+
application.add_middleware(OAuth2Middleware, config={
74+
"allow_http": True,
75+
"clients": [
76+
OAuth2Client(
77+
backend=GithubOAuth2,
78+
client_id="test_id",
79+
client_secret="test_secret",
80+
),
81+
],
5682
})
57-
response = Response()
58-
response.set_cookie(
59-
"Authorization",
60-
value=f"Bearer {access_token}",
61-
max_age=request.auth.expires,
62-
expires=request.auth.expires,
63-
httponly=request.auth.http,
64-
)
65-
return response
6683

67-
application.include_router(app_router)
68-
application.include_router(oauth2_router)
69-
application.add_middleware(OAuth2Middleware, config={
70-
"allow_http": True,
71-
"clients": [
72-
OAuth2Client(
73-
backend=GithubOAuth2,
74-
client_id="test_id",
75-
client_secret="test_secret",
76-
),
77-
],
78-
})
84+
return application
7985

80-
return application
86+
return fixture_wrapper

tests/test_middleware.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
@pytest.mark.anyio
6-
async def test_authentication(app):
7-
async with AsyncClient(app=app, base_url="http://test") as client:
6+
async def test_middleware_on_authentication(get_app):
7+
async with AsyncClient(app=get_app(), base_url="http://test") as client:
88
response = await client.get("/user")
99
assert response.status_code == 403 # Forbidden
1010

@@ -15,8 +15,8 @@ async def test_authentication(app):
1515

1616

1717
@pytest.mark.anyio
18-
async def test_logout(app):
19-
async with AsyncClient(app=app, base_url="http://test") as client:
18+
async def test_middleware_on_logout(get_app):
19+
async with AsyncClient(app=get_app(), base_url="http://test") as client:
2020
await client.get("/auth") # Simulate login
2121

2222
response = await client.get("/user")

tests/test_routes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44

55
@pytest.mark.anyio
6-
async def test_auth_redirect(app):
7-
async with AsyncClient(app=app, base_url="http://test") as client:
6+
async def test_auth_redirect(get_app):
7+
async with AsyncClient(app=get_app(), base_url="http://test") as client:
88
response = await client.get("/oauth2/github/auth")
99
assert response.status_code == 303 # Redirect
1010

1111

1212
@pytest.mark.anyio
13-
async def test_token_redirect(app):
14-
async with AsyncClient(app=app, base_url="http://test") as client:
13+
async def test_token_redirect(get_app):
14+
async with AsyncClient(app=get_app(), base_url="http://test") as client:
1515
response = await client.get("/oauth2/github/token")
1616
assert response.status_code == 400 # Bad Request
1717

@@ -20,7 +20,7 @@ async def test_token_redirect(app):
2020

2121

2222
@pytest.mark.anyio
23-
async def test_logout_redirect(app):
24-
async with AsyncClient(app=app, base_url="http://test") as client:
23+
async def test_logout_redirect(get_app):
24+
async with AsyncClient(app=get_app(), base_url="http://test") as client:
2525
response = await client.get("/oauth2/logout")
2626
assert response.status_code == 307 # Redirect

0 commit comments

Comments
 (0)