Browse Source

feat: add admin group count (#540)

pull/546/head v3.11.0
ThanhPT 9 months ago
committed by GitHub
parent
commit
ed124cdaae
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      src/keycloak/keycloak_admin.py
  2. 1
      src/keycloak/urls_patterns.py
  3. 8
      tests/test_keycloak_admin.py

18
src/keycloak/keycloak_admin.py

@ -1468,6 +1468,24 @@ class KeycloakAdmin:
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204])
def groups_count(self, query=None):
"""Count groups.
https://www.keycloak.org/docs-api/24.0.1/rest-api/index.html#_groups
:param query: (dict) Query parameters for groups count
:type query: dict
:return: Keycloak Server Response
:rtype: dict
"""
query = query or dict()
params_path = {"realm-name": self.connection.realm_name}
data_raw = self.connection.raw_get(
urls_patterns.URL_ADMIN_GROUPS_COUNT.format(**params_path), **query
)
return raise_error_from_response(data_raw, KeycloakGetError)
def group_set_permissions(self, group_id, enabled=True):
"""Enable/Disable permissions for a group.

1
src/keycloak/urls_patterns.py

@ -81,6 +81,7 @@ URL_ADMIN_USER_STORAGE = "admin/realms/{realm-name}/user-storage/{id}/sync"
URL_ADMIN_SERVER_INFO = "admin/serverinfo"
URL_ADMIN_GROUPS = "admin/realms/{realm-name}/groups"
URL_ADMIN_GROUPS_COUNT = "admin/realms/{realm-name}/groups/count"
URL_ADMIN_GROUP = "admin/realms/{realm-name}/groups/{id}"
URL_ADMIN_GROUP_BY_PATH = "admin/realms/{realm-name}/group-by-path/{path}"
URL_ADMIN_GROUP_CHILD = "admin/realms/{realm-name}/groups/{id}/children"

8
tests/test_keycloak_admin.py

@ -654,6 +654,14 @@ def test_groups(admin: KeycloakAdmin, user: str):
group_id = admin.create_group(payload={"name": "main-group"})
assert group_id is not None, group_id
# Test group count
count = admin.groups_count()
assert count.get("count") == 1, count
# Test group count with query
count = admin.groups_count(query={"search": "notpresent"})
assert count.get("count") == 0
# Test create subgroups
subgroup_id_1 = admin.create_group(payload={"name": "subgroup-1"}, parent=group_id)
subgroup_id_2 = admin.create_group(payload={"name": "subgroup-2"}, parent=group_id)

Loading…
Cancel
Save