Search in sources :

Example 56 with CreateRequest

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

the class PrivilegeAuthzModuleTest method crestCreateIsAllowed.

@Test
public void crestCreateIsAllowed() 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.createInstance(isA(Context.class), isA(CreateRequest.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 CreateRequest request = Requests.newCreateRequest("/policies", JsonValue.json(new Object()));
    Promise<ResourceResponse, ResourceException> result = router.handleCreate(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) CreateRequest(org.forgerock.json.resource.CreateRequest) FilterChain(org.forgerock.json.resource.FilterChain) JsonValue(org.forgerock.json.JsonValue) Router(org.forgerock.json.resource.Router) Matchers.anyString(org.mockito.Matchers.anyString) 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 57 with CreateRequest

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

the class AuditFilterTest method shouldReturnNullForCreateSuccess.

@Test
public void shouldReturnNullForCreateSuccess() {
    CreateRequest createRequest = mock(CreateRequest.class);
    ResourceResponse resourceResponse = mock(ResourceResponse.class);
    JsonValue filterResponse = auditFilter.getCreateSuccessDetail(createRequest, resourceResponse);
    assertThat(filterResponse).isEqualTo(null);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 58 with CreateRequest

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

the class ScriptResourceTest method shouldFailIfNoNameIsSuppliedOnCreate.

@Test(expectedExceptions = BadRequestException.class)
public void shouldFailIfNoNameIsSuppliedOnCreate() throws ResourceException {
    // given
    JsonValue requestJson = json(object(field(SCRIPT_TEXT, encodeScript), field(SCRIPT_LANGUAGE, "JAVASCRIPT"), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
    CreateRequest createRequest = mock(CreateRequest.class);
    when(createRequest.getContent()).thenReturn(requestJson);
    // when
    scriptResource.createInstance(context, createRequest).getOrThrowUninterruptibly();
// then - exception
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 59 with CreateRequest

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

the class ScriptResourceTest method shouldCreateScriptConfigurationWithoutError.

@Test
public void shouldCreateScriptConfigurationWithoutError() throws Exception {
    // given
    JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_DESCRIPTION, "A test script configuration"), field(SCRIPT_TEXT, encodeScript), field(SCRIPT_LANGUAGE, "JAVASCRIPT"), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
    CreateRequest createRequest = mock(CreateRequest.class);
    when(createRequest.getContent()).thenReturn(requestJson);
    // when
    ResourceResponse response = scriptResource.createInstance(context, createRequest).getOrThrowUninterruptibly();
    // then
    JsonValue responseJson = response.getContent();
    assertEquals(responseJson.get(SCRIPT_NAME).asString(), "MyJavaScript");
    assertEquals(responseJson.get(SCRIPT_DESCRIPTION).asString(), "A test script configuration");
    assertEquals(responseJson.get(SCRIPT_TEXT).asString(), encodeScript);
    assertEquals(getLanguageFromString(responseJson.get(SCRIPT_LANGUAGE).asString()), JAVASCRIPT);
    assertEquals(getContextFromString(responseJson.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 60 with CreateRequest

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

the class ScriptResourceTest method shouldFailIfNoContextIsSuppliedOnCreate.

@Test(expectedExceptions = BadRequestException.class)
public void shouldFailIfNoContextIsSuppliedOnCreate() throws ResourceException {
    // given
    JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_TEXT, encodeScript), field(SCRIPT_LANGUAGE, "JAVASCRIPT")));
    CreateRequest createRequest = mock(CreateRequest.class);
    when(createRequest.getContent()).thenReturn(requestJson);
    // when
    scriptResource.createInstance(context, createRequest).getOrThrowUninterruptibly();
// then - exception
}
Also used : CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Aggregations

CreateRequest (org.forgerock.json.resource.CreateRequest)70 Test (org.testng.annotations.Test)62 ResourceResponse (org.forgerock.json.resource.ResourceResponse)42 JsonValue (org.forgerock.json.JsonValue)39 ResourceException (org.forgerock.json.resource.ResourceException)38 Context (org.forgerock.services.context.Context)27 DeleteRequest (org.forgerock.json.resource.DeleteRequest)19 ReadRequest (org.forgerock.json.resource.ReadRequest)18 RootContext (org.forgerock.services.context.RootContext)18 UriRouterContext (org.forgerock.http.routing.UriRouterContext)17 ActionRequest (org.forgerock.json.resource.ActionRequest)17 PatchRequest (org.forgerock.json.resource.PatchRequest)17 QueryRequest (org.forgerock.json.resource.QueryRequest)17 UpdateRequest (org.forgerock.json.resource.UpdateRequest)17 Request (org.forgerock.http.protocol.Request)16 AttributesContext (org.forgerock.services.context.AttributesContext)16 Subject (javax.security.auth.Subject)13 RealmContext (org.forgerock.openam.rest.RealmContext)11 EntitlementException (com.sun.identity.entitlement.EntitlementException)9 ResourceType (org.forgerock.openam.entitlement.ResourceType)9