Search in sources :

Example 41 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidScopeExceptionWhenNoScope.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidScopeExceptionWhenNoScope() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    ResourceSetDescription resourceSetDescription = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\"}");
    given(resourceSetStore.read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID")).willReturn(resourceSetDescription);
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_scope");
        assertThat(e.getMessage()).contains("Missing required attribute", "'scopes'");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Test(org.testng.annotations.Test)

Example 42 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class UmaPolicyTest method setup.

@BeforeMethod
public void setup() {
    resourceSet = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    resourceSet.setDescription(json(object(field("name", "NAME"))));
}
Also used : ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 43 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method shouldGetResourceSetWithoutPolicy.

@Test
public void shouldGetResourceSetWithoutPolicy() throws Exception {
    //Given
    Context context = mock(Context.class);
    String realm = "REALM";
    String resourceSetId = "RESOURCE_SET_ID";
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicy = false;
    ResourceSetDescription resourceSetDescription = mock(ResourceSetDescription.class);
    given(resourceSetStore.read(eq(resourceSetId), any(ResourceSetSharedFilter.class))).willReturn(resourceSetDescription);
    //When
    ResourceSetDescription resourceSet = service.getResourceSet(context, realm, resourceSetId, resourceOwnerId, augmentWithPolicy).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSet).isEqualTo(resourceSetDescription);
    verifyZeroInteractions(policyService);
    verify(resourceSet, never()).setPolicy(Matchers.<JsonValue>anyObject());
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetSharedFilter(org.forgerock.openam.uma.ResourceSetSharedFilter) Test(org.testng.annotations.Test)

Example 44 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method getResourceSetsShouldReturnSetWhenResourceSetsExistWithNoPolicyQuery.

@Test
public void getResourceSetsShouldReturnSetWhenResourceSetsExistWithNoPolicyQuery() throws Exception {
    //Given
    Context context = createContext();
    String realm = "REALM";
    ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicies = false;
    QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
    Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
    ResourceSetDescription resourceSetOne = new ResourceSetDescription("RS_ID_ONE", "CLIENT_ID_ONE", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    ResourceSetDescription resourceSetTwo = new ResourceSetDescription("RS_ID_TWO", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    mockResourceOwnerIdentity(resourceOwnerId, realm);
    mockFilteredResourceSetsQueryVisitor(resourceSetQuery, queriedResourceSets);
    query.setResourceSetQuery(resourceSetQuery);
    queriedResourceSets.add(resourceSetOne);
    queriedResourceSets.add(resourceSetTwo);
    given(resourceSetStore.query(resourceSetQuery)).willReturn(queriedResourceSets);
    Collection<UmaPolicy> queriedPolicies = new HashSet<UmaPolicy>();
    Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
    Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
    given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
    //When
    Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSets).hasSize(2).contains(resourceSetOne, resourceSetTwo);
    assertThat(resourceSetOne.getPolicy()).isNull();
    assertThat(resourceSetTwo.getPolicy()).isNull();
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 45 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method getResourceSetsShouldReturnEmptySetWhenResourceSetsExistQueryingByAndWithPolicies.

@Test
public void getResourceSetsShouldReturnEmptySetWhenResourceSetsExistQueryingByAndWithPolicies() throws Exception {
    //Given
    Context context = createContext();
    String realm = "REALM";
    ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicies = true;
    QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
    QueryFilter policyQuery = QueryFilter.alwaysFalse();
    Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
    ResourceSetDescription resourceSetOne = new ResourceSetDescription("RS_ID_ONE", "CLIENT_ID_ONE", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    ResourceSetDescription resourceSetTwo = new ResourceSetDescription("RS_ID_TWO", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    ResourceSetDescription resourceSetThree = new ResourceSetDescription("RS_ID_THREE", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    Collection<UmaPolicy> queriedPolicies = new HashSet<>();
    UmaPolicy policyOne = mock(UmaPolicy.class);
    UmaPolicy policyTwo = mock(UmaPolicy.class);
    UmaPolicy policyThree = mock(UmaPolicy.class);
    JsonValue policyOneJson = mock(JsonValue.class);
    JsonValue policyTwoJson = mock(JsonValue.class);
    JsonValue policyThreeJson = mock(JsonValue.class);
    Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
    Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
    query.setResourceSetQuery(resourceSetQuery);
    query.setPolicyQuery(policyQuery);
    query.setOperator(AggregateQuery.Operator.AND);
    queriedResourceSets.add(resourceSetOne);
    queriedResourceSets.add(resourceSetTwo);
    queriedPolicies.add(policyOne);
    queriedPolicies.add(policyThree);
    given(policyOne.getId()).willReturn("RS_ID_ONE");
    given(policyOne.asJson()).willReturn(policyOneJson);
    given(policyOne.getResourceSet()).willReturn(resourceSetOne);
    given(policyTwo.getId()).willReturn("RS_ID_TWO");
    given(policyTwo.asJson()).willReturn(policyTwoJson);
    given(policyTwo.getResourceSet()).willReturn(resourceSetTwo);
    given(policyThree.getId()).willReturn("RS_ID_THREE");
    given(policyThree.asJson()).willReturn(policyThreeJson);
    given(policyThree.getResourceSet()).willReturn(resourceSetThree);
    given(resourceSetStore.query(resourceSetQuery)).willReturn(queriedResourceSets);
    given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
    given(resourceSetStore.read("RS_ID_THREE", resourceOwnerId)).willReturn(resourceSetThree);
    mockPolicyEvaluator("RS_CLIENT_ID");
    AMIdentity amIdentity = mock(AMIdentity.class);
    given(amIdentity.getUniversalId()).willReturn("UNIVERSAL_ID");
    given(coreWrapper.getIdentity("RESOURCE_OWNER_ID", realm)).willReturn(amIdentity);
    given(resourceSetQuery.accept(any(QueryFilterVisitor.class), eq(queriedResourceSets))).willReturn(queriedResourceSets);
    //When
    Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSets).hasSize(1).contains(resourceSetOne);
    assertThat(resourceSetOne.getPolicy()).isEqualTo(policyOneJson);
    assertThat(resourceSetTwo.getPolicy()).isNull();
    assertThat(resourceSetThree.getPolicy()).isNull();
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) QueryFilterVisitor(org.forgerock.util.query.QueryFilterVisitor) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) QueryFilter(org.forgerock.util.query.QueryFilter) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) AMIdentity(com.sun.identity.idm.AMIdentity) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Aggregations

ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)59 Test (org.testng.annotations.Test)33 ResourceException (org.forgerock.json.resource.ResourceException)19 HashSet (java.util.HashSet)15 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)15 Context (org.forgerock.services.context.Context)14 JsonValue (org.forgerock.json.JsonValue)12 QueryResponse (org.forgerock.json.resource.QueryResponse)12 Collection (java.util.Collection)11 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)11 RealmContext (org.forgerock.openam.rest.RealmContext)11 HashMap (java.util.HashMap)10 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)10 RootContext (org.forgerock.services.context.RootContext)10 Pair (org.forgerock.util.Pair)10 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)9 QueryFilter (org.forgerock.util.query.QueryFilter)9 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)9 List (java.util.List)8 ResourceSetLabel (org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)8