Search in sources :

Example 56 with UserDTO

use of org.wso2.carbon.identity.user.endpoint.dto.UserDTO in project identity-governance by wso2-extensions.

the class UsernameUpdateServiceImplTest method testExceptionWhileRetrievingNullRealm.

@Test(expectedExceptions = UsernameUpdateException.class)
public void testExceptionWhileRetrievingNullRealm() throws Exception {
    UserDTO userDTO = buildUserDTO("testuser1", "testuser11", UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    RealmService realmService = mock(RealmService.class);
    TenantManager tenantManager = mock(TenantManager.class);
    when(realmService.getTenantManager()).thenReturn(tenantManager);
    when(tenantManager.getTenantId(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    when(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)).thenReturn(null);
    UsernameUpdateServiceImpl usernameUpdateServiceImpl = new UsernameUpdateServiceImpl();
    usernameUpdateServiceImpl.setRealmService(realmService);
    usernameUpdateServiceImpl.updateUsername(userDTO);
}
Also used : RealmService(org.wso2.carbon.user.core.service.RealmService) UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.testng.annotations.Test)

Example 57 with UserDTO

use of org.wso2.carbon.identity.user.endpoint.dto.UserDTO in project identity-governance by wso2-extensions.

the class UsernameUpdateServiceImplTest method testUpdateUsername.

@Test(dataProvider = "getValidUsers")
public void testUpdateUsername(String existingUsername, String newUsername, String userStoreDomain, String tenantDomain) throws Exception {
    UserDTO userDTO = buildUserDTO(existingUsername, newUsername, userStoreDomain, tenantDomain);
    RealmService realmService = mock(RealmService.class);
    TenantManager tenantManager = mock(TenantManager.class);
    UserRealm userRealm = mock(UserRealm.class);
    UserStoreManager userStoreManager = mock(UserStoreManager.class);
    when(realmService.getTenantManager()).thenReturn(tenantManager);
    when(tenantManager.getTenantId(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    when(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)).thenReturn(userRealm);
    when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    when(userStoreManager.isExistingUser(UserCoreUtil.addDomainToName(userDTO.getExistingUsername(), userDTO.getUserStoreDomain()))).thenReturn(true);
    mockedForgetMeToolExecutor.when(() -> ForgetMeToolExecutor.run(userDTO.getExistingUsername(), userDTO.getNewUsername(), userDTO.getUserStoreDomain(), userDTO.getTenantDomain(), MultitenantConstants.SUPER_TENANT_ID)).thenAnswer((Answer<Void>) invocation -> null);
    ;
    UsernameUpdateServiceImpl usernameUpdateServiceImpl = new UsernameUpdateServiceImpl();
    usernameUpdateServiceImpl.setRealmService(realmService);
    StatusDTO statusDTO = usernameUpdateServiceImpl.updateUsername(userDTO);
    usernameUpdateServiceImpl.unsetRealmService(null);
    Assert.assertEquals(STATUS_SUCCESS.getCode(), statusDTO.getCode());
    Assert.assertEquals(String.format(STATUS_SUCCESS.getMessage(), userDTO.getExistingUsername(), userDTO.getNewUsername()), statusDTO.getMessage());
}
Also used : MultitenantConstants(org.wso2.carbon.utils.multitenancy.MultitenantConstants) DataProvider(org.testng.annotations.DataProvider) UserRealm(org.wso2.carbon.user.api.UserRealm) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Test(org.testng.annotations.Test) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) AfterMethod(org.testng.annotations.AfterMethod) RealmService(org.wso2.carbon.user.core.service.RealmService) MockitoAnnotations(org.mockito.MockitoAnnotations) Answer(org.mockito.stubbing.Answer) Mockito.doThrow(org.mockito.Mockito.doThrow) Assert(org.testng.Assert) UserCoreUtil(org.wso2.carbon.user.core.util.UserCoreUtil) StatusDTO(org.wso2.carbon.identity.user.rename.core.dto.StatusDTO) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) UsernameUpdateException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException) BeforeMethod(org.testng.annotations.BeforeMethod) Mockito.when(org.mockito.Mockito.when) UserCoreConstants(org.wso2.carbon.user.core.UserCoreConstants) STATUS_SUCCESS(org.wso2.carbon.identity.user.rename.core.constants.UsernameUpdateServiceConstants.Status.STATUS_SUCCESS) Mockito(org.mockito.Mockito) MockedStatic(org.mockito.MockedStatic) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) StatusDTO(org.wso2.carbon.identity.user.rename.core.dto.StatusDTO) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.testng.annotations.Test)

Example 58 with UserDTO

use of org.wso2.carbon.identity.user.endpoint.dto.UserDTO in project identity-governance by wso2-extensions.

the class UsernameUpdateServiceImpl method updateUsername.

