Browse Source

feat: Add update_mapper_in_idp

pull/333/head
Bruno Bonfils 3 years ago
parent
commit
f3de47e1b3
  1. 27
      src/keycloak/keycloak_admin.py
  2. 1
      src/keycloak/urls_patterns.py
  3. 21
      tests/test_keycloak_admin.py

27
src/keycloak/keycloak_admin.py

@ -413,6 +413,31 @@ class KeycloakAdmin:
) )
return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201]) return raise_error_from_response(data_raw, KeycloakPostError, expected_codes=[201])
def update_mapper_in_idp(self, idp_alias, mapper_id, payload):
"""
Update an IdP mapper
IdentityProviderMapperRepresentation
https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_update
:param: idp_alias: alias for Idp to fetch mappers
:param: mapper_id: Mapper Id to update
:param: payload: IdentityProviderMapperRepresentation
:return: Http response
"""
params_path = {
"realm-name": self.realm_name,
"idp-alias": idp_alias,
"mapper-id": mapper_id,
}
data_raw = self.raw_put(
urls_patterns.URL_ADMIN_IDP_MAPPER_UPDATE.format(**params_path),
data=json.dumps(payload),
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204])
def get_idp_mappers(self, idp_alias): def get_idp_mappers(self, idp_alias):
""" """
Returns a list of ID Providers mappers Returns a list of ID Providers mappers
@ -2208,7 +2233,7 @@ class KeycloakAdmin:
""" """
params_path = {"realm-name": self.realm_name, "scope-id": client_scope_id} params_path = {"realm-name": self.realm_name, "scope-id": client_scope_id}
data_raw = self.raw_get( data_raw = self.raw_get(
urls_patterns.URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path),
urls_patterns.URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER.format(**params_path)
) )
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200]) return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[200])

1
src/keycloak/urls_patterns.py

@ -119,6 +119,7 @@ URL_ADMIN_REALMS = "admin/realms"
URL_ADMIN_REALM = "admin/realms/{realm-name}" URL_ADMIN_REALM = "admin/realms/{realm-name}"
URL_ADMIN_IDPS = "admin/realms/{realm-name}/identity-provider/instances" URL_ADMIN_IDPS = "admin/realms/{realm-name}/identity-provider/instances"
URL_ADMIN_IDP_MAPPERS = "admin/realms/{realm-name}/identity-provider/instances/{idp-alias}/mappers" URL_ADMIN_IDP_MAPPERS = "admin/realms/{realm-name}/identity-provider/instances/{idp-alias}/mappers"
URL_ADMIN_IDP_MAPPER_UPDATE = URL_ADMIN_IDP_MAPPERS + "/{mapper-id}"
URL_ADMIN_IDP = "admin/realms//{realm-name}/identity-provider/instances/{alias}" URL_ADMIN_IDP = "admin/realms//{realm-name}/identity-provider/instances/{alias}"
URL_ADMIN_REALM_ROLES_ROLE_BY_NAME = "admin/realms/{realm-name}/roles/{role-name}" URL_ADMIN_REALM_ROLES_ROLE_BY_NAME = "admin/realms/{realm-name}/roles/{role-name}"
URL_ADMIN_REALM_ROLES_COMPOSITE_REALM_ROLE = ( URL_ADMIN_REALM_ROLES_COMPOSITE_REALM_ROLE = (

21
tests/test_keycloak_admin.py

@ -337,10 +337,23 @@ def test_idps(admin: KeycloakAdmin, realm: str):
assert err.match('404: b\'{"error":"HTTP 404 Not Found"}\'') assert err.match('404: b\'{"error":"HTTP 404 Not Found"}\'')
# Test IdP mappers listing # Test IdP mappers listing
idp_mappers = admin.get_idp_mappers(
idp_mappers = admin.get_idp_mappers(idp_alias="github")
assert len(idp_mappers) == 1
# Test IdP mapper update
res = admin.update_mapper_in_idp(
idp_alias="github", idp_alias="github",
mapper_id=idp_mappers[0]["id"],
# For an obscure reason, keycloak expect all fields
payload={
"id": idp_mappers[0]["id"],
"identityProviderAlias": "github-alias",
"identityProviderMapper": "github-user-attribute-mapper",
"name": "test",
"config": idp_mappers[0]["config"],
},
) )
assert len(idp_mappers) == 1
assert res == dict(), res
# Test delete # Test delete
res = admin.delete_idp(idp_alias="github") res = admin.delete_idp(idp_alias="github")
@ -1452,9 +1465,7 @@ def test_client_scopes(admin: KeycloakAdmin, realm: str):
assert err.match('404: b\'{"error":"Could not find client scope"}\'') assert err.match('404: b\'{"error":"Could not find client scope"}\'')
test_mapper["config"]["user.attribute"] = "test" test_mapper["config"]["user.attribute"] = "test"
res_update = admin.update_mapper_in_client_scope( res_update = admin.update_mapper_in_client_scope(
client_scope_id=res,
protocol_mapper_id=test_mapper["id"],
payload=test_mapper,
client_scope_id=res, protocol_mapper_id=test_mapper["id"], payload=test_mapper
) )
assert res_update == dict() assert res_update == dict()
assert ( assert (

Loading…
Cancel
Save