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