use of org.keycloak.representations.idm.authorization.ResourceRepresentation in project keycloak by keycloak.
the class ResourceManagementTest method failCreateWithSameNameDifferentOwner.
@Test
public void failCreateWithSameNameDifferentOwner() {
ResourceRepresentation martaResource = createResource("Resource A", "marta", null, null, null);
ResourceRepresentation koloResource = createResource("Resource A", "kolo", null, null, null);
assertNotNull(martaResource.getId());
assertNotNull(koloResource.getId());
assertNotEquals(martaResource.getId(), koloResource.getId());
assertEquals(2, getClientResource().authorization().resources().findByName(martaResource.getName()).size());
List<ResourceRepresentation> martaResources = getClientResource().authorization().resources().findByName(martaResource.getName(), "marta");
assertEquals(1, martaResources.size());
assertEquals(martaResource.getId(), martaResources.get(0).getId());
List<ResourceRepresentation> koloResources = getClientResource().authorization().resources().findByName(martaResource.getName(), "kolo");
assertEquals(1, koloResources.size());
assertEquals(koloResource.getId(), koloResources.get(0).getId());
}
use of org.keycloak.representations.idm.authorization.ResourceRepresentation in project keycloak by keycloak.
the class ResourceManagementTest method testUpdateScopes.
@Test
public void testUpdateScopes() {
ResourceRepresentation resource = createResourceWithDefaultScopes();
Set<ScopeRepresentation> scopes = new HashSet<>(resource.getScopes());
assertEquals(3, scopes.size());
assertTrue(scopes.removeIf(scopeRepresentation -> scopeRepresentation.getName().equals("Scope B")));
resource.setScopes(scopes);
ResourceRepresentation updated = doUpdateResource(resource);
assertEquals(2, resource.getScopes().size());
assertFalse(containsScope("Scope B", updated));
assertTrue(containsScope("Scope A", updated));
assertTrue(containsScope("Scope C", updated));
scopes = new HashSet<>(updated.getScopes());
assertTrue(scopes.removeIf(scopeRepresentation -> scopeRepresentation.getName().equals("Scope A")));
assertTrue(scopes.removeIf(scopeRepresentation -> scopeRepresentation.getName().equals("Scope C")));
updated.setScopes(scopes);
updated = doUpdateResource(updated);
assertEquals(0, updated.getScopes().size());
}
use of org.keycloak.representations.idm.authorization.ResourceRepresentation in project keycloak by keycloak.
the class ResourceManagementTest method createResourceWithDefaultScopes.
private ResourceRepresentation createResourceWithDefaultScopes() {
ResourceRepresentation resource = createResource();
assertEquals(0, resource.getScopes().size());
HashSet<ScopeRepresentation> scopes = new HashSet<>();
scopes.add(createScope("Scope A", "").toRepresentation());
scopes.add(createScope("Scope B", "").toRepresentation());
scopes.add(createScope("Scope C", "").toRepresentation());
resource.setScopes(scopes);
return doUpdateResource(resource);
}
use of org.keycloak.representations.idm.authorization.ResourceRepresentation in project keycloak by keycloak.
the class ResourceManagementTest method failCreateWithSameName.
@Test
public void failCreateWithSameName() {
ResourceRepresentation newResource = createResource();
try {
doCreateResource(newResource);
fail("Can not create resources with the same name and owner");
} catch (Exception e) {
assertEquals(HttpResponseException.class, e.getCause().getClass());
assertEquals(409, HttpResponseException.class.cast(e.getCause()).getStatusCode());
}
newResource.setName(newResource.getName() + " Another");
newResource = doCreateResource(newResource);
assertNotNull(newResource.getId());
assertEquals("Test Resource Another", newResource.getName());
}
use of org.keycloak.representations.idm.authorization.ResourceRepresentation in project keycloak by keycloak.
the class ResourceManagementTest method doCreateResource.
protected ResourceRepresentation doCreateResource(ResourceRepresentation newResource) {
ResourcesResource resources = getClientResource().authorization().resources();
try (Response response = resources.create(newResource)) {
int status = response.getStatus();
if (status != Response.Status.CREATED.getStatusCode()) {
throw new RuntimeException(new HttpResponseException("Error", status, "", null));
}
ResourceRepresentation stored = response.readEntity(ResourceRepresentation.class);
return resources.resource(stored.getId()).toRepresentation();
}
}
Aggregations