Skip to content

Commit

Permalink
Fix Keycloak server url
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted committed Feb 27, 2024
1 parent 51900c1 commit 8390808
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class TestKeycloakOAuth2:
KEYCLOAK_BASE_URL = "http://localhost:8080"
RESOURCES_PATH = Path(__file__).parent.absolute() / "resources/keycloak"

@pytest.fixture(scope="session")
Expand All @@ -33,6 +32,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() + "/"
yield keycloak
container.stop()

Expand Down Expand Up @@ -60,15 +60,15 @@ def app(self) -> FastAPI:
return FastAPI()

@pytest.fixture()
def client(self, app: FastAPI) -> TestClient:
keycloak = KeycloakOAuth2(
def client(self, app: FastAPI, keycloak: KeycloakAdmin) -> TestClient:
keycloak_oauth = KeycloakOAuth2(
client_id="test-client",
client_secret="ZPSWENNxF0z3rT8xQORol9NpXQFJxiZf",
server_metadata_url=f"{self.KEYCLOAK_BASE_URL}/realms/bakdata/.well-known/openid-configuration",
server_metadata_url=f"{keycloak.connection.server_url}/realms/bakdata/.well-known/openid-configuration",
client_kwargs={},
)
keycloak.setup_fastapi_routes()
app.include_router(keycloak.router, prefix="/auth")
keycloak_oauth.setup_fastapi_routes()
app.include_router(keycloak_oauth.router, prefix="/auth")
app.add_middleware(
SessionMiddleware, secret_key="ZPSWENNxF0z3rT8xQORol9NpXQFJxiZf"
)
Expand All @@ -77,11 +77,11 @@ def client(self, app: FastAPI) -> TestClient:
def test_keycloak_setup(self, keycloak: KeycloakAdmin):
assert keycloak.connection.realm_name == "bakdata"

@pytest.mark.usefixtures("keycloak")
def test_login_redirect(self, client: TestClient):
def test_login_redirect(self, client: TestClient, keycloak: KeycloakAdmin):
response = client.get("/auth/login", follow_redirects=False)
assert response.status_code == status.HTTP_307_TEMPORARY_REDIRECT
assert response.headers["location"] == "/auth/callback"
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

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

0 comments on commit 8390808

Please sign in to comment.