use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testRegisterOAuthApplicationData.
@Test(dataProvider = "getRegisterOAuthApplicationData")
public void testRegisterOAuthApplicationData(String oauthVersion, String userName, String consumerKey, String consumerSecret) throws Exception {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super");
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(-1234);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserRealm(userRealm);
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
OAuthConsumerAppDTO oAuthConsumerAppDTO = new OAuthConsumerAppDTO();
oAuthConsumerAppDTO.setApplicationName("SAMPLE_APP1");
oAuthConsumerAppDTO.setCallbackUrl("http://localhost:8080/acsUrl");
oAuthConsumerAppDTO.setApplicationAccessTokenExpiryTime(1234585);
oAuthConsumerAppDTO.setGrantTypes("");
oAuthConsumerAppDTO.setUsername(userName);
oAuthConsumerAppDTO.setOauthConsumerKey(consumerKey);
oAuthConsumerAppDTO.setOauthConsumerSecret(consumerSecret);
oAuthConsumerAppDTO.setOAuthVersion(oauthVersion);
oAuthConsumerAppDTO.setRenewRefreshTokenEnabled("true");
whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
doNothing().when(oAuthAppDAO).addOAuthApplication(Matchers.any(OAuthAppDO.class));
try {
oAuthAdminServiceImpl.registerOAuthApplicationData(oAuthConsumerAppDTO);
} catch (IdentityOAuthAdminException e) {
if (StringUtils.isBlank(userName)) {
Assert.assertEquals("No authenticated user found. Failed to register OAuth App", e.getMessage());
return;
}
Assert.fail("Error while registering OAuth APP");
}
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testUpdateOauthSecretKey.
@Test
public void testUpdateOauthSecretKey() throws Exception {
mockStatic(OAuthUtil.class);
when(OAuthUtil.getRandomNumber()).thenReturn(UPDATED_CONSUMER_SECRET);
when(OAuthUtil.buildConsumerAppDTO(any())).thenCallRealMethod();
OAuthAdminServiceImpl oAuthAdminServiceImpl = spy(new OAuthAdminServiceImpl());
doNothing().when(oAuthAdminServiceImpl, "updateAppAndRevokeTokensAndAuthzCodes", anyString(), Matchers.any(Properties.class));
whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
OAuthAppDO oAuthAppDO = new OAuthAppDO();
oAuthAppDO.setOauthConsumerKey(CONSUMER_KEY);
oAuthAppDO.setOauthConsumerSecret(UPDATED_CONSUMER_SECRET);
AuthenticatedUser authenticatedUser = new AuthenticatedUser();
authenticatedUser.setUserName("test_user");
oAuthAppDO.setAppOwner(authenticatedUser);
when(oAuthAppDAO.getAppInformation(CONSUMER_KEY)).thenReturn(oAuthAppDO);
OAuthConsumerAppDTO oAuthConsumerAppDTO;
oAuthConsumerAppDTO = oAuthAdminServiceImpl.updateAndRetrieveOauthSecretKey(CONSUMER_KEY);
Assert.assertEquals(oAuthConsumerAppDTO.getOauthConsumerSecret(), UPDATED_CONSUMER_SECRET);
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testGetOAuthApplicationDataByAppNameException.
@Test(dataProvider = "getAppInformationExceptions", expectedExceptions = IdentityOAuthAdminException.class)
public void testGetOAuthApplicationDataByAppNameException(String exception) throws Exception {
String appName = "some-app-name";
switch(exception) {
case "InvalidOAuthClientException":
when(oAuthAppDAO.getAppInformationByAppName(appName)).thenThrow(InvalidOAuthClientException.class);
break;
case "IdentityOAuth2Exception":
when(oAuthAppDAO.getAppInformationByAppName(appName)).thenThrow(IdentityOAuth2Exception.class);
}
whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
oAuthAdminServiceImpl.getOAuthApplicationDataByAppName(appName);
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testGetOAuthApplicationDataByAppName.
@Test
public void testGetOAuthApplicationDataByAppName() throws Exception {
String appName = "some-app-name";
// Create oauth application data.
OAuthAppDO app = buildDummyOAuthAppDO("some-user-name");
when(oAuthAppDAO.getAppInformationByAppName(appName)).thenReturn(app);
whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
OAuthConsumerAppDTO oAuthConsumerApp = oAuthAdminServiceImpl.getOAuthApplicationDataByAppName(appName);
Assert.assertEquals(oAuthConsumerApp.getApplicationName(), app.getApplicationName(), "Application name should be same as the application name in app data object.");
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testUpdateConsumerApplication.
@Test(dataProvider = "getUpdateConsumerAppTestData")
public void testUpdateConsumerApplication(String loggedInUsername, String appOwnerInRequest, boolean appOwnerInRequestExists, String expectedAppOwnerAfterUpdate) throws Exception {
AuthenticatedUser loggedInUser = buildUser(loggedInUsername);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(loggedInUser.getTenantDomain());
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(IdentityTenantUtil.getTenantId(loggedInUser.getTenantDomain()));
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(loggedInUser.getUserName());
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserRealm(userRealm);
AuthenticatedUser appOwner = buildUser(appOwnerInRequest);
String tenantAwareUsernameOfAppOwner = MultitenantUtils.getTenantAwareUsername(appOwner.toFullQualifiedUsername());
when(userStoreManager.isExistingUser(tenantAwareUsernameOfAppOwner)).thenReturn(appOwnerInRequestExists);
String consumerKey = UUID.randomUUID().toString();
OAuthAppDO app = buildDummyOAuthAppDO("original-app-owner");
AuthenticatedUser originalOwner = app.getAppOwner();
OAuthAppDAO oAuthAppDAOMock = PowerMockito.spy(new OAuthAppDAO());
OAuthAppDO oAuthAppDO = new OAuthAppDO();
PowerMockito.doReturn(true).when(oAuthAppDAOMock, "validateUserForOwnerUpdate", oAuthAppDO);
when(oAuthAppDAO.getAppInformation(consumerKey)).thenReturn(app);
whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
OAuthConsumerAppDTO consumerAppDTO = new OAuthConsumerAppDTO();
consumerAppDTO.setApplicationName("new-application-name");
consumerAppDTO.setCallbackUrl("http://new-call-back-url.com");
consumerAppDTO.setOauthConsumerKey(consumerKey);
consumerAppDTO.setOauthConsumerSecret("some-consumer-secret");
consumerAppDTO.setOAuthVersion("new-oauth-version");
consumerAppDTO.setUsername(appOwner.toFullQualifiedUsername());
oAuthAdminServiceImpl.updateConsumerApplication(consumerAppDTO);
OAuthConsumerAppDTO updatedOAuthConsumerApp = oAuthAdminServiceImpl.getOAuthApplicationData(consumerKey);
Assert.assertEquals(updatedOAuthConsumerApp.getApplicationName(), consumerAppDTO.getApplicationName(), "Updated Application name should be same as the application name in consumerAppDTO data object.");
Assert.assertEquals(updatedOAuthConsumerApp.getCallbackUrl(), consumerAppDTO.getCallbackUrl(), "Updated Application callbackUrl should be same as the callbackUrl in consumerAppDTO data object.");
if (appOwnerInRequestExists) {
// Application update should change the app owner if the app owner sent in the request is a valid user.
Assert.assertNotEquals(updatedOAuthConsumerApp.getUsername(), originalOwner.toFullQualifiedUsername());
}
Assert.assertEquals(updatedOAuthConsumerApp.getUsername(), expectedAppOwnerAfterUpdate);
}
Aggregations