From b9f8bd29d009495a082d99becf7a1f8ef699389d Mon Sep 17 00:00:00 2001 From: Richard Nemeth Date: Sat, 14 Dec 2024 13:39:12 +0100 Subject: [PATCH] fix: get group by path should not raise on 404 --- src/keycloak/keycloak_admin.py | 4 ++-- tests/test_keycloak_admin.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index eeeaea3..6b8234e 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -1149,7 +1149,7 @@ class KeycloakAdmin: data_raw = self.connection.raw_get( urls_patterns.URL_ADMIN_GROUP_BY_PATH.format(**params_path) ) - return raise_error_from_response(data_raw, KeycloakGetError) + return raise_error_from_response(data_raw, KeycloakGetError, [200, 404]) def create_group(self, payload, parent=None, skip_exists=False): """Create a group in the Realm. @@ -5460,7 +5460,7 @@ class KeycloakAdmin: data_raw = await self.connection.a_raw_get( urls_patterns.URL_ADMIN_GROUP_BY_PATH.format(**params_path) ) - return raise_error_from_response(data_raw, KeycloakGetError) + return raise_error_from_response(data_raw, KeycloakGetError, [200, 404]) async def a_create_group(self, payload, parent=None, skip_exists=False): """Create a group in the Realm asynchronously. diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index e33614b..3e1e98f 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -845,9 +845,12 @@ def test_groups(admin: KeycloakAdmin, user: str): assert res is not None, res assert res["id"] == subgroup_id_1, res - with pytest.raises(KeycloakGetError) as err: - admin.get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1/test") - assert err.match('404: b\'{"error":"Group path does not exist".*}\'') + res = admin.get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1/test") + assert res == { + "error": "Group path does not exist", + "error_description": "For more on this error consult the server log at the " + "debug level.", + }, res res = admin.get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1") assert res is not None, res @@ -3947,9 +3950,12 @@ async def test_a_groups(admin: KeycloakAdmin, user: str): assert res is not None, res assert res["id"] == subgroup_id_1, res - with pytest.raises(KeycloakGetError) as err: - await admin.a_get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1/test") - assert err.match('404: b\'{"error":"Group path does not exist".*}\'') + res = await admin.a_get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1/test") + assert res == { + "error": "Group path does not exist", + "error_description": "For more on this error consult the server log at the " + "debug level.", + }, res res = await admin.a_get_group_by_path(path="/main-group/subgroup-2/subsubgroup-1") assert res is not None, res