Browse Source

Merge pull request #525 from marcospereirampj/fix/new-keycloak

Fix: New Keycloak Version
fix/update-dependencies
Marcos Pereira 10 months ago
committed by GitHub
parent
commit
f594c7b20c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 39
      src/keycloak/keycloak_admin.py
  2. 2
      src/keycloak/keycloak_openid.py
  3. 7
      tests/test_keycloak_admin.py

39
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,22 @@ 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 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): def get_group_members(self, group_id, query=None):
"""Get members by group id. """Get members by group id.

2
src/keycloak/keycloak_openid.py

@ -477,7 +477,7 @@ class KeycloakOpenID:
params_path = {"realm-name": self.realm_name, "resource-server-id": 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)) 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, KeycloakDeprecationError)
return raise_error_from_response(data_raw, KeycloakGetError) # pragma: no cover return raise_error_from_response(data_raw, KeycloakGetError) # pragma: no cover

7
tests/test_keycloak_admin.py

@ -660,14 +660,14 @@ 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 len(groups[0]["subGroups"]) == 2, groups[0]["subGroups"]
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} assert {x["id"] for x in groups[0]["subGroups"]} == {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 len(groups[0]["subGroups"]) == 2, groups["subGroups"]
assert len(groups[0]["subGroups"]) == 2, groups[0]["subGroups"]
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} assert {x["id"] for x in groups[0]["subGroups"]} == {subgroup_id_1, subgroup_id_2}
@ -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