From f642d742c4acaa5dc02aa9fdf0d21be2a8c7ec36 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Thu, 13 Jul 2023 14:00:34 +0400 Subject: [PATCH] GH-5: Simplify the testing of clients with all backends --- tests/test_oauth2_middleware.py | 55 +++++++-------------------------- 1 file changed, 11 insertions(+), 44 deletions(-) diff --git a/tests/test_oauth2_middleware.py b/tests/test_oauth2_middleware.py index 406f0ac..4617b74 100644 --- a/tests/test_oauth2_middleware.py +++ b/tests/test_oauth2_middleware.py @@ -1,55 +1,15 @@ -import importlib -import os - import pytest -import social_core.backends as backends -from fastapi import APIRouter from fastapi import FastAPI from httpx import AsyncClient from social_core.backends.github import GithubOAuth2 -from social_core.backends.oauth import BaseOAuth2 -from starlette.requests import Request from fastapi_oauth2.client import OAuth2Client -from fastapi_oauth2.config import OAuth2Config -from fastapi_oauth2.middleware import OAuth2Backend +from fastapi_oauth2.core import OAuth2Core from fastapi_oauth2.middleware import OAuth2Middleware from fastapi_oauth2.router import router as oauth2_router app = FastAPI() -router = APIRouter() - - -@router.get("/test_backends") -async def _backends(request: Request): - responses = [] - for module in os.listdir(backends.__path__[0]): - try: - module_instance = importlib.import_module("social_core.backends.%s" % module[:-3]) - backend_implementations = [ - attr for attr in module_instance.__dict__.values() - if type(attr) is type and all([ - issubclass(attr, BaseOAuth2), - attr is not BaseOAuth2, - ]) - ] - for backend_cls in backend_implementations: - backend = OAuth2Backend(OAuth2Config( - clients=[ - OAuth2Client( - backend=backend_cls, - client_id="test_client_id", - client_secret="test_client_secret", - ) - ] - )) - responses.append(await backend.authenticate(request)) - except ImportError: - continue - return responses - -app.include_router(router) app.include_router(oauth2_router) app.add_middleware(OAuth2Middleware, config={ "allow_http": True, @@ -71,6 +31,13 @@ async def test_auth_redirect(): @pytest.mark.anyio -async def test_backends(): - async with AsyncClient(app=app, base_url="http://test") as client: - assert all((await client.get("/test_backends")).json()) +async def test_core_init(backends): + for backend in backends: + try: + OAuth2Core(OAuth2Client( + backend=backend, + client_id="test_client_id", + client_secret="test_client_secret", + )) + except (NotImplementedError, Exception): + assert False