diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index 5f8b20c..8923ba1 100644 --- a/src/keycloak/keycloak_admin.py +++ b/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. diff --git a/src/keycloak/urls_patterns.py b/src/keycloak/urls_patterns.py index b40bcef..9dc18fd 100644 --- a/src/keycloak/urls_patterns.py +++ b/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" diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index d854fe5..bfa98c3 100644 --- a/tests/test_keycloak_admin.py +++ b/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)