Search in sources :

Example 1 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaPolicyTest method shouldCreateUmaPolicyFromUnderlyingPolicies.

@Test
public void shouldCreateUmaPolicyFromUnderlyingPolicies() throws BadRequestException {
    //Given
    Set<ResourceResponse> policies = createUnderlyingPolicies("RESOURCE_OWNER_ID");
    //When
    UmaPolicy umaPolicy = UmaPolicy.fromUnderlyingPolicies(resourceSet, policies);
    //Then
    assertThat(umaPolicy.getId()).isEqualTo("RESOURCE_SET_ID");
    assertThat(umaPolicy.getRevision()).isNotNull();
    assertThat(umaPolicy.asJson().asMap()).hasSize(3).contains(entry("policyId", "RESOURCE_SET_ID"), entry("name", "NAME"));
    JsonValue permissions = umaPolicy.asJson().get("permissions");
    assertThat(permissions.asList()).hasSize(2);
    assertThat(permissions.get(0).asMap()).contains(entry("subject", "SUBJECT_ONE"));
    assertThat(permissions.get(0).get("scopes").asList()).containsOnly("SCOPE_A", "SCOPE_B");
    assertThat(permissions.get(1).asMap()).contains(entry("subject", "SUBJECT_TWO"));
    assertThat(permissions.get(1).get("scopes").asList()).containsOnly("SCOPE_A");
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 2 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class UmaEnabledFilterTest method testFilterPatch.

@Test(dataProvider = "factories")
public void testFilterPatch(UmaProviderSettingsFactory factory, boolean expectFailure) throws Exception {
    // Given
    UmaEnabledFilter filter = new UmaEnabledFilter(factory);
    PatchRequest request = Requests.newPatchRequest("test", "test");
    // When
    Promise<ResourceResponse, ResourceException> promise = filter.filterPatch(context, request, requestHandler);
    // Then
    checkResult(expectFailure, promise);
    if (expectFailure) {
        verifyNoMoreInteractions(requestHandler);
    } else {
        verify(requestHandler).handlePatch(context, request);
    }
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) PatchRequest(org.forgerock.json.resource.PatchRequest) Test(org.testng.annotations.Test)

Example 3 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class PrivilegeAuthzModuleTest method crestPatchIsAllowed.

@Test
public void crestPatchIsAllowed() throws SSOException, DelegationException {
    // Given...
    final Set<String> actions = new HashSet<>(Arrays.asList("MODIFY"));
    final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS, DUMB_FUNC);
    given(factory.newInstance("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS)).willReturn(permission);
    given(subjectContext.getCallerSSOToken()).willReturn(token);
    given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(true);
    JsonValue jsonValue = json(object(field("someKey", "someValue")));
    Promise<ResourceResponse, ResourceException> promise = Promises.newResultPromise(Responses.newResourceResponse("1", "1.0", jsonValue));
    given(provider.patchInstance(isA(Context.class), eq("123"), isA(PatchRequest.class))).willReturn(promise);
    // When...
    final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
    final Router router = new Router();
    router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
    final RealmContext context = new RealmContext(subjectContext);
    context.setSubRealm("abc", "abc");
    final PatchRequest request = Requests.newPatchRequest("/policies/123", PatchOperation.add("abc", "123"));
    Promise<ResourceResponse, ResourceException> result = router.handlePatch(context, request);
    // Then...
    assertThat(result).succeeded().withContent().stringAt("someKey").isEqualTo("someValue");
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) Context(org.forgerock.services.context.Context) RealmContext(org.forgerock.openam.rest.RealmContext) FilterChain(org.forgerock.json.resource.FilterChain) JsonValue(org.forgerock.json.JsonValue) Router(org.forgerock.json.resource.Router) Matchers.anyString(org.mockito.Matchers.anyString) PatchRequest(org.forgerock.json.resource.PatchRequest) DelegationPermission(com.sun.identity.delegation.DelegationPermission) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class PrivilegeAuthzModuleTest method crestRequestNotAllowed.

@Test
public void crestRequestNotAllowed() throws SSOException, DelegationException {
    // Given...
    final Set<String> actions = new HashSet<>(Arrays.asList("MODIFY"));
    final DelegationPermission permission = new DelegationPermission("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS, DUMB_FUNC);
    given(factory.newInstance("/abc", "rest", "1.0", "policies", "modify", actions, EXTENSIONS)).willReturn(permission);
    given(subjectContext.getCallerSSOToken()).willReturn(token);
    given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(false);
    // When...
    final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module);
    final Router router = new Router();
    router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain);
    final RealmContext context = new RealmContext(subjectContext);
    context.setSubRealm("abc", "abc");
    final CreateRequest request = Requests.newCreateRequest("/policies", JsonValue.json(new Object()));
    Promise<ResourceResponse, ResourceException> promise = router.handleCreate(context, request);
    // Then...
    assertThat(promise).failedWithException().isInstanceOf(ForbiddenException.class);
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) FilterChain(org.forgerock.json.resource.FilterChain) Router(org.forgerock.json.resource.Router) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) DelegationPermission(com.sun.identity.delegation.DelegationPermission) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with ResourceResponse

use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.

the class RestRouterIT method shouldBeAbleToReachInternalViaInternalRouter.

@Test
public void shouldBeAbleToReachInternalViaInternalRouter() throws Exception {
    // Given
    Promise<ResourceResponse, ResourceException> promise = newResultPromise(newResourceResponse("1", "1", json(object())));
    given(internalResource.readInstance(any(Context.class), eq("123"), any(ReadRequest.class))).willReturn(promise);
    Router internalRouter = InjectorHolder.getInstance(Key.get(Router.class, Names.named("InternalCrestRouter")));
    Context context = mockRequiredContexts();
    ReadRequest request = Requests.newReadRequest("internal/123");
    // When
    internalRouter.handleRead(context, request);
    // Then
    verify(internalResource).readInstance(any(Context.class), eq("123"), any(ReadRequest.class));
}
Also used : RootContext(org.forgerock.services.context.RootContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) HttpContext(org.forgerock.json.resource.http.HttpContext) SessionContext(org.forgerock.http.session.SessionContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) AttributesContext(org.forgerock.services.context.AttributesContext) SecurityContext(org.forgerock.services.context.SecurityContext) Context(org.forgerock.services.context.Context) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) Router(org.forgerock.json.resource.Router) ResourceException(org.forgerock.json.resource.ResourceException) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

ResourceResponse (org.forgerock.json.resource.ResourceResponse)227 ResourceException (org.forgerock.json.resource.ResourceException)171 Test (org.testng.annotations.Test)154 JsonValue (org.forgerock.json.JsonValue)123 Context (org.forgerock.services.context.Context)83 RealmContext (org.forgerock.openam.rest.RealmContext)72 Subject (javax.security.auth.Subject)57 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)48 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)44 CreateRequest (org.forgerock.json.resource.CreateRequest)41 ClientContext (org.forgerock.services.context.ClientContext)41 ArrayList (java.util.ArrayList)38 EntitlementException (com.sun.identity.entitlement.EntitlementException)30 ReadRequest (org.forgerock.json.resource.ReadRequest)30 NotFoundException (org.forgerock.json.resource.NotFoundException)29 UpdateRequest (org.forgerock.json.resource.UpdateRequest)29 Matchers.anyString (org.mockito.Matchers.anyString)28 HashSet (java.util.HashSet)27 BadRequestException (org.forgerock.json.resource.BadRequestException)26 DeleteRequest (org.forgerock.json.resource.DeleteRequest)26