Search in sources :

Example 51 with Context

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

the class PendingRequestsServiceTest method shouldSendEmailOnPendingRequestApproval.

@Test
public void shouldSendEmailOnPendingRequestApproval() throws Exception {
    //Given
    Context context = mock(Context.class);
    createPendingRequest(PENDING_REQUEST_ID, RESOURCE_SET_ID, RESOURCE_SET_NAME, RESOURCE_OWNER_ID, REALM, REQUESTING_PARTY_ID, Collections.singleton(SCOPE));
    given(settings.isEmailRequestingPartyOnPendingRequestApprovalEnabled()).willReturn(true);
    mockPendingRequestApprovalEmailTemplate(REQUESTING_PARTY_ID, REALM);
    mockSuccessfulPolicyCreationForPendingRequest();
    JsonValue content = json(object());
    //When
    service.approvePendingRequest(context, PENDING_REQUEST_ID, content, REALM);
    //Then
    verify(policyService).createPolicy(eq(context), any(JsonValue.class));
    verify(emailService).email(REALM, REQUESTING_PARTY_ID, "APPROVAL_SUBJECT", "APPROVAL_BODY " + RESOURCE_OWNER_ID + " " + RESOURCE_SET_NAME + " " + SCOPE);
    verify(store).delete(PENDING_REQUEST_ID);
    verify(auditLogger).log(RESOURCE_SET_ID, RESOURCE_SET_NAME, resourceOwnerIdentity, UmaAuditType.REQUEST_APPROVED, REQUESTING_PARTY_ID);
}
Also used : Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 52 with Context

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

the class PendingRequestsServiceTest method shouldApprovePendingRequestUsingScopesFromRequestContent.

@Test
public void shouldApprovePendingRequestUsingScopesFromRequestContent() throws Exception {
    //Given
    Context context = mock(Context.class);
    createPendingRequest(PENDING_REQUEST_ID, RESOURCE_SET_ID, RESOURCE_SET_NAME, RESOURCE_OWNER_ID, REALM, REQUESTING_PARTY_ID, Collections.singleton(SCOPE));
    mockSuccessfulPolicyCreationForPendingRequest();
    JsonValue content = json(object(field("scopes", array("SCOPE_A", "SCOPE_B"))));
    //When
    service.approvePendingRequest(context, PENDING_REQUEST_ID, content, REALM);
    //Then
    ArgumentCaptor<JsonValue> policyCaptor = ArgumentCaptor.forClass(JsonValue.class);
    verify(policyService).createPolicy(eq(context), policyCaptor.capture());
    JsonValue policy = policyCaptor.getValue();
    assertThat(policy).stringAt("policyId").isEqualTo(RESOURCE_SET_ID);
    assertThat(policy).hasArray("permissions").hasSize(1);
    assertThat(policy).stringAt("permissions/0/subject").isEqualTo(REQUESTING_PARTY_ID);
    assertThat(policy).hasArray("permissions/0/scopes").containsOnly("SCOPE_A", "SCOPE_B");
    verify(store).delete(PENDING_REQUEST_ID);
    verify(auditLogger).log(RESOURCE_SET_ID, RESOURCE_SET_NAME, resourceOwnerIdentity, UmaAuditType.REQUEST_APPROVED, REQUESTING_PARTY_ID);
}
Also used : Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 53 with Context

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

the class AuditTestUtils method mockAuditContext.

