Search in sources :

Example 16 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project carbon-apimgt by wso2.

the class ImportApiServiceImplTestCase method getRequest.

// Sample request to be used by tests
private Request getRequest() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
    Request request = new Request(carbonMessage);
    return request;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Example 17 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project carbon-apimgt by wso2.

the class BasicAuthAuthenticatorTestCase method testAuthenticate.

@Test
public void testAuthenticate() throws Exception {
    final String authorizationHttpHeader = "Basic YWRtaW46YWRtaW4=";
    final String authorizationHttpHeader1 = "DummyHeader YWRtaW46YWRtaW4=";
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Request requestObj = new Request(carbonMessage);
    try {
        PowerMockito.whenNew(Request.class).withArguments(carbonMessage).thenReturn(requestObj);
    } catch (Exception e) {
        throw new APIMgtSecurityException("Error while mocking Request Object ", e);
    }
    try {
        BasicAuthAuthenticator basicAuthAuthenticator = new BasicAuthAuthenticator();
        basicAuthAuthenticator.authenticate(requestObj, null, null);
    } catch (APIMgtSecurityException e) {
        Assert.assertEquals(e.getMessage(), "Missing Authorization header in the request.`");
    }
    when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn(authorizationHttpHeader1);
    Response responseObj = Mockito.mock(Response.class);
    ServiceMethodInfo serviceMethodInfoObj = Mockito.mock(ServiceMethodInfo.class);
    try {
        BasicAuthAuthenticator basicAuthAuthenticator = new BasicAuthAuthenticator();
        basicAuthAuthenticator.authenticate(requestObj, responseObj, serviceMethodInfoObj);
    } catch (APIMgtSecurityException e) {
        Assert.assertEquals(e.getMessage(), "Missing 'Authorization : Basic' header in the request.`");
    }
    when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn(authorizationHttpHeader);
    BasicAuthAuthenticator basicAuthAuthenticator = new BasicAuthAuthenticator();
    boolean isAuthenticated = basicAuthAuthenticator.authenticate(requestObj, responseObj, serviceMethodInfoObj);
    if (isAuthenticated) {
        Assert.assertEquals(isAuthenticated, true);
    } else {
        Assert.assertEquals(isAuthenticated, false);
    }
}
Also used : Response(org.wso2.msf4j.Response) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BasicAuthAuthenticator(org.wso2.carbon.apimgt.rest.api.common.impl.BasicAuthAuthenticator) APIMgtSecurityException(org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) Request(org.wso2.msf4j.Request) APIMgtSecurityException(org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException) Test(org.testng.annotations.Test)

Example 18 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project carbon-apimgt by wso2.

the class OAuth2AuthenticatorTestCase method testOauthAuthenticate.

@Test
public void testOauthAuthenticate() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Request requestObj = new Request(carbonMessage);
    Response responseObj = Mockito.mock(Response.class);
    ServiceMethodInfo serviceMethodInfoObj = Mockito.mock(ServiceMethodInfo.class);
    final String authorizationHttpHeader = "Bearer 7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
    final String accessToken = "7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
    Mockito.when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn(authorizationHttpHeader);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setTokenValid(true);
    accessTokenInfo.setEndUserName("admin@carbon.super");
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getIdentityProvider()).thenReturn(identityProvider);
    Mockito.when(identityProvider.getTokenMetaData(accessToken)).thenReturn(accessTokenInfo);
    when((String) requestObj.getProperty(APIConstants.REQUEST_URL)).thenReturn("/api/am/publisher/");
    OAuth2Authenticator oAuth2Authenticator = new OAuth2Authenticator();
    oAuth2Authenticator.authenticate(requestObj, responseObj, serviceMethodInfoObj);
    Assert.assertEquals(0, responseObj.getStatusCode());
}
Also used : Response(org.wso2.msf4j.Response) AccessTokenInfo(org.wso2.carbon.apimgt.core.models.AccessTokenInfo) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) OAuth2Authenticator(org.wso2.carbon.apimgt.rest.api.common.impl.OAuth2Authenticator) Request(org.wso2.msf4j.Request) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project carbon-apimgt by wso2.

the class RESTAPISecurityInterceptorTestCase method testGetApisSuccess.

@Test
public void testGetApisSuccess() throws APIManagementException {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Request requestObj = Mockito.mock(Request.class);
    try {
        PowerMockito.whenNew(Request.class).withArguments(carbonMessage).thenReturn(requestObj);
    } catch (Exception e) {
        throw new APIMgtSecurityException("Error while mocking Request Object ", e);
    }
    Response responseObj = Mockito.mock(Response.class);
    Mockito.when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn("Authorization:  053d68ee-12bc-36b0-ab9c-31752ef5bda9");
    Mockito.when(requestObj.getHeader("REQUEST_URL")).thenReturn("http://localhost:9090/api/am/publisher/v1/api");
    ServiceMethodInfo serviceMethodInfoObj = Mockito.mock(ServiceMethodInfo.class);
    RESTAPISecurityInterceptor interceptor = Mockito.mock(RESTAPISecurityInterceptor.class);
    boolean isAuthorized = interceptor.preCall(requestObj, responseObj, serviceMethodInfoObj);
    if (isAuthorized) {
        Assert.assertEquals(isAuthorized, true);
    } else {
        Assert.assertEquals(isAuthorized, false);
    }
}
Also used : Response(org.wso2.msf4j.Response) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) APIMgtSecurityException(org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) Request(org.wso2.msf4j.Request) RESTAPISecurityInterceptor(org.wso2.carbon.apimgt.rest.api.common.interceptors.RESTAPISecurityInterceptor) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIMgtSecurityException(org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException) Test(org.junit.Test)

Example 20 with HTTPCarbonMessage

use of org.wso2.transport.http.netty.message.HTTPCarbonMessage in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method getRequest.

private Request getRequest() throws Exception {
    HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
    Request request = new Request(carbonMessage);
    PowerMockito.whenNew(Request.class).withArguments(carbonMessage).thenReturn(request);
    return request;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Request(org.wso2.msf4j.Request)

Aggregations

Request (org.wso2.msf4j.Request)34 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)34 APIMgtSecurityException (org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 Response (org.wso2.msf4j.Response)3 ServiceMethodInfo (org.wso2.msf4j.ServiceMethodInfo)3 Test (org.junit.Test)2 NotFoundException (org.wso2.carbon.apimgt.rest.api.admin.NotFoundException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)1 AccessTokenInfo (org.wso2.carbon.apimgt.core.models.AccessTokenInfo)1 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException)1 BasicAuthAuthenticator (org.wso2.carbon.apimgt.rest.api.common.impl.BasicAuthAuthenticator)1 OAuth2Authenticator (org.wso2.carbon.apimgt.rest.api.common.impl.OAuth2Authenticator)1 RESTAPISecurityInterceptor (org.wso2.carbon.apimgt.rest.api.common.interceptors.RESTAPISecurityInterceptor)1