diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 87ad78f..eddaed9 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -1307,6 +1307,28 @@ class KeycloakAdmin: data_raw = self.connection.raw_get(urls_patterns.URL_ADMIN_GROUP.format(**params_path)) return raise_error_from_response(data_raw, KeycloakGetError) + def get_child_groups(self, group_id, query=None): + """Get child groups by parent id. + + Returns a list of groups who are children of group with id + + 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: list + """ + query = query or {} + params_path = {"realm-name": self.connection.realm_name, "id": group_id} + url = urls_patterns.URL_ADMIN_GROUP_CHILD.format(**params_path) + + if "first" in query or "max" in query: + return self.__fetch_paginated(url, query) + + return self.__fetch_all(url, query) + def get_subgroups(self, group, path): """Get subgroups. diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index c5aa6ff..a91c0a9 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -660,15 +660,21 @@ def test_groups(admin: KeycloakAdmin, user: str): # Test get groups again groups = admin.get_groups() assert len(groups) == 1, groups - assert len(groups[0]["subGroups"]) == 2, groups["subGroups"] + assert groups[0]["subGroupCount"] == 2, groups["subGroupCount"] assert groups[0]["id"] == group_id - assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2} + sub_groups = admin.get_child_groups(groups[0]["id"]) + assert {x["id"] for x in sub_groups} == {subgroup_id_1, subgroup_id_2} # Test get groups query groups = admin.get_groups(query={"max": 10}) assert len(groups) == 1, groups + assert groups[0]["subGroupCount"] == 2, groups["subGroupCount"] + + # Test get all groups query + groups = admin.get_groups(query={"q": "%"}) + assert len(groups) == 1, groups + assert groups[0]["subGroupCount"] == 2, groups["subGroupCount"] assert len(groups[0]["subGroups"]) == 2, groups["subGroups"] - assert groups[0]["id"] == group_id assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2} # Test get group