Browse Source

Merge pull request #399 from Hadeer-Elsaeed/feat/create_client_authz_scopes

feat(api): add function create_authz_scopes
pull/401/head v2.8.0
Richard Nemeth 2 years ago
committed by GitHub
parent
commit
3580718b38
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      src/keycloak/keycloak_admin.py
  2. 18
      tests/test_keycloak_admin.py

19
src/keycloak/keycloak_admin.py

@ -1475,6 +1475,25 @@ class KeycloakAdmin:
data_raw = self.raw_get(urls_patterns.URL_ADMIN_CLIENT_AUTHZ_SCOPES.format(**params_path)) data_raw = self.raw_get(urls_patterns.URL_ADMIN_CLIENT_AUTHZ_SCOPES.format(**params_path))
return raise_error_from_response(data_raw, KeycloakGetError) return raise_error_from_response(data_raw, KeycloakGetError)
def create_client_authz_scopes(self, client_id, payload):
"""Create scopes for client.
:param client_id: id in ClientRepresentation
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_clientrepresentation
:param payload: ScopeRepresentation
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_ScopeRepresentation
:type payload: dict
:type client_id: str
:return: Keycloak server response
:rtype: bytes
"""
params_path = {"realm-name": self.realm_name, "id": client_id}
data_raw = self.raw_post(
urls_patterns.URL_ADMIN_CLIENT_AUTHZ_SCOPES.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201])
def get_client_authz_permissions(self, client_id): def get_client_authz_permissions(self, client_id):
"""Get permissions from client. """Get permissions from client.

18
tests/test_keycloak_admin.py

@ -890,6 +890,24 @@ def test_clients(admin: KeycloakAdmin, realm: str):
admin.get_client_authz_scopes(client_id=client_id) admin.get_client_authz_scopes(client_id=client_id)
assert err.match('404: b\'{"error":"HTTP 404 Not Found"}\'') assert err.match('404: b\'{"error":"HTTP 404 Not Found"}\'')
res = admin.create_client_authz_scopes(
client_id=auth_client_id, payload={"name": "test-authz-scope"}
)
assert res["name"] == "test-authz-scope", res
with pytest.raises(KeycloakPostError) as err:
admin.create_client_authz_scopes(
client_id="invalid_client_id", payload={"name": "test-authz-scope"}
)
assert err.match('404: b\'{"error":"Could not find client"')
assert admin.create_client_authz_scopes(
client_id=auth_client_id, payload={"name": "test-authz-scope"}
)
res = admin.get_client_authz_scopes(client_id=auth_client_id)
assert len(res) == 1
assert {x["name"] for x in res} == {"test-authz-scope"}
# Test service account user # Test service account user
res = admin.get_client_service_account_user(client_id=auth_client_id) res = admin.get_client_service_account_user(client_id=auth_client_id)
assert res["username"] == "service-account-authz-client", res assert res["username"] == "service-account-authz-client", res

Loading…
Cancel
Save