diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 87ad78f..e5db618 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -1286,9 +1286,15 @@ class KeycloakAdmin: url = urls_patterns.URL_ADMIN_GROUPS.format(**params_path) 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): """Get group by id. @@ -1304,8 +1310,17 @@ class KeycloakAdmin: :rtype: dict """ 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): """Get subgroups. @@ -1333,6 +1348,23 @@ class KeycloakAdmin: # went through the tree without hits 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): """Get members by group id. diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 0c6c65a..7e6aa6a 100644 --- a/tests/test_keycloak_admin.py +++ b/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) # 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["id"] == subsubgroup_id_1