Search in sources :

Example 26 with Request

use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.

the class AbstractHttpAccessAuditFilterTest method shouldAuditAccessAttemptAndResult.

@Test(dataProvider = "handlerResponses")
public void shouldAuditAccessAttemptAndResult(Status responseStatus) throws AuditException {
    //Given
    Context context = new RequestAuditContext(mockContext());
    Request request = new Request().setMethod("GET").setUri(URI.create("http://example.com:8080?query=value"));
    request.getHeaders().put(ContentTypeHeader.valueOf("CONTENT_TYPE"));
    enableAccessTopicAuditing();
    Handler handler = mockHandler(context, request, responseStatus);
    //When
    auditFilter.filter(context, request, handler);
    //Then
    ArgumentCaptor<AuditEvent> auditEventCaptor = ArgumentCaptor.forClass(AuditEvent.class);
    verify(eventPublisher, times(2)).tryPublish(eq(AuditConstants.ACCESS_TOPIC), auditEventCaptor.capture());
    verifyAccessAttemptAuditEvent(auditEventCaptor.getAllValues().get(0).getValue());
    if (responseStatus.isSuccessful()) {
        verifyAccessSuccessAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    } else {
        verifyAccessFailedAuditEvent(auditEventCaptor.getAllValues().get(1).getValue());
    }
}
Also used : RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Context(org.forgerock.services.context.Context) AuditRequestContext(org.forgerock.openam.audit.context.AuditRequestContext) SessionContext(org.forgerock.http.session.SessionContext) AttributesContext(org.forgerock.services.context.AttributesContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) AuditEvent(org.forgerock.audit.events.AuditEvent) Test(org.testng.annotations.Test)

Example 27 with Request

use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.

the class AbstractHttpAccessAuditFilterTest method shouldNotAuditIfAuditingIsNotEnabledForAccessTopic.

@Test(dataProvider = "handlerResponses")
public void shouldNotAuditIfAuditingIsNotEnabledForAccessTopic(Status responseStatus) throws AuditException {
    //Given
    Context context = new RequestAuditContext(mockContext());
    Request request = new Request().setUri(URI.create("http://example.com"));
    disableAccessTopicAuditing();
    Handler handler = mockHandler(context, request, responseStatus);
    //When
    auditFilter.filter(context, request, handler);
    //Then
    verify(eventPublisher, never()).tryPublish(anyString(), any(AuditEvent.class));
}
Also used : RootContext(org.forgerock.services.context.RootContext) ClientContext(org.forgerock.services.context.ClientContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Context(org.forgerock.services.context.Context) AuditRequestContext(org.forgerock.openam.audit.context.AuditRequestContext) SessionContext(org.forgerock.http.session.SessionContext) AttributesContext(org.forgerock.services.context.AttributesContext) RequestAuditContext(org.forgerock.services.context.RequestAuditContext) Request(org.forgerock.http.protocol.Request) Handler(org.forgerock.http.Handler) AuditEvent(org.forgerock.audit.events.AuditEvent) Test(org.testng.annotations.Test)

Example 28 with Request

use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldReturnUnauthorizedCodeWithJsonErrorMessage.

@Test
public void shouldReturnUnauthorizedCodeWithJsonErrorMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    RestAuthException exception = new RestAuthException(401, "Invalid Password!!");
    exception.setFailureUrl("http://localhost:8080");
    // when
    Response response = authServiceV1.handleErrorResponse(httpRequest, Status.valueOf(401), exception);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).stringAt("errorMessage").isEqualTo("Invalid Password!!");
    assertThat(responseBody).stringAt("failureUrl").isEqualTo("http://localhost:8080");
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Response(org.forgerock.http.protocol.Response) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 29 with Request

use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldReturnErrorMessageWithoutTemplate.

@Test
public void shouldReturnErrorMessageWithoutTemplate() throws IOException {
    // given
    Request httpRequest = new Request();
    AuthLoginException ale = new AuthLoginException("amAuth", "119", null);
    RestAuthException exception = new RestAuthException(401, ale);
    // when
    String message = authServiceV1.getLocalizedMessage(httpRequest, exception);
    // then
    assertThat(message).isEqualTo("Invalid Auth Level.");
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Request(org.forgerock.http.protocol.Request) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) Test(org.testng.annotations.Test)

Example 30 with Request

use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldReturnFrenchErrorMessageFromException.

@Test
public void shouldReturnFrenchErrorMessageFromException() throws IOException {
    // given
    Request httpRequest = new Request();
    AuthLoginException exception = new AuthLoginException("amAuth", "120", null);
    httpRequest.getHeaders().put("Accept-Language", "fr-fr");
    // when
    String message = authServiceV1.getLocalizedMessage(httpRequest, exception);
    // then
    assertThat(message).isEqualTo("L’authentification sur module n’est pas autorisée.");
}
Also used : Request(org.forgerock.http.protocol.Request) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) Test(org.testng.annotations.Test)

Aggregations

Request (org.forgerock.http.protocol.Request)51 Test (org.testng.annotations.Test)45 Context (org.forgerock.services.context.Context)36 RootContext (org.forgerock.services.context.RootContext)33 AttributesContext (org.forgerock.services.context.AttributesContext)30 ReadRequest (org.forgerock.json.resource.ReadRequest)29 ActionRequest (org.forgerock.json.resource.ActionRequest)21 CreateRequest (org.forgerock.json.resource.CreateRequest)21 DeleteRequest (org.forgerock.json.resource.DeleteRequest)21 PatchRequest (org.forgerock.json.resource.PatchRequest)21 QueryRequest (org.forgerock.json.resource.QueryRequest)21 UpdateRequest (org.forgerock.json.resource.UpdateRequest)21 UriRouterContext (org.forgerock.http.routing.UriRouterContext)19 Response (org.forgerock.http.protocol.Response)14 SessionContext (org.forgerock.http.session.SessionContext)11 Handler (org.forgerock.http.Handler)10 RequestAuditContext (org.forgerock.services.context.RequestAuditContext)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpContext (org.forgerock.json.resource.http.HttpContext)7 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7