Browse Source

Merge pull request #316 from marcospereirampj/315-creating-client-or-realm-roles-is-failing-if-skip_exists-is-true

315 creating client or realm roles is failing if skip exists is true
pull/323/head
Richard Nemeth 3 years ago
committed by GitHub
parent
commit
7ab033c7bc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      README.md
  2. 8
      src/keycloak/keycloak_admin.py
  3. 4
      tests/test_keycloak_admin.py

4
README.md

@ -154,7 +154,7 @@ new_user = keycloak_admin.create_user({"email": "example@example.fr",
"lastName": "Example", "lastName": "Example",
"attributes": { "attributes": {
"locale": ["fr"] "locale": ["fr"]
})
}})
# User counter # User counter
count_users = keycloak_admin.users_count() count_users = keycloak_admin.users_count()
@ -226,7 +226,7 @@ role = keycloak_admin.get_client_role(client_id="client_id", role_name="role_nam
role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test") role_id = keycloak_admin.get_client_role_id(client_id="client_id", role_name="test")
# Create client role # Create client role
keycloak_admin.create_client_role(client_role_id='client_id', {'name': 'roleName', 'clientRole': True})
keycloak_admin.create_client_role(client_role_id='client_id', payload={'name': 'roleName', 'clientRole': True})
# Assign client role to user. Note that BOTH role_name and role_id appear to be required. # Assign client role to user. Note that BOTH role_name and role_id appear to be required.
keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test") keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test")

8
src/keycloak/keycloak_admin.py

@ -1361,9 +1361,11 @@ class KeycloakAdmin:
""" """
if skip_exists: if skip_exists:
try:
res = self.get_client_role(client_id=client_role_id, role_name=payload["name"]) res = self.get_client_role(client_id=client_role_id, role_name=payload["name"])
if res:
return res["name"] return res["name"]
except KeycloakGetError:
pass
params_path = {"realm-name": self.realm_name, "id": client_role_id} params_path = {"realm-name": self.realm_name, "id": client_role_id}
data_raw = self.raw_post( data_raw = self.raw_post(
@ -1480,9 +1482,11 @@ class KeycloakAdmin:
""" """
if skip_exists: if skip_exists:
try:
role = self.get_realm_role(role_name=payload["name"]) role = self.get_realm_role(role_name=payload["name"])
if role is not None:
return role["name"] return role["name"]
except KeycloakGetError:
pass
params_path = {"realm-name": self.realm_name} params_path = {"realm-name": self.realm_name}
data_raw = self.raw_post( data_raw = self.raw_post(

4
tests/test_keycloak_admin.py

@ -718,7 +718,7 @@ def test_realm_roles(admin: KeycloakAdmin, realm: str):
assert members == list(), members assert members == list(), members
# Test create realm role # Test create realm role
role_id = admin.create_realm_role(payload={"name": "test-realm-role"})
role_id = admin.create_realm_role(payload={"name": "test-realm-role"}, skip_exists=True)
assert role_id, role_id assert role_id, role_id
with pytest.raises(KeycloakPostError) as err: with pytest.raises(KeycloakPostError) as err:
admin.create_realm_role(payload={"name": "test-realm-role"}) admin.create_realm_role(payload={"name": "test-realm-role"})
@ -865,7 +865,7 @@ def test_client_roles(admin: KeycloakAdmin, client: str):
# Test create client role # Test create client role
client_role_id = admin.create_client_role( client_role_id = admin.create_client_role(
client_role_id=client, payload={"name": "client-role-test"}
client_role_id=client, payload={"name": "client-role-test"}, skip_exists=True
) )
with pytest.raises(KeycloakPostError) as err: with pytest.raises(KeycloakPostError) as err:
admin.create_client_role(client_role_id=client, payload={"name": "client-role-test"}) admin.create_client_role(client_role_id=client, payload={"name": "client-role-test"})

Loading…
Cancel
Save