Search in sources :

Example 1 with OAuth2Authenticator

use of org.wso2.carbon.apimgt.rest.api.common.impl.OAuth2Authenticator 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)

Aggregations

Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)1 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)1 AccessTokenInfo (org.wso2.carbon.apimgt.core.models.AccessTokenInfo)1 OAuth2Authenticator (org.wso2.carbon.apimgt.rest.api.common.impl.OAuth2Authenticator)1 Request (org.wso2.msf4j.Request)1 Response (org.wso2.msf4j.Response)1 ServiceMethodInfo (org.wso2.msf4j.ServiceMethodInfo)1 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)1