Skip to content

Commit 9143f34

Browse files
committed
Configure a client for TestOAuth2 and connect IDP
1 parent 6a515cf commit 9143f34

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

tests/conftest.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
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
1213
from starlette.responses import Response
1314

1415
from fastapi_oauth2.client import OAuth2Client
1516
from fastapi_oauth2.middleware import OAuth2Middleware
16-
from fastapi_oauth2.router import router as oauth2_router
1717
from fastapi_oauth2.security import OAuth2
18+
from tests.idp import TestOAuth2
19+
from tests.idp import get_idp
1820

1921
package_path = backends.__path__[0]
2022

@@ -47,6 +49,20 @@ def fixture_wrapper(authentication: OAuth2 = None):
4749
application = FastAPI()
4850
app_router = APIRouter()
4951

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+
5066
@app_router.get("/user")
5167
def user(request: Request, _: str = Depends(oauth2)):
5268
return request.user
@@ -73,10 +89,15 @@ def auth(request: Request):
7389
return response
7490

7591
application.include_router(app_router)
76-
application.include_router(oauth2_router)
92+
application.mount("", get_idp())
7793
application.add_middleware(OAuth2Middleware, config={
7894
"allow_http": True,
7995
"clients": [
96+
OAuth2Client(
97+
backend=TestOAuth2,
98+
client_id="test_id",
99+
client_secret="test_secret",
100+
),
80101
OAuth2Client(
81102
backend=GithubOAuth2,
82103
client_id="test_id",

0 commit comments

Comments
 (0)