Search in sources :

Example 1 with UmaPolicy

use of org.forgerock.openam.uma.UmaPolicy in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldSuccessfullyCreatePolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldSuccessfullyCreatePolicy() {
    //Given
    Context context = mock(Context.class);
    CreateRequest request = Requests.newCreateRequest("/policies", json(object()));
    UmaPolicy policy = mock(UmaPolicy.class);
    Promise<UmaPolicy, ResourceException> promise = Promises.newResultPromise(policy);
    given(policy.getId()).willReturn("ID");
    given(policy.getRevision()).willReturn("REVISION");
    given(policyService.createPolicy(context, request.getContent())).willReturn(promise);
    //When
    Promise<ResourceResponse, ResourceException> result = policyResource.createInstance(context, request);
    //Then
    assertThat(result).succeeded().withId().isEqualTo("ID");
    assertThat(result).succeeded().withRevision().isEqualTo("REVISION");
    assertThat(result).succeeded().withContent().isEmpty();
}
Also used : Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Test(org.testng.annotations.Test)

Example 2 with UmaPolicy

use of org.forgerock.openam.uma.UmaPolicy in project OpenAM by OpenRock.

the class UmaPolicyServiceImplDelegationTest method bobShouldNotBeAbleToCreatePolicyForResourceNotSharedByAlice.

@Test
public void bobShouldNotBeAbleToCreatePolicyForResourceNotSharedByAlice() throws Exception {
    //Given
    userIsLoggedIn("bob", "REALM");
    accessingUriForUser("bob");
    String resourceSetId = registerResourceSet("alice");
    JsonValue policy = policyToCreate(resourceSetId);
    Context context = getContext();
    //When
    Promise<UmaPolicy, ResourceException> promise = policyService.createPolicy(context, policy);
    //Then
    assertThat(promise).failedWithException().isInstanceOf(ForbiddenException.class);
    verifyPolicyIsNotCreatedForLoggedInUser();
    verifyAuditLogNotCreatedForLoggedInUser(resourceSetId);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Test(org.testng.annotations.Test) UmaPolicyServiceImplTest(org.forgerock.openam.uma.rest.UmaPolicyServiceImplTest)

Example 3 with UmaPolicy

use of org.forgerock.openam.uma.UmaPolicy in project OpenAM by OpenRock.

the class UmaPolicyServiceImplDelegationTest method bobShouldNotBeAbleToUpdatePolicyForResourceWithMoreScopesThanSharedByAlice.

@Test
public void bobShouldNotBeAbleToUpdatePolicyForResourceWithMoreScopesThanSharedByAlice() throws Exception {
    //Given
    userIsLoggedIn("bob", "REALM");
    accessingUriForUser("bob");
    String resourceSetId = registerResourceSet("alice");
    createPolicyFor("bob", resourceSetId, "SCOPE_B");
    createPolicyFor("charlie", resourceSetId, "SCOPE_A", "SCOPE_B");
    JsonValue policy = policyToUpdate(resourceSetId);
    Context context = getContext();
    //When
    Promise<UmaPolicy, ResourceException> promise = policyService.updatePolicy(context, resourceSetId, policy);
    //Then
    assertThat(promise).failedWithException().isInstanceOf(ForbiddenException.class);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Test(org.testng.annotations.Test) UmaPolicyServiceImplTest(org.forgerock.openam.uma.rest.UmaPolicyServiceImplTest)

Example 4 with UmaPolicy

use of org.forgerock.openam.uma.UmaPolicy in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldSuccessfullyQueryPolicies.

@Test
public void shouldSuccessfullyQueryPolicies() {
    //Given
    Context context = mock(Context.class);
    QueryRequest request = Requests.newQueryRequest("/policies");
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
    QueryResponse queryResult = newQueryResponse();
    Collection<UmaPolicy> umaPolicies = new HashSet<UmaPolicy>();
    UmaPolicy policy1 = mock(UmaPolicy.class);
    UmaPolicy policy2 = mock(UmaPolicy.class);
    umaPolicies.add(policy1);
    umaPolicies.add(policy2);
    Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> promise = Promises.newResultPromise(Pair.of(queryResult, umaPolicies));
    given(policyService.queryPolicies(context, request)).willReturn(promise);
    //When
    policyResource.queryCollection(context, request, handler);
    //Then
    verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
Also used : Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 5 with UmaPolicy

use of org.forgerock.openam.uma.UmaPolicy in project OpenAM by OpenRock.

the class UmaPolicyResourceTest method shouldHandledFailedUpdatePolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldHandledFailedUpdatePolicy() {
    //Given
    Context context = mock(Context.class);
    UpdateRequest request = Requests.newUpdateRequest("/policies", json(object()));
    ResourceException resourceException = mock(ResourceException.class);
    Promise<UmaPolicy, ResourceException> promise = Promises.newExceptionPromise(resourceException);
    given(policyService.updatePolicy(context, "RESOURCE_SET_UID", request.getContent())).willReturn(promise);
    //When
    Promise<ResourceResponse, ResourceException> result = policyResource.updateInstance(context, "RESOURCE_SET_UID", request);
    //Then
    assertThat(result).failedWithResourceException().isEqualTo(resourceException);
}
Also used : Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Test(org.testng.annotations.Test)

Aggregations

UmaPolicy (org.forgerock.openam.uma.UmaPolicy)34 ResourceException (org.forgerock.json.resource.ResourceException)33 Context (org.forgerock.services.context.Context)28 Test (org.testng.annotations.Test)28 RealmContext (org.forgerock.openam.rest.RealmContext)21 JsonValue (org.forgerock.json.JsonValue)17 QueryResponse (org.forgerock.json.resource.QueryResponse)15 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)15 Pair (org.forgerock.util.Pair)15 ResourceResponse (org.forgerock.json.resource.ResourceResponse)13 HashSet (java.util.HashSet)12 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)12 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)11 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)11 ClientContext (org.forgerock.services.context.ClientContext)11 Collection (java.util.Collection)10 RootContext (org.forgerock.services.context.RootContext)9 UmaPolicyServiceImplTest (org.forgerock.openam.uma.rest.UmaPolicyServiceImplTest)8 Matchers.anyString (org.mockito.Matchers.anyString)8 ArrayList (java.util.ArrayList)7