From d0f10806e6a889a4539d5d466fc182b6a4d7dae3 Mon Sep 17 00:00:00 2001 From: armin shoughi Date: Mon, 1 May 2023 11:52:01 +0330 Subject: [PATCH] fix: Initializing KeycloakAdmin without server_url (#439) * fix: initializing KeycloakAdmin without server_url According to the project readme, we could initialize a KeycloakAdmin object with a KeycloakOpenIDConnection object without other arguments but, server_url is required. I made server_url optional and wrote a test for it. * Revert "fix: initializing KeycloakAdmin without server_url" This reverts commit 09cf503415761bd902f505bbeb4ee596c271e30b. * fix: initializing KeycloakAdmin without server_url According to the project readme, we could initialize a KeycloakAdmin object with a KeycloakOpenIDConnection object without other arguments but, server_url is required. I made server_url optional and wrote a test for it. * fix: check linting, formatting --------- Co-authored-by: Armin Shoughi --- src/keycloak/keycloak_admin.py | 2 +- tests/test_keycloak_admin.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index e740ce1..8ee275a 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -88,7 +88,7 @@ class KeycloakAdmin: def __init__( self, - server_url, + server_url=None, username=None, password=None, token=None, diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index e984023..454acfc 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -9,7 +9,7 @@ import pytest from dateutil import parser as datetime_parser import keycloak -from keycloak import KeycloakAdmin, KeycloakOpenID +from keycloak import KeycloakAdmin, KeycloakOpenID, KeycloakOpenIDConnection from keycloak.connection import ConnectionManager from keycloak.exceptions import ( KeycloakAuthenticationError, @@ -111,6 +111,17 @@ def test_keycloak_admin_init(env): is None ) + keycloak_connection = KeycloakOpenIDConnection( + server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", + username=env.KEYCLOAK_ADMIN, + password=env.KEYCLOAK_ADMIN_PASSWORD, + realm_name="master", + client_id="admin-cli", + verify=True, + ) + keycloak_admin = KeycloakAdmin(connection=keycloak_connection) + assert keycloak_admin.token + def test_realms(admin: KeycloakAdmin): """Test realms.