Search in sources :

Example 6 with MockUserStoreManager

use of org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager in project carbon-identity-framework by wso2.

the class ApplicationManagementServiceImplTest method setupConfiguration.

private void setupConfiguration() throws UserStoreException, RegistryException {
    String carbonHome = Paths.get(System.getProperty("user.dir"), "target", "test-classes", "repository").toString();
    System.setProperty(CarbonBaseConstants.CARBON_HOME, carbonHome);
    System.setProperty(CarbonBaseConstants.CARBON_CONFIG_DIR_PATH, Paths.get(carbonHome, "conf").toString());
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(SUPER_TENANT_DOMAIN_NAME);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(SUPER_TENANT_ID);
    PrivilegedCarbonContext.getThreadLocalCarbonContext();
    // Configure RealmService.
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(SUPER_TENANT_ID);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(SUPER_TENANT_DOMAIN_NAME);
    InMemoryRealmService testSessionRealmService = new InMemoryRealmService(SUPER_TENANT_ID);
    UserStoreManager userStoreManager = testSessionRealmService.getTenantUserRealm(SUPER_TENANT_ID).getUserStoreManager();
    ((MockUserStoreManager) userStoreManager).addSecondaryUserStoreManager("PRIMARY", (MockUserStoreManager) userStoreManager);
    IdentityTenantUtil.setRealmService(testSessionRealmService);
    RegistryDataHolder.getInstance().setRealmService(testSessionRealmService);
    OSGiDataHolder.getInstance().setUserRealmService(testSessionRealmService);
    IdentityCoreServiceDataHolder.getInstance().setRealmService(testSessionRealmService);
    ApplicationManagementServiceComponentHolder holder = ApplicationManagementServiceComponentHolder.getInstance();
    setInstanceValue(testSessionRealmService, RealmService.class, ApplicationManagementServiceComponentHolder.class, holder);
    // Configure Registry Service.
    RegistryService mockRegistryService = mock(RegistryService.class);
    UserRegistry mockRegistry = mock(UserRegistry.class);
    when(mockRegistryService.getGovernanceUserRegistry(anyString(), anyInt())).thenReturn(mockRegistry);
    OSGiDataHolder.getInstance().setRegistryService(mockRegistryService);
    CarbonCoreDataHolder.getInstance().setRegistryService(mockRegistryService);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setRegistry(RegistryType.USER_GOVERNANCE, mockRegistryService.getRegistry());
    when(mockRegistry.resourceExists(anyString())).thenReturn(FALSE);
    Collection mockPermissionNode = mock(Collection.class);
    when(mockRegistry.newCollection()).thenReturn(mockPermissionNode);
    when(mockRegistry.get(anyString())).thenReturn(mockPermissionNode);
}
Also used : MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) ApplicationManagementServiceComponentHolder(org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponentHolder) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Collection(org.wso2.carbon.registry.core.Collection) Matchers.anyString(org.mockito.Matchers.anyString) MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) InMemoryRealmService(org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)

Example 7 with MockUserStoreManager

use of org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager in project carbon-identity-framework by wso2.

the class ApplicationMgtUtilTest method testIsUserAuthorized.

@Test(dataProvider = "userAuthorizeDataProvider")
public void testIsUserAuthorized(String applicationName, String userName, String allowRoleValidationProperty, String[] userRoles, int applicationId, Boolean expected) throws UserStoreException, IdentityApplicationManagementException {
    validateRole(allowRoleValidationProperty);
    mockStatic(ApplicationMgtSystemConfig.class);
    ApplicationDAO mockApplicationDAO = mock(ApplicationDAO.class);
    ApplicationMgtSystemConfig mockAppMgtSystemConfig = mock(ApplicationMgtSystemConfig.class);
    when(ApplicationMgtSystemConfig.getInstance()).thenReturn(mockAppMgtSystemConfig);
    when(mockAppMgtSystemConfig.getApplicationDAO()).thenReturn(mockApplicationDAO);
    when(mockApplicationDAO.getApplicationName(anyInt())).thenReturn(APPLICATION_NAME);
    mockUserStoreManager();
    when(mockUserStoreManager.getRoleListOfUser(userName)).thenReturn(userRoles);
    assertEquals(ApplicationMgtUtil.isUserAuthorized(applicationName, userName), expected);
    assertEquals(ApplicationMgtUtil.isUserAuthorized(applicationName, userName, applicationId), expected);
}
Also used : ApplicationDAO(org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with MockUserStoreManager

use of org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager in project carbon-identity-framework by wso2.

the class ApplicationMgtUtilTest method testDeleteAppRoleUserStoreException.

@Test
public void testDeleteAppRoleUserStoreException() throws UserStoreException {
    mockUserStoreManager();
    doThrow(new UserStoreException("")).when(mockUserStoreManager).deleteRole(ROLE_NAME);
    assertThrows(IdentityApplicationManagementException.class, () -> ApplicationMgtUtil.deleteAppRole(APPLICATION_NAME));
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with MockUserStoreManager

use of org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager in project carbon-identity-framework by wso2.

the class ApplicationMgtUtilTest method testCreateAppRoleUserStoreException.

@Test
public void testCreateAppRoleUserStoreException() throws UserStoreException {
    mockUserStoreManager();
    UserStoreException mockUserStoreException = mock(UserStoreException.class);
    doThrow(mockUserStoreException).when(mockUserStoreManager).addRole(ROLE_NAME, new String[] { USERNAME }, null);
    when(mockUserStoreException.getMessage()).thenReturn(String.format(ERROR_CODE_ROLE_ALREADY_EXISTS.getMessage() + CODE, ROLE_NAME));
    doThrow(new UserStoreException("")).when(mockUserStoreManager).updateRoleListOfUser(USERNAME, null, new String[] { ROLE_NAME });
    when(mockUserStoreManager.getRoleListOfUser(USERNAME)).thenReturn(new String[] { ROLE_NAME });
    try {
        ApplicationMgtUtil.createAppRole(APPLICATION_NAME, USERNAME);
    } catch (IdentityApplicationManagementException e) {
        assertEquals(e.getMessage(), "Error while updating application role: " + ROLE_NAME + " with user " + USERNAME);
    }
}
Also used : IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 BeforeTest (org.testng.annotations.BeforeTest)5 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 InMemoryRealmService (org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)2 MockUserStoreManager (org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager)2 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)2 DataSource (javax.sql.DataSource)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Mockito.anyString (org.mockito.Mockito.anyString)1 ITestClass (org.testng.ITestClass)1 AbstractFrameworkTest (org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest)1 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)1 PostAuthnHandlerFlowStatus (org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthnHandlerFlowStatus)1 StepBasedSequenceHandler (org.wso2.carbon.identity.application.authentication.framework.handler.sequence.StepBasedSequenceHandler)1 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)1 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)1 User (org.wso2.carbon.identity.application.common.model.User)1 ApplicationDAO (org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO)1 ApplicationManagementServiceComponentHolder (org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponentHolder)1