Browse Source

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.
pull/439/head
Armin Shoughi 2 years ago
parent
commit
09cf503415
  1. 16
      src/keycloak/keycloak_admin.py
  2. 13
      tests/test_keycloak_admin.py

16
src/keycloak/keycloak_admin.py

@ -88,7 +88,7 @@ class KeycloakAdmin:
def __init__( def __init__(
self, self,
server_url,
server_url=None,
username=None, username=None,
password=None, password=None,
token=None, token=None,
@ -815,7 +815,7 @@ class KeycloakAdmin:
) )
raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201]) raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201])
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def users_count(self, query=None): def users_count(self, query=None):
"""Count users. """Count users.
@ -1339,7 +1339,7 @@ class KeycloakAdmin:
) )
try: try:
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
except KeyError: except KeyError:
return return
@ -1965,7 +1965,7 @@ class KeycloakAdmin:
data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists
) )
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def update_client(self, client_id, payload): def update_client(self, client_id, payload):
"""Update a client. """Update a client.
@ -2218,7 +2218,7 @@ class KeycloakAdmin:
data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists
) )
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def add_composite_client_roles_to_role(self, client_role_id, role_name, roles): def add_composite_client_roles_to_role(self, client_role_id, role_name, roles):
"""Add composite roles to client role. """Add composite roles to client role.
@ -2385,7 +2385,7 @@ class KeycloakAdmin:
data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists
) )
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def get_realm_role(self, role_name): def get_realm_role(self, role_name):
"""Get realm role by role name. """Get realm role by role name.
@ -3281,7 +3281,7 @@ class KeycloakAdmin:
data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists
) )
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def update_client_scope(self, client_scope_id, payload): def update_client_scope(self, client_scope_id, payload):
"""Update a client scope. """Update a client scope.
@ -3650,7 +3650,7 @@ class KeycloakAdmin:
) )
raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201]) raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201])
_last_slash_idx = data_raw.headers["Location"].rindex("/") _last_slash_idx = data_raw.headers["Location"].rindex("/")
return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203
return data_raw.headers["Location"][_last_slash_idx + 1:] # noqa: E203
def get_component(self, component_id): def get_component(self, component_id):
"""Get representation of the component. """Get representation of the component.

13
tests/test_keycloak_admin.py

@ -19,6 +19,8 @@ from keycloak.exceptions import (
KeycloakPutError, KeycloakPutError,
) )
from src.keycloak import KeycloakOpenIDConnection
def test_keycloak_version(): def test_keycloak_version():
"""Test version.""" """Test version."""
@ -111,6 +113,17 @@ def test_keycloak_admin_init(env):
is None 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): def test_realms(admin: KeycloakAdmin):
"""Test realms. """Test realms.

Loading…
Cancel
Save