Search in sources :

Example 81 with ResourceRepresentation

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());
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 82 with ResourceRepresentation

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());
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Arrays(java.util.Arrays) ResourceResource(org.keycloak.admin.client.resource.ResourceResource) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) HashMap(java.util.HashMap) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) NotFoundException(javax.ws.rs.NotFoundException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) HashSet(java.util.HashSet) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) List(java.util.List) Response(javax.ws.rs.core.Response) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) Assert.fail(org.junit.Assert.fail) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) Assert.assertEquals(org.junit.Assert.assertEquals) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with ResourceRepresentation

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);
}
Also used : ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) HashSet(java.util.HashSet)

Example 84 with ResourceRepresentation

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());
}
Also used : HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) NotFoundException(javax.ws.rs.NotFoundException) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 85 with ResourceRepresentation

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();
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Aggregations

ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)154 Test (org.junit.Test)96 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)49 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)45 AuthzClient (org.keycloak.authorization.client.AuthzClient)44 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)39 ClientResource (org.keycloak.admin.client.resource.ClientResource)38 Response (javax.ws.rs.core.Response)36 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)35 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)33 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)33 Permission (org.keycloak.representations.idm.authorization.Permission)28 ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)26 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)23 OAuthClient (org.keycloak.testsuite.util.OAuthClient)23 PermissionRequest (org.keycloak.representations.idm.authorization.PermissionRequest)22 AccessToken (org.keycloak.representations.AccessToken)19 ArrayList (java.util.ArrayList)18 List (java.util.List)18 TokenIntrospectionResponse (org.keycloak.authorization.client.representation.TokenIntrospectionResponse)18