Search in sources :

Example 16 with Context

use of org.forgerock.services.context.Context in project OpenAM by OpenRock.

the class SmsRouteTreeTest method shouldUseProvidedAuthModuleForMatchingPath.

@Test
public void shouldUseProvidedAuthModuleForMatchingPath() throws Exception {
    //Given
    RequestHandler requestHandler = mock(RequestHandler.class);
    Context context = mock(Context.class);
    ReadRequest request = Requests.newReadRequest("/not-authorized/service");
    Promise<AuthorizationResult, ResourceException> failResult = newResultPromise(accessDenied("no"));
    given(authModule.authorizeRead(any(Context.class), any(ReadRequest.class))).willReturn(failResult);
    //When
    routeTree.handles("NOT_AUTHORIZED").addRoute(RoutingMode.STARTS_WITH, "/service", requestHandler);
    Promise<ResourceResponse, ResourceException> result = routeTree.handleRead(context, request);
    //Then
    assertThat(result).failedWithException();
    verify(authModule).authorizeRead(any(Context.class), any(ReadRequest.class));
    verifyNoMoreInteractions(requestHandler, defaultAuthModule);
}
Also used : Context(org.forgerock.services.context.Context) RequestHandler(org.forgerock.json.resource.RequestHandler) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) AuthorizationResult(org.forgerock.authz.filter.api.AuthorizationResult) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 17 with Context

use of org.forgerock.services.context.Context in project OpenAM by OpenRock.

the class TokenOwnerAuthzModuleTest method setupUser.

private Context setupUser(String finalId) throws SSOException {
    Principal mockPrincipal = mock(Principal.class);
    SSOTokenContext tc = mock(SSOTokenContext.class);
    Context mockContext = mock(Context.class);
    given(mockContext.asContext(SSOTokenContext.class)).willReturn(tc);
    SSOToken mockToken = mock(SSOToken.class);
    given(tc.getCallerSSOToken()).willReturn(mockToken);
    given(mockToken.getProperty(eq(Constants.UNIVERSAL_IDENTIFIER))).willReturn("uid" + finalId);
    given(mockTokenManager.createSSOToken(eq("token"))).willReturn(mockToken);
    given(mockToken.getPrincipal()).willReturn(mockPrincipal);
    given(mockPrincipal.getName()).willReturn(finalId);
    return mockContext;
}
Also used : Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOToken(com.iplanet.sso.SSOToken) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Principal(java.security.Principal)

Example 18 with Context

use of org.forgerock.services.context.Context in project OpenAM by OpenRock.

the class PolicyRequestFactoryTest method shouldRejectUnsupportedAction.

@Test(expectedExceptions = EntitlementException.class)
public void shouldRejectUnsupportedAction() throws EntitlementException {
    // Given...
    Context context = buildContextStructure("/abc");
    factory.buildRequest(PolicyAction.UNKNOWN, context, actionRequest);
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) Test(org.testng.annotations.Test)

Example 19 with Context

use of org.forgerock.services.context.Context in project OpenAM by OpenRock.

the class PolicyResourceEvaluationTest method shouldHandleEntitlementExceptions.

@Test
public void shouldHandleEntitlementExceptions() throws EntitlementException {
    // Given...
    given(request.getAction()).willReturn("evaluate");
    Context context = buildContextStructure("/abc");
    EntitlementException eE = new EntitlementException(EntitlementException.INVALID_VALUE);
    given(requestFactory.buildRequest(PolicyAction.EVALUATE, context, request)).willThrow(eE);
    given(request.getRequestType()).willReturn(RequestType.ACTION);
    // When...
    Promise<ActionResponse, ResourceException> promise = policyResource.actionCollection(context, request);
    // Then...
    verify(request).getAction();
    verify(requestFactory).buildRequest(PolicyAction.EVALUATE, context, request);
    verify(request).getRequestType();
    assertThat(promise).failedWithException().isInstanceOf(BadRequestException.class);
    verifyNoMoreInteractions(request, requestFactory, policyRequest, factory, evaluator, parser);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) EntitlementException(com.sun.identity.entitlement.EntitlementException) ResourceException(org.forgerock.json.resource.ResourceException) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Example 20 with Context

use of org.forgerock.services.context.Context in project OpenAM by OpenRock.

the class PolicyResourceEvaluationTest method shouldMakeBatchEvaluation.

@Test
public void shouldMakeBatchEvaluation() throws EntitlementException {
    // Given...
    given(request.getAction()).willReturn("evaluate");
    Context context = buildContextStructure("/abc");
    given(requestFactory.buildRequest(PolicyAction.EVALUATE, context, request)).willReturn(policyRequest);
    given(policyRequest.getRestSubject()).willReturn(restSubject);
    given(policyRequest.getApplication()).willReturn("some-application");
    given(factory.getEvaluator(restSubject, "some-application")).willReturn(evaluator);
    given(policyRequest.getApplication()).willReturn("some-application");
    given(policyRequest.getRealm()).willReturn("/abc");
    List<Entitlement> decisions = Arrays.asList(new Entitlement());
    given(evaluator.routePolicyRequest(policyRequest)).willReturn(decisions);
    JsonValue jsonDecision = JsonValue.json(array());
    given(parser.printEntitlements(decisions)).willReturn(jsonDecision);
    // When...
    Promise<ActionResponse, ResourceException> promise = policyResource.actionCollection(context, request);
    // Then...
    verify(request).getAction();
    verify(requestFactory).buildRequest(PolicyAction.EVALUATE, context, request);
    verify(policyRequest).getRestSubject();
    verify(policyRequest, times(2)).getApplication();
    verify(policyRequest).getRealm();
    verify(factory).getEvaluator(restSubject, "some-application");
    verify(evaluator).routePolicyRequest(policyRequest);
    verify(parser).printEntitlements(decisions);
    assertThat(promise).succeeded().withContent().isEqualTo(jsonDecision);
    verifyNoMoreInteractions(request, requestFactory, policyRequest, factory, evaluator, parser);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Entitlement(com.sun.identity.entitlement.Entitlement) ActionResponse(org.forgerock.json.resource.ActionResponse) Test(org.testng.annotations.Test)

Aggregations

Context (org.forgerock.services.context.Context)250 Test (org.testng.annotations.Test)231 RealmContext (org.forgerock.openam.rest.RealmContext)150 ResourceException (org.forgerock.json.resource.ResourceException)132 ClientContext (org.forgerock.services.context.ClientContext)120 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)101 ResourceResponse (org.forgerock.json.resource.ResourceResponse)82 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)76 RootContext (org.forgerock.services.context.RootContext)62 JsonValue (org.forgerock.json.JsonValue)56 QueryRequest (org.forgerock.json.resource.QueryRequest)54 ReadRequest (org.forgerock.json.resource.ReadRequest)50 AttributesContext (org.forgerock.services.context.AttributesContext)44 ActionRequest (org.forgerock.json.resource.ActionRequest)43 QueryResponse (org.forgerock.json.resource.QueryResponse)43 Subject (javax.security.auth.Subject)42 Matchers.anyString (org.mockito.Matchers.anyString)38 Request (org.forgerock.http.protocol.Request)37 UpdateRequest (org.forgerock.json.resource.UpdateRequest)36 DeleteRequest (org.forgerock.json.resource.DeleteRequest)35