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;
}
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);
}
}
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());
}
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);
}
}
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;
}
Aggregations