Search in sources :

Example 6 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder 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 7 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder 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 8 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder 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 9 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder 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

ServiceURLBuilder (org.wso2.carbon.identity.core.ServiceURLBuilder)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Response (javax.ws.rs.core.Response)4 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)4 BeforeTest (org.testng.annotations.BeforeTest)4 ServiceURL (org.wso2.carbon.identity.core.ServiceURL)4 DeviceAuthServiceImpl (org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl)4 CommonAuthRequestWrapper (org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper)3 URLBuilderException (org.wso2.carbon.identity.core.URLBuilderException)3 IOException (java.io.IOException)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Matchers.anyString (org.mockito.Matchers.anyString)1 OAuthClientAuthnContext (org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)1