Skip to content

Commit d9a2393

Browse files
committed
Extend the usability of get_app fixture
1 parent e88d130 commit d9a2393

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

tests/conftest.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from fastapi import Request
1010
from social_core.backends.github import GithubOAuth2
1111
from social_core.backends.oauth import BaseOAuth2
12-
from starlette.responses import RedirectResponse
1312
from starlette.responses import Response
1413

1514
from fastapi_oauth2.client import OAuth2Client
1615
from fastapi_oauth2.middleware import OAuth2Middleware
16+
from fastapi_oauth2.router import router as oauth2_router
1717
from fastapi_oauth2.security import OAuth2
1818
from tests.idp import TestOAuth2
1919
from tests.idp import get_idp
@@ -41,28 +41,18 @@ def backends():
4141

4242
@pytest.fixture
4343
def get_app():
44-
def fixture_wrapper(authentication: OAuth2 = None):
44+
def fixture_wrapper(
45+
authentication: OAuth2 = None, # type of security
46+
with_idp=False, # used to test oauth2 flow
47+
with_ssr=True, # used to test oauth2 flow
48+
):
4549
if not authentication:
4650
authentication = OAuth2()
4751

4852
oauth2 = authentication
4953
application = FastAPI()
5054
app_router = APIRouter()
5155

52-
@app_router.get("/oauth2/{provider}/auth")
53-
async def login(request: Request, provider: str):
54-
return await request.auth.clients[provider].login_redirect(request)
55-
56-
@app_router.get("/oauth2/{provider}/token")
57-
async def token(request: Request, provider: str):
58-
return await request.auth.clients[provider].token_redirect(request, app=get_idp())
59-
60-
@app_router.get("/oauth2/logout")
61-
def logout(request: Request):
62-
response = RedirectResponse(request.base_url)
63-
response.delete_cookie("Authorization")
64-
return response
65-
6656
@app_router.get("/user")
6757
def user(request: Request, _: str = Depends(oauth2)):
6858
return request.user
@@ -88,9 +78,21 @@ def auth(request: Request):
8878
)
8979
return response
9080

81+
if with_idp:
82+
@app_router.get("/oauth2/{provider}/token")
83+
async def token(request: Request, provider: str):
84+
if request.auth.ssr:
85+
return await request.auth.clients[provider].token_redirect(request, app=get_idp())
86+
return await request.auth.clients[provider].token_data(request)
87+
9188
application.include_router(app_router)
92-
application.mount("", get_idp())
89+
application.include_router(oauth2_router)
90+
91+
if with_idp:
92+
application.mount("", get_idp())
93+
9394
application.add_middleware(OAuth2Middleware, config={
95+
"enable_ssr": with_ssr,
9496
"allow_http": True,
9597
"clients": [
9698
OAuth2Client(

0 commit comments

Comments
 (0)