public static Context mockAuditContext() throws Exception {
    final Context httpContext = new HttpContext(jsonFromFile("/org/forgerock/openam/rest/fluent/httpContext.json"), AbstractAuditFilterTest.class.getClassLoader());
    final Subject callerSubject = new Subject();
    final Context securityContext = new SecurityContext(httpContext, null, null);
    final Context subjectContext = new SSOTokenContext(mock(Debug.class), null, securityContext) {

        @Override
        public Subject getCallerSubject() {
            return callerSubject;
        }

        @Override
        public SSOToken getCallerSSOToken() {
            SSOToken token = mock(SSOToken.class);
            try {
                given(token.getProperty(Constants.AM_CTX_ID)).willReturn("TRACKING_ID");
                given(token.getProperty(Constants.UNIVERSAL_IDENTIFIER)).willReturn("USER_ID");
            } catch (SSOException e) {
            // won't happen - it's a mock
            }
            return token;
        }
    };
    final Context clientContext = ClientContext.newInternalClientContext(subjectContext);
    return new RequestAuditContext(new AuditInfoContext(clientContext, AuditConstants.Component.AUDIT));
}
Also used : SecurityContext(org.forgerock.services.context.SecurityContext) HttpContext(org.forgerock.json.resource.http.HttpContext) AuditInfoContext(org.forgerock.openam.rest.resource.AuditInfoContext) Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) SSOToken(com.iplanet.sso.SSOToken) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) HttpContext(org.forgerock.json.resource.http.HttpContext) SecurityContext(org.forgerock.services.context.SecurityContext) SSOException(com.iplanet.sso.SSOException) AuditInfoContext(org.forgerock.openam.rest.resource.AuditInfoContext) Subject(javax.security.auth.Subject) Debug(com.sun.identity.shared.debug.Debug)

Example 54 with Context

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

the class ApplicationTypesResourceTest method shouldReadInstanceCorrectly.

@Test
public void shouldReadInstanceCorrectly() throws IllegalAccessException, InstantiationException, ExecutionException, InterruptedException {
    //given
    SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSubjectContext);
    Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
    Subject subject = new Subject();
    given(mockSubjectContext.getCallerSubject()).willReturn(subject);
    ReadRequest request = mock(ReadRequest.class);
    ApplicationType mockApplicationType = new ApplicationType("test", null, null, null, null);
    given(mockApplicationTypeManager.getApplicationType(subject, "test")).willReturn(mockApplicationType);
    //when
    Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request);
    //then
    assertTrue(result.get().getId().equals("test"));
}
Also used : Context(org.forgerock.services.context.Context) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) ApplicationType(com.sun.identity.entitlement.ApplicationType) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Subject(javax.security.auth.Subject) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 55 with Context

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

the class PolicyResourceDelegateTest method shouldHandleFailureToDeleteFailedCreationOfPolicies.

@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToDeleteFailedCreationOfPolicies() throws ResourceException {
    //Given
    //Given
    Context context = mock(Context.class);
    Set<JsonValue> policies = new HashSet<JsonValue>();
    JsonValue policyOne = json(object(field("name", "POLICY_ONE")));
    JsonValue policyTwo = json(object(field("name", "POLICY_TWO")));
    policies.add(policyOne);
    policies.add(policyTwo);
    ResourceResponse createdPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
    ResourceException createException = mock(ResourceException.class);
    ResourceException deleteException = mock(ResourceException.class);
    Promise<ResourceResponse, ResourceException> createPolicyOnePromise = Promises.newResultPromise(createdPolicyOne);
    Promise<ResourceResponse, ResourceException> createPolicyTwoPromise = Promises.newExceptionPromise(createException);
    Promise<ResourceResponse, ResourceException> deletePolicyOnePromise = Promises.newExceptionPromise(deleteException);
    given(policyResource.handleCreate(eq(context), Matchers.<CreateRequest>anyObject())).willReturn(createPolicyOnePromise).willReturn(createPolicyTwoPromise);
    given(policyResource.handleDelete(eq(context), Matchers.<DeleteRequest>anyObject())).willReturn(deletePolicyOnePromise);
    //When
    try {
        delegate.createPolicies(context, policies).getOrThrowUninterruptibly();
    } catch (ResourceException e) {
        //Then
        ArgumentCaptor<DeleteRequest> requestCaptor = ArgumentCaptor.forClass(DeleteRequest.class);
        verify(policyResource).handleDelete(eq(context), requestCaptor.capture());
        assertThat(requestCaptor.getValue().getResourcePathObject().leaf()).isEqualTo("ID_1");
        assertThat(e).isEqualTo(deleteException);
        throw e;
    }
}
Also used : Context(org.forgerock.services.context.Context) ArgumentCaptor(org.mockito.ArgumentCaptor) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) DeleteRequest(org.forgerock.json.resource.DeleteRequest) HashSet(java.util.HashSet) 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