Skip to content

Commit 326a2d3

Browse files
cainotisCainã S. G
andauthored
feat: added KeycloakAdmin.update_client_authz_resource() (#462)
* feat: added KeycloakAdmin.update_client_authz_resource() Signed-off-by: Cainã S. G <p-caina.galante@pd.tec.br> * fix: linting Signed-off-by: Cainã S. G <p-caina.galante@pd.tec.br> * fix: test expecting and different anwser from server Signed-off-by: Cainã S. G <p-caina.galante@pd.tec.br> * fix: test expecting and different anwser from server Signed-off-by: Cainã S. G <p-caina.galante@pd.tec.br> --------- Signed-off-by: Cainã S. G <p-caina.galante@pd.tec.br> Co-authored-by: Cainã S. G <p-caina.galante@pd.tec.br>
1 parent 3ea5bdc commit 326a2d3

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/keycloak/keycloak_admin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,39 @@ def create_client_authz_resource(self, client_id, payload, skip_exists=False):
15331533
data_raw, KeycloakPostError, expected_codes=[201], skip_exists=skip_exists
15341534
)
15351535

1536+
def update_client_authz_resource(self, client_id, resource_id, payload):
1537+
"""Update resource of client.
1538+
1539+
Any parameter missing from the ResourceRepresentation in the payload WILL be set
1540+
to default by the Keycloak server.
1541+
1542+
:param client_id: id in ClientRepresentation
1543+
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_clientrepresentation
1544+
:type client_id: str
1545+
:param payload: ResourceRepresentation
1546+
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_resourcerepresentation
1547+
:type payload: dict
1548+
:param client_id: id in ClientRepresentation
1549+
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_clientrepresentation
1550+
:type client_id: str
1551+
:param resource_id: id in ResourceRepresentation
1552+
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_resourcerepresentation
1553+
:type resource_id: str
1554+
1555+
:return: Keycloak server response
1556+
:rtype: bytes
1557+
"""
1558+
params_path = {
1559+
"realm-name": self.connection.realm_name,
1560+
"id": client_id,
1561+
"resource-id": resource_id,
1562+
}
1563+
data_raw = self.connection.raw_put(
1564+
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_RESOURCE.format(**params_path),
1565+
data=json.dumps(payload),
1566+
)
1567+
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204])
1568+
15361569
def delete_client_authz_resource(self, client_id: str, resource_id: str):
15371570
"""Delete a client resource.
15381571

tests/test_keycloak_admin.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,22 @@ def test_clients(admin: KeycloakAdmin, realm: str):
851851
client_id=auth_client_id, payload={"name": "temp-resource"}
852852
)
853853
assert res["name"] == "temp-resource", res
854-
temp_resource_id = res["_id"]
854+
temp_resource_id: str = res["_id"]
855+
# Test update authz resources
856+
admin.update_client_authz_resource(
857+
client_id=auth_client_id,
858+
resource_id=temp_resource_id,
859+
payload={"name": "temp-updated-resource"},
860+
)
861+
res = admin.get_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id)
862+
assert res["name"] == "temp-updated-resource", res
863+
with pytest.raises(KeycloakPutError) as err:
864+
admin.update_client_authz_resource(
865+
client_id=auth_client_id,
866+
resource_id="invalid_resource_id",
867+
payload={"name": "temp-updated-resource"},
868+
)
869+
assert err.match("404: b''"), err
855870
admin.delete_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id)
856871
with pytest.raises(KeycloakGetError) as err:
857872
admin.get_client_authz_resource(client_id=auth_client_id, resource_id=temp_resource_id)

0 commit comments

Comments
 (0)