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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
12 additions and
8 deletions
-
README.md
-
src/keycloak/keycloak_admin.py
-
tests/test_keycloak_admin.py
|
|
@ -154,7 +154,7 @@ new_user = keycloak_admin.create_user({"email": "example@example.fr", |
|
|
|
"lastName": "Example", |
|
|
|
"attributes": { |
|
|
|
"locale": ["fr"] |
|
|
|
}) |
|
|
|
}}) |
|
|
|
|
|
|
|
# User counter |
|
|
|
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") |
|
|
|
|
|
|
|
# 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. |
|
|
|
keycloak_admin.assign_client_role(client_id="client_id", user_id="user_id", role_id="role_id", role_name="test") |
|
|
|
|
|
@ -1361,9 +1361,11 @@ class KeycloakAdmin: |
|
|
|
""" |
|
|
|
|
|
|
|
if skip_exists: |
|
|
|
res = self.get_client_role(client_id=client_role_id, role_name=payload["name"]) |
|
|
|
if res: |
|
|
|
try: |
|
|
|
res = self.get_client_role(client_id=client_role_id, role_name=payload["name"]) |
|
|
|
return res["name"] |
|
|
|
except KeycloakGetError: |
|
|
|
pass |
|
|
|
|
|
|
|
params_path = {"realm-name": self.realm_name, "id": client_role_id} |
|
|
|
data_raw = self.raw_post( |
|
|
@ -1480,9 +1482,11 @@ class KeycloakAdmin: |
|
|
|
""" |
|
|
|
|
|
|
|
if skip_exists: |
|
|
|
role = self.get_realm_role(role_name=payload["name"]) |
|
|
|
if role is not None: |
|
|
|
try: |
|
|
|
role = self.get_realm_role(role_name=payload["name"]) |
|
|
|
return role["name"] |
|
|
|
except KeycloakGetError: |
|
|
|
pass |
|
|
|
|
|
|
|
params_path = {"realm-name": self.realm_name} |
|
|
|
data_raw = self.raw_post( |
|
|
|
|
|
@ -718,7 +718,7 @@ def test_realm_roles(admin: KeycloakAdmin, realm: str): |
|
|
|
assert members == list(), members |
|
|
|
|
|
|
|
# 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 |
|
|
|
with pytest.raises(KeycloakPostError) as err: |
|
|
|
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 |
|
|
|
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: |
|
|
|
admin.create_client_role(client_role_id=client, payload={"name": "client-role-test"}) |
|
|
|