Search in sources :

Example 11 with OAuthAdminServiceImpl

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");
    }
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 12 with OAuthAdminServiceImpl

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);
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Properties(java.util.Properties) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 13 with OAuthAdminServiceImpl

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);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 14 with OAuthAdminServiceImpl

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.");
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 15 with OAuthAdminServiceImpl

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);
}
Also used : OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 Test (org.testng.annotations.Test)13 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)13 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)10 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)9 Matchers.anyString (org.mockito.Matchers.anyString)7 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)5 OAuthAdminServiceImpl (org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl)3 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)2 OAuthAppDAO (org.wso2.carbon.identity.oauth.dao.OAuthAppDAO)2 OAuthAppRevocationRequestDTO (org.wso2.carbon.identity.oauth.dto.OAuthAppRevocationRequestDTO)2 OAuthRevocationResponseDTO (org.wso2.carbon.identity.oauth.dto.OAuthRevocationResponseDTO)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 GrantType (org.wso2.carbon.identity.api.server.application.management.v1.GrantType)1 GrantTypeMetaData (org.wso2.carbon.identity.api.server.application.management.v1.GrantTypeMetaData)1 MetadataProperty (org.wso2.carbon.identity.api.server.application.management.v1.MetadataProperty)1