From e4905c1b85470218032fe99e7066c8a8ee2977cb Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Tue, 12 Mar 2024 12:48:04 +0000 Subject: [PATCH] Fix asyncio usage in test --- poetry.lock | 22 ++++++++++++++++++++-- pyproject.toml | 1 + tests/test_oauth_signed_jwt.py | 24 +++++++++++++++--------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/poetry.lock b/poetry.lock index cef82b2..80a6ef7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.0 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "annotated-types" @@ -1002,6 +1002,24 @@ tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +[[package]] +name = "pytest-asyncio" +version = "0.23.5.post1" +description = "Pytest support for asyncio" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pytest-asyncio-0.23.5.post1.tar.gz", hash = "sha256:b9a8806bea78c21276bc34321bbf234ba1b2ea5b30d9f0ce0f2dea45e4685813"}, + {file = "pytest_asyncio-0.23.5.post1-py3-none-any.whl", hash = "sha256:30f54d27774e79ac409778889880242b0403d09cabd65b727ce90fe92dd5d80e"}, +] + +[package.dependencies] +pytest = ">=7.0.0,<9" + +[package.extras] +docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"] +testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"] + [[package]] name = "pytest-mock" version = "3.12.0" @@ -1499,4 +1517,4 @@ starlette-admin = ["starlette-admin"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "7c9078dcd88df54bcfab651f7003db74ba1830b739b98673c3078b935edd4450" +content-hash = "630493c00273131e0cbd33e01f97707911612e1740b3de469941f35e3f2000c6" diff --git a/pyproject.toml b/pyproject.toml index 62ae914..77500b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ pyright = "^1.1.351" [tool.poetry.group.test.dependencies] pytest = "^8.0.1" +pytest-asyncio = "^0.23.5.post1" pytest-mock = "^3.12.0" python-keycloak = "^3.9.0" testcontainers-keycloak = { git = "https://github.com/TheForgottened/testcontainers-python", subdirectory = "keycloak" } # updated Keycloak container: https://github.com/testcontainers/testcontainers-python/pull/369 diff --git a/tests/test_oauth_signed_jwt.py b/tests/test_oauth_signed_jwt.py index 3829af2..5b14215 100644 --- a/tests/test_oauth_signed_jwt.py +++ b/tests/test_oauth_signed_jwt.py @@ -3,6 +3,7 @@ from typing import Annotated, Generator from fastapi import Depends, FastAPI, Request, status import httpx +import pytest_asyncio from starlette.middleware.sessions import SessionMiddleware from keycloak import KeycloakAdmin import pytest @@ -54,17 +55,17 @@ def keycloak(self) -> Generator[KeycloakAdmin, None, None]: def app(self) -> FastAPI: return FastAPI() - @pytest.fixture() - def client(self, app: FastAPI, keycloak: KeycloakAdmin) -> TestClient: + @pytest_asyncio.fixture() + async def client(self, app: FastAPI, keycloak: KeycloakAdmin) -> TestClient: keycloak_oauth = KeycloakOAuth2( client_id="test-client", client_secret=None, server_metadata_url=f"{keycloak.connection.base_url}/realms/bakdata/.well-known/openid-configuration", client_kwargs={ "scope": "openid profile email", - "code_challenge_method": "S256", }, ) + await keycloak_oauth.setup_signed_jwt() keycloak_oauth.setup_fastapi_routes() app.include_router(keycloak_oauth.router, prefix="/auth") app.add_middleware(SessionMiddleware, secret_key="!secret") @@ -92,14 +93,17 @@ def bar( def test_keycloak_setup(self, keycloak: KeycloakAdmin): assert keycloak.connection.realm_name == "bakdata" - def test_public_keys_endpoint(self, client: TestClient): - assert client.get("/auth/certs").json()["keys"] == [] + @pytest.mark.asyncio + async def test_public_keys_endpoint(self, client: TestClient): + assert client.get("/auth/certs").json()["keys"] + @pytest.mark.asyncio @pytest.mark.parametrize("endpoint", ["/", "/foo", "/bar"]) - def test_protected_endpoint(self, client: TestClient, endpoint: str): + async def test_protected_endpoint(self, client: TestClient, endpoint: str): response = client.get(endpoint) assert response.status_code == status.HTTP_401_UNAUTHORIZED + @pytest.mark.asyncio @pytest.mark.parametrize( "query_params", [ @@ -108,7 +112,7 @@ def test_protected_endpoint(self, client: TestClient, endpoint: str): httpx.QueryParams({"next": "/bar", "unrelated": "should be hidden"}), ], ) - def test_login_redirect( + async def test_login_redirect( self, client: TestClient, keycloak: KeycloakAdmin, @@ -129,12 +133,14 @@ def test_login_redirect( else: assert not redirect_uri.params - def test_logout_redirect(self, client: TestClient): + @pytest.mark.asyncio + async def test_logout_redirect(self, client: TestClient): response = client.get("/auth/logout", follow_redirects=False) assert response.status_code == status.HTTP_307_TEMPORARY_REDIRECT assert response.headers["location"] == "/" - def test_auth_flow(self, client: TestClient, keycloak: KeycloakAdmin): + @pytest.mark.asyncio + async def test_auth_flow(self, client: TestClient, keycloak: KeycloakAdmin): # try accessing protected endpoint response = client.get("/", follow_redirects=False) assert response.status_code == status.HTTP_401_UNAUTHORIZED