Browse Source

feat: Add get_child_groups(#509)

pull/516/head
Nikolai Eugen Sandvik 10 months ago
parent
commit
ec6e4f7a15
  1. 22
      src/keycloak/keycloak_admin.py
  2. 12
      tests/test_keycloak_admin.py

22
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)) data_raw = self.connection.raw_get(urls_patterns.URL_ADMIN_GROUP.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError) 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): def get_subgroups(self, group, path):
"""Get subgroups. """Get subgroups.

12
tests/test_keycloak_admin.py

@ -660,15 +660,21 @@ def test_groups(admin: KeycloakAdmin, user: str):
# Test get groups again # Test get groups again
groups = admin.get_groups() groups = admin.get_groups()
assert len(groups) == 1, 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 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 # Test get groups query
groups = admin.get_groups(query={"max": 10}) groups = admin.get_groups(query={"max": 10})
assert len(groups) == 1, groups 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 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} assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2}
# Test get group # Test get group

Loading…
Cancel
Save