From 2e2735a38882f3d08e506fc48208bbe522b82300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cain=C3=A3?= <40773170+cainotis@users.noreply.github.com> Date: Tue, 27 Jun 2023 20:31:23 -0300 Subject: [PATCH] feat: added KeycloakAdmin.update_client_authz_resource() (#462) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: added KeycloakAdmin.update_client_authz_resource() Signed-off-by: Cainã S. G * fix: linting Signed-off-by: Cainã S. G * fix: test expecting and different anwser from server Signed-off-by: Cainã S. G * fix: test expecting and different anwser from server Signed-off-by: Cainã S. G --------- Signed-off-by: Cainã S. G Co-authored-by: Cainã S. G --- src/keycloak/keycloak_admin.py | 33 +++++++++++++++++++++++++++++++++ tests/test_keycloak_admin.py | 17 ++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/keycloak/keycloak_admin.py b/src/keycloak/keycloak_admin.py index bf84239..7c86577 100644 --- a/src/keycloak/keycloak_admin.py +++ b/src/keycloak/keycloak_admin.py @@ -1533,6 +1533,39 @@ class KeycloakAdmin: data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists ) + def update_client_authz_resource(self, client_id, resource_id, payload): + """Update resource of client. + + Any parameter missing from the ResourceRepresentation in the payload WILL be set + to default by the Keycloak server. + + :param client_id: id in ClientRepresentation + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_clientrepresentation + :type client_id: str + :param payload: ResourceRepresentation + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_resourcerepresentation + :type payload: dict + :param client_id: id in ClientRepresentation + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_clientrepresentation + :type client_id: str + :param resource_id: id in ResourceRepresentation + https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_resourcerepresentation + :type resource_id: str + + :return: Keycloak server response + :rtype: bytes + """ + params_path = { + "realm-name": self.connection.realm_name, + "id": client_id, + "resource-id": resource_id, + } + data_raw = self.connection.raw_put( + urls_patterns.URL_ADMIN_CLIENT_AUTHZ_RESOURCE.format(**params_path), + data=json.dumps(payload), + ) + return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204]) + def delete_client_authz_resource(self, client_id: str, resource_id: str): """Delete a client resource. diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index fdbc56b..eec3a28 100644 --- a/tests/test_keycloak_admin.py +++ b/tests/test_keycloak_admin.py @@ -851,7 +851,22 @@ def test_clients(admin: KeycloakAdmin, realm: str): client_id=auth_client_id, payload={"name": "temp-resource"} ) assert res["name"] == "temp-resource", res - temp_resource_id = res["_id"] + temp_resource_id: str = res["_id"] + # Test update authz resources + admin.update_client_authz_resource( + client_id=auth_client_id, + resource_id=temp_resource_id, + payload={"name": "temp-updated-resource"}, + ) + res = admin.get_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id) + assert res["name"] == "temp-updated-resource", res + with pytest.raises(KeycloakPutError) as err: + admin.update_client_authz_resource( + client_id=auth_client_id, + resource_id="invalid_resource_id", + payload={"name": "temp-updated-resource"}, + ) + assert err.match("404: b''"), err admin.delete_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id) with pytest.raises(KeycloakGetError) as err: admin.get_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id)