Search in sources :

Example 61 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthorizationFilterTest method testRoleAllowed.

@Test
public void testRoleAllowed() throws IOException {
    // Given
    FF4jAuthorizationFilter faf = new FF4jAuthorizationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    UriInfo mockUriInfo = mock(UriInfo.class);
    ResourceInfo mockResInfo = new ResourceInfo() {

        public Method getResourceMethod() {
            return methodRole;
        }

        public Class<?> getResourceClass() {
            return targetResource;
        }
    };
    faf.setInfo(mockResInfo);
    when(mockUriInfo.getPath()).thenReturn("localhost");
    when(mockRequest.getSecurityContext()).thenReturn(new FF4jSecurityContext("user", "", Util.set("USER")));
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    // When
    faf.filter(mockRequest);
// Then expecte 403
}
Also used : FF4jAuthorizationFilter(org.ff4j.web.api.security.FF4jAuthorizationFilter) ResourceInfo(javax.ws.rs.container.ResourceInfo) FF4jSecurityContext(org.ff4j.web.api.security.FF4jSecurityContext) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 62 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterApiKeyExpired.

@Test(expected = WebApplicationException.class)
public void testFilterApiKeyExpired() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    mvm.putSingle(ApiKeyValidatorFilter.HEADER_APIKEY, "12");
    when(mockRequest.getHeaders()).thenReturn(mvm);
    // create valid KEY to test against
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("12");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() - 100000));
    initMap.put(ak1.getValue(), ak1);
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKey(org.ff4j.web.api.filter.ApiKey) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Example 63 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterOK.

@Test
public void testFilterOK() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    mvm.putSingle(ApiKeyValidatorFilter.HEADER_APIKEY, "12");
    when(mockRequest.getHeaders()).thenReturn(mvm);
    // create valid KEY to test against
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("12");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() + 100000));
    initMap.put(ak1.getValue(), ak1);
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKey(org.ff4j.web.api.filter.ApiKey) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Example 64 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterNondeader.

@Test(expected = WebApplicationException.class)
public void testFilterNondeader() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    when(mockRequest.getHeaders()).thenReturn(mvm);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Test(org.junit.Test)

Example 65 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthenticationFilterTest method testUnAuthorizedInvalidValue.

@Test(expected = WebApplicationException.class)
public void testUnAuthorizedInvalidValue() throws IOException {
    // Given
    FF4jAuthenticationFilter faf = new FF4jAuthenticationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    when(mockRequest.getHeaderString("Authorization")).thenReturn("12");
    when(mockRequest.getMethod()).thenReturn("GET");
    UriInfo mockUriInfo = mock(UriInfo.class);
    when(mockUriInfo.getPath()).thenReturn("someURL");
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    faf.filter(mockRequest);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) FF4jAuthenticationFilter(org.ff4j.web.api.security.FF4jAuthenticationFilter) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)81 Test (org.junit.Test)40 Response (javax.ws.rs.core.Response)32 UriInfo (javax.ws.rs.core.UriInfo)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Test (org.junit.jupiter.api.Test)18 Resource (org.glassfish.jersey.server.model.Resource)15 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)14 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 File (java.io.File)8 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)8 ResourceInfo (javax.ws.rs.container.ResourceInfo)8 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)8 IOException (java.io.IOException)7 URI (java.net.URI)6 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)6 FF4jAuthenticationFilter (org.ff4j.web.api.security.FF4jAuthenticationFilter)6 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 ApiKeyValidatorFilter (org.ff4j.web.api.filter.ApiKeyValidatorFilter)5