Skip to content

Commit

Permalink
Merge pull request #525 from marcospereirampj/fix/new-keycloak
Browse files Browse the repository at this point in the history
Fix: New Keycloak Version
  • Loading branch information
marcospereirampj authored Feb 13, 2024
2 parents bc810d1 + 16eec61 commit f594c7b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
39 changes: 35 additions & 4 deletions src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,9 +1286,15 @@ def get_groups(self, query=None):
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.
Expand All @@ -1304,8 +1310,17 @@ def get_group(self, group_id):
: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.
Expand Down Expand Up @@ -1333,6 +1348,22 @@ def get_subgroups(self, group, path):
# went through the tree without hits
return None

def get_group_children(self, group_id):
"""Get group children by id.
Returns full group children details
: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.
Expand Down
2 changes: 1 addition & 1 deletion src/keycloak/keycloak_openid.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def entitlement(self, token, resource_server_id):
params_path = {"realm-name": self.realm_name, "resource-server-id": resource_server_id}
data_raw = self.connection.raw_get(URL_ENTITLEMENT.format(**params_path))

if data_raw.status_code == 404:
if data_raw.status_code == 404 or data_raw.status_code == 405:
return raise_error_from_response(data_raw, KeycloakDeprecationError)

return raise_error_from_response(data_raw, KeycloakGetError) # pragma: no cover
Expand Down
7 changes: 4 additions & 3 deletions tests/test_keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,14 @@ 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 len(groups[0]["subGroups"]) == 2, groups[0]["subGroups"]
assert groups[0]["id"] == group_id
assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2}

# Test get groups query
groups = admin.get_groups(query={"max": 10})
assert len(groups) == 1, groups
assert len(groups[0]["subGroups"]) == 2, groups["subGroups"]
assert len(groups[0]["subGroups"]) == 2, groups[0]["subGroups"]
assert groups[0]["id"] == group_id
assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2}

Expand All @@ -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

Expand Down

0 comments on commit f594c7b

Please sign in to comment.