use of org.keycloak.representations.idm.authorization.PolicyRepresentation in project keycloak by keycloak.
the class GenericPolicyManagementTest method testCreate.
@Test
public void testCreate() {
PolicyRepresentation newPolicy = createTestingPolicy().toRepresentation();
assertEquals("Test Generic Policy", newPolicy.getName());
assertEquals("scope", newPolicy.getType());
assertEquals(Logic.POSITIVE, newPolicy.getLogic());
assertEquals(DecisionStrategy.UNANIMOUS, newPolicy.getDecisionStrategy());
assertEquals("configuration for A", newPolicy.getConfig().get("configA"));
assertEquals("configuration for B", newPolicy.getConfig().get("configB"));
assertEquals("configuration for C", newPolicy.getConfig().get("configC"));
List<PolicyRepresentation> policies = getClientResource().authorization().policies().policies();
assertEquals(6, policies.size());
assertAssociatedPolicy("Test Associated A", newPolicy);
assertAssociatedPolicy("Test Associated B", newPolicy);
assertAssociatedPolicy("Test Associated C", newPolicy);
assertAssociatedResource("Test Resource A", newPolicy);
assertAssociatedResource("Test Resource B", newPolicy);
assertAssociatedResource("Test Resource C", newPolicy);
assertAssociatedScope("Test Scope A", newPolicy);
assertAssociatedScope("Test Scope B", newPolicy);
assertAssociatedScope("Test Scope C", newPolicy);
}
use of org.keycloak.representations.idm.authorization.PolicyRepresentation in project keycloak by keycloak.
the class GenericPolicyManagementTest method testQueryPolicyAllFields.
@Test
public void testQueryPolicyAllFields() {
AuthorizationResource authorization = getClientResource().authorization();
authorization.resources().create(new ResourceRepresentation("Resource A"));
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("Permission A");
permission.addResource("Resource A");
authorization.permissions().resource().create(permission);
List<PolicyRepresentation> policies = authorization.policies().policies(null, "Permission A", null, null, null, true, null, "*", -1, -1);
assertEquals(1, policies.size());
assertEquals(1, policies.get(0).getResourcesData().size());
policies = authorization.policies().policies(null, "Permission A", null, null, null, true, null, null, -1, -1);
assertEquals(1, policies.size());
assertNull(policies.get(0).getResourcesData());
}
use of org.keycloak.representations.idm.authorization.PolicyRepresentation in project keycloak by keycloak.
the class GenericPolicyManagementTest method testUpdate.
@Test
public void testUpdate() {
PolicyResource policyResource = createTestingPolicy();
PolicyRepresentation policy = policyResource.toRepresentation();
policy.setName("changed");
policy.setLogic(Logic.NEGATIVE);
policy.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
policy.getConfig().put("configA", "changed configuration for A");
policy.getConfig().remove("configB");
policy.getConfig().put("configC", "changed configuration for C");
policyResource.update(policy);
policy = policyResource.toRepresentation();
assertEquals("changed", policy.getName());
assertEquals(Logic.NEGATIVE, policy.getLogic());
assertEquals(DecisionStrategy.AFFIRMATIVE, policy.getDecisionStrategy());
assertEquals("changed configuration for A", policy.getConfig().get("configA"));
assertNull(policy.getConfig().get("configB"));
assertEquals("changed configuration for C", policy.getConfig().get("configC"));
Map<String, String> config = policy.getConfig();
config.put("applyPolicies", buildConfigOption(findPolicyByName("Test Associated C").getId()));
config.put("resources", buildConfigOption(findResourceByName("Test Resource B").getId()));
config.put("scopes", buildConfigOption(findScopeByName("Test Scope A").getId()));
policyResource.update(policy);
policy = policyResource.toRepresentation();
config = policy.getConfig();
assertAssociatedPolicy("Test Associated C", policy);
List<PolicyRepresentation> associatedPolicies = getClientResource().authorization().policies().policy(policy.getId()).associatedPolicies();
assertFalse(associatedPolicies.stream().filter(associated -> associated.getId().equals(findPolicyByName("Test Associated A").getId())).findFirst().isPresent());
assertFalse(associatedPolicies.stream().filter(associated -> associated.getId().equals(findPolicyByName("Test Associated B").getId())).findFirst().isPresent());
assertAssociatedResource("Test Resource B", policy);
List<ResourceRepresentation> resources = policyResource.resources();
assertFalse(resources.contains(findResourceByName("Test Resource A")));
assertFalse(resources.contains(findResourceByName("Test Resource C")));
assertAssociatedScope("Test Scope A", policy);
List<ScopeRepresentation> scopes = getClientResource().authorization().policies().policy(policy.getId()).scopes();
assertFalse(scopes.contains(findScopeByName("Test Scope B").getId()));
assertFalse(scopes.contains(findScopeByName("Test Scope C").getId()));
}
use of org.keycloak.representations.idm.authorization.PolicyRepresentation in project keycloak by keycloak.
the class GenericPolicyManagementTest method testQueryPolicyByIdAllFields.
@Test
public void testQueryPolicyByIdAllFields() {
PolicyResource policy = createTestingPolicy();
PolicyRepresentation representation = policy.toRepresentation("*");
Set<ResourceRepresentation> resources = representation.getResourcesData();
assertEquals(3, resources.size());
representation = policy.toRepresentation();
assertNull(representation.getResourcesData());
}
use of org.keycloak.representations.idm.authorization.PolicyRepresentation in project keycloak by keycloak.
the class RolePolicyManagementTest method testGenericConfig.
@Test
public void testGenericConfig() {
AuthorizationResource authorization = getClient().authorization();
RolePolicyRepresentation representation = new RolePolicyRepresentation();
representation.setName("Test Generic Config Permission");
representation.addRole("Role A", false);
RolePoliciesResource policies = authorization.policies().role();
try (Response response = policies.create(representation)) {
RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
PolicyResource policy = authorization.policies().policy(created.getId());
PolicyRepresentation genericConfig = policy.toRepresentation();
assertNotNull(genericConfig.getConfig());
assertNotNull(genericConfig.getConfig().get("roles"));
RoleRepresentation role = getRealm().roles().get("Role A").toRepresentation();
assertTrue(genericConfig.getConfig().get("roles").contains(role.getId()));
}
}
Aggregations