Search in sources :

Example 1 with MockUserStoreManager

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

the class MockRealm method init.

@Override
public void init(RealmConfiguration realmConfiguration, Map<String, ClaimMapping> claimMappingMap, Map<String, ProfileConfiguration> map1, int tenantId) throws UserStoreException {
    this.realmConfiguration = realmConfiguration;
    this.tenantId = tenantId;
    DataSource dataSource = MockInitialContextFactory.initializeDatasource(UM_DB_JNDI_NAME, this.getClass(), new String[] { UM_DB_SQL_FILE });
    userStoreManager = new MockUserStoreManager(dataSource);
    ((MockUserStoreManager) this.userStoreManager).setRealmConfiguration(this.realmConfiguration);
    claimManager = new MockClaimManager(claimMappingMap);
    ((MockUserStoreManager) this.userStoreManager).setClaimManager(this.claimManager);
    HybridRoleManager hybridRoleManager = new MockHybridRoleManager(dataSource, tenantId, realmConfiguration, this);
    ((MockUserStoreManager) this.userStoreManager).setHybridRoleManager(hybridRoleManager);
}
Also used : HybridRoleManager(org.wso2.carbon.user.core.hybrid.HybridRoleManager) DataSource(javax.sql.DataSource)

Example 2 with MockUserStoreManager

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

the class ApplicationMgtUtilTest method testIsUserAuthorizedUserStoreException.

@Test
public void testIsUserAuthorizedUserStoreException() throws UserStoreException {
    mockUserStoreManager();
    doThrow(new UserStoreException("")).when(mockUserStoreManager).getRoleListOfUser(anyString());
    assertThrows(IdentityApplicationManagementException.class, () -> ApplicationMgtUtil.isUserAuthorized(APPLICATION_NAME, USERNAME));
}
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 3 with MockUserStoreManager

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

the class ApplicationMgtUtilTest method testIsValidApplicationOwner.

