Search in sources :

Example 21 with ServiceURL

use of org.wso2.carbon.identity.core.ServiceURL in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpointTest method mockServiceURLBuilder.

private void mockServiceURLBuilder() throws URLBuilderException {
    ServiceURLBuilder builder = new ServiceURLBuilder() {

        String path = "";

        @Override
        public ServiceURLBuilder addPath(String... strings) {
            Arrays.stream(strings).forEach(x -> {
                path += "/" + x;
            });
            return this;
        }

        @Override
        public ServiceURLBuilder addParameter(String s, String s1) {
            return this;
        }

        @Override
        public ServiceURLBuilder setFragment(String s) {
            return this;
        }

        @Override
        public ServiceURLBuilder addFragmentParameter(String s, String s1) {
            return this;
        }

        @Override
        public ServiceURL build() throws URLBuilderException {
            ServiceURL serviceURL = mock(ServiceURL.class);
            when(serviceURL.getAbsolutePublicURL()).thenReturn("https://localhost:9443" + path);
            when(serviceURL.getRelativeInternalURL()).thenReturn(path);
            return serviceURL;
        }
    };
    mockStatic(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(builder);
}
Also used : ServiceURL(org.wso2.carbon.identity.core.ServiceURL) Matchers.anyString(org.mockito.Matchers.anyString) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder)

Example 22 with ServiceURL

use of org.wso2.carbon.identity.core.ServiceURL in project identity-inbound-auth-oauth by wso2-extensions.

the class EndpointUtilTest method mockServiceURLBuilder.

private void mockServiceURLBuilder(String url) throws URLBuilderException {
    mockStatic(ServiceURLBuilder.class);
    ServiceURLBuilder serviceURLBuilder = mock(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    ServiceURL serviceURL = mock(ServiceURL.class);
    when(serviceURL.getAbsolutePublicURL()).thenReturn(url);
    when(serviceURLBuilder.build()).thenReturn(serviceURL);
}
Also used : ServiceURL(org.wso2.carbon.identity.core.ServiceURL) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder)

Example 23 with ServiceURL

use of org.wso2.carbon.identity.core.ServiceURL in project identity-inbound-auth-oauth by wso2-extensions.

the class UserAuthenticationEndpointTest method testDeviceAuthorizeForIOExceptionPath.

/**
 * Test device endpoint throwing IOException.
 *
 * @param userCode      User code of the user.
 * @param clientId      Consumer key of the application.
 * @param expectedValue Expected http status.
 * @param status        Status of user code.
 * @param uri           Redirection uri.
 * @throws Exception Error while testing device endpoint throwing IOException.
 */
