1- import importlib
2- import os
3-
41import pytest
5- import social_core .backends as backends
6- from fastapi import APIRouter
72from fastapi import FastAPI
83from httpx import AsyncClient
94from social_core .backends .github import GithubOAuth2
10- from social_core .backends .oauth import BaseOAuth2
11- from starlette .requests import Request
125
136from fastapi_oauth2 .client import OAuth2Client
14- from fastapi_oauth2 .config import OAuth2Config
15- from fastapi_oauth2 .middleware import OAuth2Backend
7+ from fastapi_oauth2 .core import OAuth2Core
168from fastapi_oauth2 .middleware import OAuth2Middleware
179from fastapi_oauth2 .router import router as oauth2_router
1810
1911app = FastAPI ()
20- router = APIRouter ()
21-
22-
23- @router .get ("/test_backends" )
24- async def _backends (request : Request ):
25- responses = []
26- for module in os .listdir (backends .__path__ [0 ]):
27- try :
28- module_instance = importlib .import_module ("social_core.backends.%s" % module [:- 3 ])
29- backend_implementations = [
30- attr for attr in module_instance .__dict__ .values ()
31- if type (attr ) is type and all ([
32- issubclass (attr , BaseOAuth2 ),
33- attr is not BaseOAuth2 ,
34- ])
35- ]
36- for backend_cls in backend_implementations :
37- backend = OAuth2Backend (OAuth2Config (
38- clients = [
39- OAuth2Client (
40- backend = backend_cls ,
41- client_id = "test_client_id" ,
42- client_secret = "test_client_secret" ,
43- )
44- ]
45- ))
46- responses .append (await backend .authenticate (request ))
47- except ImportError :
48- continue
49- return responses
5012
51-
52- app .include_router (router )
5313app .include_router (oauth2_router )
5414app .add_middleware (OAuth2Middleware , config = {
5515 "allow_http" : True ,
@@ -71,6 +31,13 @@ async def test_auth_redirect():
7131
7232
7333@pytest .mark .anyio
74- async def test_backends ():
75- async with AsyncClient (app = app , base_url = "http://test" ) as client :
76- assert all ((await client .get ("/test_backends" )).json ())
34+ async def test_core_init (backends ):
35+ for backend in backends :
36+ try :
37+ OAuth2Core (OAuth2Client (
38+ backend = backend ,
39+ client_id = "test_client_id" ,
40+ client_secret = "test_client_secret" ,
41+ ))
42+ except (NotImplementedError , Exception ):
43+ assert False
0 commit comments