@Test(dataProvider = "validApplicationOwnerDataProvider")
public void testIsValidApplicationOwner(String username, String tenantDomain, Boolean hasOwner, Boolean expected) throws IdentityApplicationManagementException, UserStoreException {
    ServiceProvider serviceProvider = new ServiceProvider();
    if (hasOwner) {
        User user = new User();
        user.setUserName(username);
        user.setTenantDomain(tenantDomain);
        serviceProvider.setOwner(user);
    }
    mockUserStoreManager();
    when(mockUserStoreManager.isExistingUser(anyString())).thenReturn(FALSE);
    when(mockUserRealm.getRealmConfiguration()).thenReturn(mockRealmConfiguration);
    when(mockRealmConfiguration.getAdminUserName()).thenReturn("admin");
    when(mockRealmConfiguration.getUserStoreProperty(anyString())).thenReturn("property");
    when(mockCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    assertEquals(ApplicationMgtUtil.isValidApplicationOwner(serviceProvider), expected);
}
Also used : User(org.wso2.carbon.identity.application.common.model.User) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with MockUserStoreManager

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

the class CarbonBasedTestListener method createRealmService.

private RealmService createRealmService(WithRealmService withRealmService, boolean reuseIfAvailable) throws UserStoreException {
    if (testSessionRealmService != null && reuseIfAvailable) {
        return testSessionRealmService;
    }
    try {
        int tenantId = withRealmService.tenantId();
        String tenantDomain = withRealmService.tenantDomain();
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        testSessionRealmService = new InMemoryRealmService(tenantId);
        UserStoreManager userStoreManager = testSessionRealmService.getTenantUserRealm(tenantId).getUserStoreManager();
        ((MockUserStoreManager) userStoreManager).addSecondaryUserStoreManager("PRIMARY", (MockUserStoreManager) userStoreManager);
        IdentityTenantUtil.setRealmService(testSessionRealmService);
        TenantManager tenantManager = new InMemoryTenantManager();
        testSessionRealmService.setTenantManager(tenantManager);
        RegistryDataHolder.getInstance().setRealmService(testSessionRealmService);
        OSGiDataHolder.getInstance().setUserRealmService(testSessionRealmService);
        Class[] singletonClasses = withRealmService.injectToSingletons();
        for (Class singletonClass : singletonClasses) {
            Object instance = getSingletonInstance(singletonClass);
            if (instance != null) {
                setInstanceValue(testSessionRealmService, RealmService.class, singletonClass, instance);
            } else {
                setInstanceValue(testSessionRealmService, RealmService.class, singletonClass, null);
            }
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
    return testSessionRealmService;
}
Also used : MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) ITestClass(org.testng.ITestClass) MockUserStoreManager(org.wso2.carbon.identity.common.testng.realm.MockUserStoreManager) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) InMemoryTenantManager(org.wso2.carbon.identity.common.testng.realm.InMemoryTenantManager) TenantManager(org.wso2.carbon.user.api.TenantManager) InMemoryTenantManager(org.wso2.carbon.identity.common.testng.realm.InMemoryTenantManager) InMemoryRealmService(org.wso2.carbon.identity.common.testng.realm.InMemoryRealmService)

Example 5 with MockUserStoreManager

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

the class JITProvisioningPostAuthenticationHandlerTest method testHandleWithAuthenticatedUserWithFederatedIdp.

@Test(description = "This test case tests the Post JIT provisioning handling flow with an authenticated user")
public void testHandleWithAuthenticatedUserWithFederatedIdp() throws FrameworkException, FederatedAssociationManagerException, AccountLockServiceException, UserStoreException {
    AuthenticationContext context = processAndGetAuthenticationContext(sp, true, true);
    FederatedAssociationManager federatedAssociationManager = mock(FederatedAssociationManagerImpl.class);
    when(FrameworkUtils.getFederatedAssociationManager()).thenReturn(federatedAssociationManager);
    doReturn("test").when(federatedAssociationManager).getUserForFederatedAssociation(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
    when(FrameworkUtils.getStepBasedSequenceHandler()).thenReturn(Mockito.mock(StepBasedSequenceHandler.class));
    mockStatic(FrameworkServiceDataHolder.class);
    PowerMockito.when(FrameworkServiceDataHolder.getInstance()).thenReturn(frameworkServiceDataHolder);
    mockStatic(AccountLockService.class);
    when(frameworkServiceDataHolder.getAccountLockService()).thenReturn(accountLockService);
    when(accountLockService.isAccountLocked(anyString(), anyString())).thenReturn(false);
    RealmService mockRealmService = mock(RealmService.class);
    PowerMockito.when(FrameworkServiceDataHolder.getInstance().getRealmService()).thenReturn(mockRealmService);
    UserRealm mockUserRealm = mock(UserRealm.class);
    UserStoreManager mockUserStoreManager = mock(UserStoreManager.class);
    Map<String, String> mockClaimValues = mock(HashMap.class);
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(1);
    when(mockRealmService.getTenantUserRealm(anyInt())).thenReturn(mockUserRealm);
    when(mockUserRealm.getUserStoreManager()).thenReturn(mockUserStoreManager);
    when(mockUserStoreManager.getUserClaimValues(anyString(), eq(new String[] { AccountConstants.ACCOUNT_DISABLED_CLAIM }), eq(UserCoreConstants.DEFAULT_PROFILE))).thenReturn(mockClaimValues);
    when(mockClaimValues.get(AccountConstants.ACCOUNT_DISABLED_CLAIM)).thenReturn("false");
    PostAuthnHandlerFlowStatus postAuthnHandlerFlowStatus = postJITProvisioningHandler.handle(request, response, context);
    Assert.assertEquals(postAuthnHandlerFlowStatus, PostAuthnHandlerFlowStatus.SUCCESS_COMPLETED, "Post JIT provisioning handler executed while having a authenticated user without federated " + "authenticator");
}
Also used : FederatedAssociationManager(org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) Mockito.anyString(org.mockito.Mockito.anyString) StepBasedSequenceHandler(org.wso2.carbon.identity.application.authentication.framework.handler.sequence.StepBasedSequenceHandler) PostAuthnHandlerFlowStatus(org.wso2.carbon.identity.application.authentication.framework.handler.request.PostAuthnHandlerFlowStatus) Test(org.testng.annotations.Test) AbstractFrameworkTest(org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest) 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