From d9c3326fd1f40c8e453762bf6279f320fffb9304 Mon Sep 17 00:00:00 2001 From: Richard Nemeth Date: Wed, 25 May 2022 20:16:18 +0200 Subject: [PATCH] fix: allow query parameters for users count --- src/keycloak/keycloak_admin.py | 9 +++++++-- tests/test_keycloak_admin.py | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index ffc3cde..766159a 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -464,14 +464,19 @@ class KeycloakAdmin: _last_slash_idx = data_raw.headers["Location"].rindex("/") return data_raw.headers["Location"][_last_slash_idx + 1 :] # noqa: E203 - def users_count(self): + def users_count(self, query=None): """ User counter + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_users_resource + + :param query: (dict) Query parameters for users count + :return: counter """ + query = query or dict() params_path = {"realm-name": self.realm_name} - data_raw = self.raw_get(urls_patterns.URL_ADMIN_USERS_COUNT.format(**params_path)) + data_raw = self.raw_get(urls_patterns.URL_ADMIN_USERS_COUNT.format(**params_path), **query) return raise_error_from_response(data_raw, KeycloakGetError) def get_user_id(self, username): diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 2990621..b0f1948 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -184,6 +184,10 @@ def test_users(admin: KeycloakAdmin, realm: str): count = admin.users_count() assert count == 1, count + # Test users count with query + count = admin.users_count(query={"username": "notpresent"}) + assert count == 0 + # Test user groups groups = admin.get_user_groups(user_id=user["id"]) assert len(groups) == 0