Browse Source

fix: depracated endpoint and fix groups services.

fix/new-keycloak
Marcos Pereira Jr 10 months ago
parent
commit
039cd75c2a
  1. 40
      src/keycloak/keycloak_admin.py
  2. 3
      tests/test_keycloak_admin.py

40
src/keycloak/keycloak_admin.py

@ -1286,9 +1286,15 @@ class KeycloakAdmin:
url = urls_patterns.URL_ADMIN_GROUPS.format(**params_path) url = urls_patterns.URL_ADMIN_GROUPS.format(**params_path)
if "first" in query or "max" in query: if "first" in query or "max" in query:
return self.__fetch_paginated(url, query)
groups = self.__fetch_paginated(url, query)
groups = self.__fetch_all(url, query)
return self.__fetch_all(url, query)
# For version +23.0.0
for group in groups:
if group.get("subGroupCount"):
group["subGroups"] = self.get_group_children(group.get('id'))
return groups
def get_group(self, group_id): def get_group(self, group_id):
"""Get group by id. """Get group by id.
@ -1304,8 +1310,17 @@ class KeycloakAdmin:
:rtype: dict :rtype: dict
""" """
params_path = {"realm-name": self.connection.realm_name, "id": group_id} params_path = {"realm-name": self.connection.realm_name, "id": group_id}
data_raw = self.connection.raw_get(urls_patterns.URL_ADMIN_GROUP.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError)
response = self.connection.raw_get(urls_patterns.URL_ADMIN_GROUP.format(**params_path))
if response.status_code >= 400:
return raise_error_from_response(response, KeycloakGetError)
# For version +23.0.0
group = response.json()
if group.get("subGroupCount"):
group["subGroups"] = self.get_group_children(group.get('id'))
return group
def get_subgroups(self, group, path): def get_subgroups(self, group, path):
"""Get subgroups. """Get subgroups.
@ -1333,6 +1348,23 @@ class KeycloakAdmin:
# went through the tree without hits # went through the tree without hits
return None return None
def get_group_children(self, group_id):
"""Get group children by id.
Returns full group details
GroupRepresentation
https://www.keycloak.org/docs-api/18.0/rest-api/#_grouprepresentation
:param group_id: The group id
:type group_id: str
:return: Keycloak server response (GroupRepresentation)
:rtype: dict
"""
params_path = {"realm-name": self.connection.realm_name, "id": group_id}
data_raw = self.connection.raw_get(urls_patterns.URL_ADMIN_GROUP_CHILD.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError)
def get_group_members(self, group_id, query=None): def get_group_members(self, group_id, query=None):
"""Get members by group id. """Get members by group id.

3
tests/test_keycloak_admin.py

@ -687,7 +687,8 @@ def test_groups(admin: KeycloakAdmin, user: str):
main_group = admin.get_group(group_id=group_id) main_group = admin.get_group(group_id=group_id)
# Test nested searches # Test nested searches
res = admin.get_subgroups(group=main_group, path="/main-group/subgroup-2/subsubgroup-1")
subgroup_2 = admin.get_group(group_id=subgroup_id_2)
res = admin.get_subgroups(group=subgroup_2, path="/main-group/subgroup-2/subsubgroup-1")
assert res is not None, res assert res is not None, res
assert res["id"] == subsubgroup_id_1 assert res["id"] == subsubgroup_id_1

Loading…
Cancel
Save