Skip to content

Commit

Permalink
Test login callback
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted committed Feb 27, 2024
1 parent 8390808 commit 8c7298d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import Generator
from fastapi import FastAPI, status
from starlette.datastructures import URL
from starlette.middleware.sessions import SessionMiddleware
from keycloak import KeycloakAdmin
import pytest
Expand Down Expand Up @@ -32,7 +33,7 @@ def keycloak(self) -> Generator[KeycloakAdmin, None, None]:
json.loads(Path(self.RESOURCES_PATH / "realm.json").read_bytes())
)
keycloak.change_current_realm("bakdata")
assert keycloak.connection.server_url == container.get_base_api_url() + "/"
assert keycloak.connection.base_url == container.get_base_api_url() + "/"
yield keycloak
container.stop()

Expand Down Expand Up @@ -64,7 +65,7 @@ def client(self, app: FastAPI, keycloak: KeycloakAdmin) -> TestClient:
keycloak_oauth = KeycloakOAuth2(
client_id="test-client",
client_secret="ZPSWENNxF0z3rT8xQORol9NpXQFJxiZf",
server_metadata_url=f"{keycloak.connection.server_url}/realms/bakdata/.well-known/openid-configuration",
server_metadata_url=f"{keycloak.connection.base_url}/realms/bakdata/.well-known/openid-configuration",
client_kwargs={},
)
keycloak_oauth.setup_fastapi_routes()
Expand All @@ -78,10 +79,15 @@ def test_keycloak_setup(self, keycloak: KeycloakAdmin):
assert keycloak.connection.realm_name == "bakdata"

def test_login_redirect(self, client: TestClient, keycloak: KeycloakAdmin):
response = client.get("/auth/login", follow_redirects=False)
assert response.status_code == status.HTTP_302_FOUND # redirect
assert response.headers["location"].startswith(keycloak.connection.server_url)
# http://localhost:32798/realms/bakdata/protocol/openid-connect/auth?response_type=code&client_id=test-client&redirect_uri=http%3A%2F%2Ftestserver%2Fauth%2Fcallback&state=MppMcnKsZd5DTP88QtWeOTxOU6NlBC
response = client.get("/auth/login")
keycloak_base_url = URL(keycloak.connection.base_url)
assert response.url.host == keycloak_base_url.hostname
assert response.url.port == keycloak_base_url.port
assert response.url.path == "/realms/bakdata/protocol/openid-connect/auth"
assert response.url.params["client_id"] == "test-client"
assert response.url.params["redirect_uri"]
assert URL(response.url.params["redirect_uri"]).path == "/auth/callback"
# assert response.status_code == status.HTTP_200_OK

def test_logout_redirect(self, client: TestClient):
response = client.get("/auth/logout", follow_redirects=False)
Expand Down

0 comments on commit 8c7298d

Please sign in to comment.