Search in sources :

Example 1 with UserEnabledException

use of com.thoughtworks.go.server.exceptions.UserEnabledException in project gocd by gocd.

the class UserServiceTest method shouldFailWithErrorWhenDeletingAnEnabledUserFails.

@Test
void shouldFailWithErrorWhenDeletingAnEnabledUserFails() {
    String username = "username";
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    when(userDao.deleteUser(username, "currentUser")).thenThrow(new UserEnabledException());
    userService.deleteUser(username, "currentUser", result);
    assertThat(result.isSuccessful()).isFalse();
    assertThat(result.hasMessage()).isTrue();
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) UserEnabledException(com.thoughtworks.go.server.exceptions.UserEnabledException) Test(org.junit.jupiter.api.Test)

Example 2 with UserEnabledException

use of com.thoughtworks.go.server.exceptions.UserEnabledException in project gocd by gocd.

the class UserSqlMapDao method deleteUser.

@Override
public boolean deleteUser(final String username, String byWhom) {
    return (Boolean) transactionTemplate.execute((TransactionCallback) status -> {
        User user = findUser(username);
        if (user instanceof NullUser) {
            throw new RecordNotFoundException(EntityType.User, username);
        }
        if (user.isEnabled()) {
            throw new UserEnabledException();
        }
        sessionFactory.getCurrentSession().delete(user);
        accessTokenDao.revokeTokensBecauseOfUserDelete(Collections.singletonList(username), byWhom);
        return Boolean.TRUE;
    });
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) NullUser(com.thoughtworks.go.domain.NullUser) User(com.thoughtworks.go.domain.User) NullUser(com.thoughtworks.go.domain.NullUser) UserEnabledException(com.thoughtworks.go.server.exceptions.UserEnabledException)

Aggregations

UserEnabledException (com.thoughtworks.go.server.exceptions.UserEnabledException)2 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 NullUser (com.thoughtworks.go.domain.NullUser)1 User (com.thoughtworks.go.domain.User)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 Test (org.junit.jupiter.api.Test)1 TransactionCallback (org.springframework.transaction.support.TransactionCallback)1