Skip to content

Commit

Permalink
Fix asyncio usage in test
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored and torbsto committed Mar 12, 2024
1 parent 4deda34 commit d147dd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
22 changes: 20 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions tests/test_oauth_signed_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
[
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit d147dd5

Please sign in to comment.