@Test(dataProvider = "providePostParamsForIOExceptionPath")
public void testDeviceAuthorizeForIOExceptionPath(String userCode, String clientId, int expectedValue, String status, String uri) throws Exception {
    mockOAuthServerConfiguration();
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, "oAuth2AuthzEndpoint", oAuth2AuthzEndpoint);
    mockStatic(IdentityDatabaseUtil.class);
    when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
    mockStatic(DeviceFlowPersistenceFactory.class);
    when(DeviceFlowPersistenceFactory.getInstance()).thenReturn(deviceFlowPersistenceFactory);
    when(deviceFlowPersistenceFactory.getDeviceFlowDAO()).thenReturn(deviceFlowDAO);
    when(deviceFlowDAO.getClientIdByUserCode(anyString())).thenReturn(clientId);
    when(deviceFlowDAO.getDetailsForUserCode(anyString())).thenReturn(deviceFlowDOAsExpired);
    when(deviceFlowDAO.getScopesForUserCode(anyString())).thenReturn(scopes);
    when(httpServletRequest.getParameter(anyString())).thenReturn(userCode);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getAppInformationByClientId(anyString())).thenReturn(oAuthAppDO);
    when(oAuthAppDO.getCallbackUrl()).thenReturn(uri);
    Response response1;
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    mockStatic(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addParameter(any(), any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.build()).thenReturn(serviceURL);
    when(serviceURL.getAbsolutePublicURL()).thenReturn(TEST_URL);
    Mockito.doThrow(new IOException("Throwing IOException.")).when(httpServletResponse).sendRedirect(TEST_URL);
    when(oAuth2AuthzEndpoint.authorize(any(CommonAuthRequestWrapper.class), any(HttpServletResponse.class))).thenReturn(response);
    DeviceAuthServiceImpl deviceAuthService = new DeviceAuthServiceImpl();
    userAuthenticationEndpoint = new UserAuthenticationEndpoint();
    userAuthenticationEndpoint.setDeviceAuthService(deviceAuthService);
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, OAuth2AuthzEndpoint.class, oAuth2AuthzEndpoint);
    response1 = userAuthenticationEndpoint.deviceAuthorize(httpServletRequest, httpServletResponse);
    if (expectedValue == HttpServletResponse.SC_ACCEPTED) {
        Assert.assertNotNull(response1);
    } else {
        Assert.assertNull(response1);
    }
}
Also used : Response(javax.ws.rs.core.Response) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) CommonAuthRequestWrapper(org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DeviceAuthServiceImpl(org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with ServiceURL

use of org.wso2.carbon.identity.core.ServiceURL in project identity-inbound-auth-oauth by wso2-extensions.

the class UserAuthenticationEndpointTest method testDeviceAuthorize.

/**
 * Test device endpoint.
 *
 * @param userCode      User code of the user.
 * @param clientId      Consumer key of the application.
 * @param expectedValue Expected http status.
 * @param status        Status of user code.
 * @param uri           Redirection uri.
 * @throws Exception Error while testing device endpoint.
 */
@Test(dataProvider = "providePostParams")
public void testDeviceAuthorize(String userCode, String clientId, int expectedValue, String status, String uri) throws Exception {
    mockOAuthServerConfiguration();
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, "oAuth2AuthzEndpoint", oAuth2AuthzEndpoint);
    mockStatic(IdentityDatabaseUtil.class);
    when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
    mockStatic(DeviceFlowPersistenceFactory.class);
    when(DeviceFlowPersistenceFactory.getInstance()).thenReturn(deviceFlowPersistenceFactory);
    when(deviceFlowPersistenceFactory.getDeviceFlowDAO()).thenReturn(deviceFlowDAO);
    when(deviceFlowDAO.getClientIdByUserCode(anyString())).thenReturn(clientId);
    when(deviceFlowDAO.getDetailsForUserCode(anyString())).thenReturn(deviceFlowDOAsNotExpired);
    when(deviceFlowDAO.getScopesForUserCode(anyString())).thenReturn(scopes);
    when(httpServletRequest.getParameter(anyString())).thenReturn(userCode);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getAppInformationByClientId(anyString())).thenReturn(oAuthAppDO);
    when(oAuthAppDO.getCallbackUrl()).thenReturn(uri);
    Response response1;
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    mockStatic(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addParameter(any(), any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.build()).thenReturn(serviceURL);
    when(serviceURL.getAbsolutePublicURL()).thenReturn(TEST_URL);
    when(oAuth2AuthzEndpoint.authorize(any(CommonAuthRequestWrapper.class), any(HttpServletResponse.class))).thenReturn(response);
    DeviceAuthServiceImpl deviceAuthService = new DeviceAuthServiceImpl();
    userAuthenticationEndpoint = new UserAuthenticationEndpoint();
    userAuthenticationEndpoint.setDeviceAuthService(deviceAuthService);
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, OAuth2AuthzEndpoint.class, oAuth2AuthzEndpoint);
    response1 = userAuthenticationEndpoint.deviceAuthorize(httpServletRequest, httpServletResponse);
    if (expectedValue == HttpServletResponse.SC_ACCEPTED) {
        Assert.assertNotNull(response1);
    } else {
        Assert.assertNull(response1);
    }
}
Also used : Response(javax.ws.rs.core.Response) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) CommonAuthRequestWrapper(org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeviceAuthServiceImpl(org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

OMElement (org.apache.axiom.om.OMElement)6 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)3 Iterator (java.util.Iterator)3 Properties (java.util.Properties)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Response (javax.ws.rs.core.Response)3 QName (javax.xml.namespace.QName)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 ServiceClient (org.apache.axis2.client.ServiceClient)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 ServiceURL (org.wso2.carbon.identity.core.ServiceURL)3 ServiceURLBuilder (org.wso2.carbon.identity.core.ServiceURLBuilder)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2