@Override
public StatusDTO updateUsername(UserDTO userDTO) throws UsernameUpdateException {
    validate(userDTO);
    String tenantDomain = userDTO.getTenantDomain();
    String domainQualifiedUsername = UserCoreUtil.addDomainToName(userDTO.getExistingUsername(), userDTO.getUserStoreDomain());
    UserStoreManager userStoreManager = getUserStoreManager(tenantDomain);
    if (isExistingUser(domainQualifiedUsername, tenantDomain, userStoreManager)) {
        performUpdate(userDTO, userStoreManager);
    } else {
        throw new UsernameUpdateClientException(String.format(ERROR_USER_NOT_FOUND.getMessage(), tenantDomain, domainQualifiedUsername), ERROR_USER_NOT_FOUND.getCode(), UsernameUpdateClientException.ErrorType.NOT_FOUND);
    }
    return UpdateUsernameServiceUtil.buildStatus(STATUS_SUCCESS.getCode(), String.format(STATUS_SUCCESS.getMessage(), userDTO.getExistingUsername(), userDTO.getNewUsername()));
}
Also used : UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) UsernameUpdateClientException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateClientException)

Example 59 with UserDTO

use of org.wso2.carbon.identity.user.endpoint.dto.UserDTO in project identity-governance by wso2-extensions.

the class UpdateUsernameApiServiceImpl method updateUsernamePut.

/**
 * Username update service method.
 *
 * @param user UsernameUpdateRequestDTO{@link UsernameUpdateRequestDTO} including update request parameters
 * @return Response status. Returns 200 upon successful update
 */
@Override
public Response updateUsernamePut(UsernameUpdateRequestDTO user) {
    try {
        UserDTO userDTO = new UserDTO();
        userDTO.setExistingUsername(user.getExistingUsername());
        userDTO.setNewUsername(user.getNewUsername());
        userDTO.setUserStoreDomain(user.getRealm());
        userDTO.setTenantDomain(getTenantDomainFromContext());
        UsernameUpdateService usernameUpdateService = Utils.getUsernameUpdateService();
        usernameUpdateService.updateUsername(userDTO);
    } catch (UsernameUpdateClientException e) {
        handleClientError(e);
    } catch (UsernameUpdateException e) {
        handleServerError(e);
    } catch (Throwable e) {
        handleUnexpectedServerError(e);
    }
    return Response.ok().status(Response.Status.OK).build();
}
Also used : UsernameUpdateService(org.wso2.carbon.identity.user.rename.core.service.UsernameUpdateService) UserDTO(org.wso2.carbon.identity.user.rename.core.dto.UserDTO) UsernameUpdateClientException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateClientException) UsernameUpdateException(org.wso2.carbon.identity.user.rename.core.exception.UsernameUpdateException)

Example 60 with UserDTO

use of org.wso2.carbon.identity.user.endpoint.dto.UserDTO in project identity-governance by wso2-extensions.

the class Utils method getUser.

public static User getUser(UserDTO userDTO) {
    User user = new User();
    user.setTenantDomain(userDTO.getTenantDomain());
    if (userDTO.getRealm() == null) {
        userDTO.setRealm(IdentityUtil.getPrimaryDomainName());
    }
    user.setUserStoreDomain(userDTO.getRealm());
    user.setUserName(userDTO.getUsername());
    return user;
}
Also used : User(org.wso2.carbon.identity.application.common.model.User)

Aggregations

IdentityException (org.wso2.carbon.identity.base.IdentityException)23 UserDTO (org.wso2.carbon.identity.mgt.dto.UserDTO)22 VerificationBean (org.wso2.carbon.identity.mgt.beans.VerificationBean)19 RecoveryProcessor (org.wso2.carbon.identity.mgt.RecoveryProcessor)17 UserStoreException (org.wso2.carbon.user.api.UserStoreException)17 IdentityMgtServiceException (org.wso2.carbon.identity.mgt.IdentityMgtServiceException)14 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)12 Test (org.testng.annotations.Test)10 UserDTO (org.wso2.carbon.identity.user.rename.core.dto.UserDTO)10 UserRecoveryDTO (org.wso2.carbon.identity.mgt.dto.UserRecoveryDTO)9 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)9 RealmService (org.wso2.carbon.user.core.service.RealmService)9 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)8 UserDTO (org.wso2.carbon.identity.test.integration.service.stub.UserDTO)7 UserDTO (org.wso2.carbon.identity.user.endpoint.dto.UserDTO)6 NotificationDataDTO (org.wso2.carbon.identity.mgt.dto.NotificationDataDTO)5 UserChallengesDTO (org.wso2.carbon.identity.mgt.dto.UserChallengesDTO)4 User (org.wso2.carbon.apimgt.core.models.User)3 User (org.wso2.carbon.identity.application.common.model.User)3 ChallengeQuestionProcessor (org.wso2.carbon.identity.mgt.ChallengeQuestionProcessor)3