use of org.keycloak.representations.idm.ClientPoliciesRepresentation in project keycloak by keycloak.
the class ClientPoliciesLoadUpdateTest method testUpdateValidProfilesAndPolicies.
@Test
public void testUpdateValidProfilesAndPolicies() throws Exception {
setupValidProfilesAndPolicies();
assertExpectedLoadedProfiles((ClientProfilesRepresentation reps) -> {
ClientProfileRepresentation rep = getProfileRepresentation(reps, "ordinal-test-profile", false);
assertExpectedProfile(rep, "ordinal-test-profile", "The profile that can be loaded.");
});
assertExpectedLoadedPolicies((ClientPoliciesRepresentation reps) -> {
ClientPolicyRepresentation rep = getPolicyRepresentation(reps, "new-policy");
assertExpectedPolicy("new-policy", "duplicated profiles are ignored.", true, Arrays.asList("ordinal-test-profile", "lack-of-builtin-field-test-profile"), rep);
});
// update existing profiles
String modifiedProfileDescription = "The profile has been updated.";
ClientProfilesRepresentation actualProfilesRep = getProfilesWithoutGlobals();
ClientProfilesBuilder profilesBuilder = new ClientProfilesBuilder();
actualProfilesRep.getProfiles().stream().forEach(i -> {
if (i.getName().equals("ordinal-test-profile")) {
i.setDescription(modifiedProfileDescription);
}
profilesBuilder.addProfile(i);
});
updateProfiles(profilesBuilder.toString());
assertExpectedLoadedProfiles((ClientProfilesRepresentation reps) -> {
ClientProfileRepresentation rep = getProfileRepresentation(reps, "ordinal-test-profile", false);
assertExpectedProfile(rep, "ordinal-test-profile", modifiedProfileDescription);
});
// update existing policies
String modifiedPolicyDescription = "The policy has also been updated.";
ClientPoliciesRepresentation actualPoliciesRep = getPolicies();
ClientPoliciesBuilder policiesBuilder = new ClientPoliciesBuilder();
actualPoliciesRep.getPolicies().stream().forEach(i -> {
if (i.getName().equals("new-policy")) {
i.setDescription(modifiedPolicyDescription);
i.setEnabled(null);
}
policiesBuilder.addPolicy(i);
});
updatePolicies(policiesBuilder.toString());
assertExpectedLoadedPolicies((ClientPoliciesRepresentation reps) -> {
ClientPolicyRepresentation rep = getPolicyRepresentation(reps, "new-policy");
assertExpectedPolicy("new-policy", modifiedPolicyDescription, false, Arrays.asList("ordinal-test-profile", "lack-of-builtin-field-test-profile"), rep);
});
}
use of org.keycloak.representations.idm.ClientPoliciesRepresentation in project keycloak by keycloak.
the class AbstractClientPoliciesTest method updatePolicies.
// TODO: Possibly change this to accept ClientPoliciesRepresentation instead of String to have more type-safety.
protected void updatePolicies(String json) throws ClientPolicyException {
try {
ClientPoliciesRepresentation clientPolicies = json == null ? null : JsonSerialization.readValue(json, ClientPoliciesRepresentation.class);
adminClient.realm(REALM_NAME).clientPoliciesPoliciesResource().updatePolicies(clientPolicies);
} catch (BadRequestException e) {
throw new ClientPolicyException("update policies failed", e.getResponse().getStatusInfo().toString());
} catch (IOException e) {
throw new ClientPolicyException("update policies failed", e.getMessage());
}
}
use of org.keycloak.representations.idm.ClientPoliciesRepresentation in project keycloak by keycloak.
the class AbstractClientPoliciesTest method addPolicy.
protected void addPolicy(ClientPolicyRepresentation policyRep) throws ClientPolicyException {
ClientPoliciesRepresentation reps = getPolicies();
if (reps == null || reps.getPolicies() == null)
return;
reps.getPolicies().add(policyRep);
updatePolicies(convertToPoliciesJson(reps));
return;
}
use of org.keycloak.representations.idm.ClientPoliciesRepresentation in project keycloak by keycloak.
the class AbstractClientPoliciesTest method updatePolicy.
protected void updatePolicy(ClientPolicyRepresentation policyRep) throws ClientPolicyException {
if (policyRep == null || policyRep.getName() == null)
return;
String policyName = policyRep.getName();
ClientPoliciesRepresentation reps = getPolicies();
if (reps.getPolicies().stream().anyMatch(i -> policyName.equals(i.getName()))) {
ClientPolicyRepresentation rep = reps.getPolicies().stream().filter(i -> policyName.equals(i.getName())).collect(Collectors.toList()).get(0);
reps.getPolicies().remove(rep);
reps.getPolicies().add(policyRep);
updatePolicies(convertToPoliciesJson(reps));
} else {
return;
}
}
use of org.keycloak.representations.idm.ClientPoliciesRepresentation in project keycloak by keycloak.
the class ClientPoliciesTest method testPoliciesJsonView.
@Test
public void testPoliciesJsonView() throws Exception {
clientPoliciesJsonPage.navigateTo();
assertEquals(new ClientPoliciesRepresentation(), clientPoliciesJsonPage.form().getPolicies());
ClientPoliciesRepresentation policies = new ClientPoliciesBuilder().addPolicy(new ClientPolicyBuilder().createPolicy("prof", "desc", false).addCondition(ClientAccessTypeConditionFactory.PROVIDER_ID, createClientAccessTypeConditionConfig(Arrays.asList(ClientAccessTypeConditionFactory.TYPE_CONFIDENTIAL, ClientAccessTypeConditionFactory.TYPE_BEARERONLY, ClientAccessTypeConditionFactory.TYPE_PUBLIC))).toRepresentation()).toRepresentation();
testRealmResource().clientPoliciesPoliciesResource().updatePolicies(policies);
refreshPageAndWaitForLoad();
assertEquals(policies, clientPoliciesJsonPage.form().getPolicies());
policies.getPolicies().add(new ClientPolicyBuilder().createPolicy("prof2", "desc2", true).toRepresentation());
clientPoliciesJsonPage.form().setPolicies(policies);
clientPoliciesJsonPage.form().save();
assertAlertSuccess();
assertClientPolicy(policies);
clientPoliciesJsonPage.form().setPoliciesAsString("aaa");
clientPoliciesJsonPage.form().save();
assertAlertDanger();
}
Aggregations