Search in sources :

Example 1 with UserSearchModel

use of com.thoughtworks.go.presentation.UserSearchModel in project gocd by gocd.

the class UserService method create.

public void create(List<UserSearchModel> userSearchModels, HttpLocalizedOperationResult result) {
    if (userSearchModels.isEmpty()) {
        result.badRequest(LocalizedMessage.string("NO_USERS_SELECTED"));
        return;
    }
    synchronized (enableUserMutex) {
        for (UserSearchModel userSearchModel : userSearchModels) {
            User user = userSearchModel.getUser();
            if (userExists(user)) {
                result.conflict(LocalizedMessage.string("RESOURCE_ALREADY_EXISTS", "user", user.getName(), user.getDisplayName(), user.getEmail()));
                return;
            }
            if (user.isAnonymous()) {
                result.badRequest(LocalizedMessage.string("USERNAME_NOT_PERMITTED", user.getName()));
                return;
            }
            if (!userSearchModel.getUserSourceType().equals(UserSourceType.PASSWORD_FILE) && validate(result, user)) {
                return;
            }
            userDao.saveOrUpdate(user);
            result.setMessage(LocalizedMessage.string("USER_SUCCESSFULLY_ADDED", user.getName()));
        }
    }
}
Also used : UserSearchModel(com.thoughtworks.go.presentation.UserSearchModel)

Example 2 with UserSearchModel

use of com.thoughtworks.go.presentation.UserSearchModel in project gocd by gocd.

the class UserServiceTest method shouldCreateNewUsers.

@Test
public void shouldCreateNewUsers() throws Exception {
    UserSearchModel foo = new UserSearchModel(new User("fooUser", "Mr Foo", "foo@cruise.com"), UserSourceType.LDAP);
    doNothing().when(userDao).saveOrUpdate(foo.getUser());
    when(userDao.findUser("fooUser")).thenReturn(new NullUser());
    when(userDao.enabledUserCount()).thenReturn(10);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    userService.create(Arrays.asList(foo), result);
    assertThat(result.isSuccessful(), is(true));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) UserSearchModel(com.thoughtworks.go.presentation.UserSearchModel) Test(org.junit.Test)

Example 3 with UserSearchModel

use of com.thoughtworks.go.presentation.UserSearchModel in project gocd by gocd.

the class UserServiceIntegrationTest method shouldReturnErrorWhenUserAlreadyExists.

@Test
public void shouldReturnErrorWhenUserAlreadyExists() throws Exception {
    UserSearchModel foo = new UserSearchModel(new User("fooUser", "Mr Foo", "foo@cruise.com"), UserSourceType.LDAP);
    addUser(foo.getUser());
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    userService.create(Arrays.asList(foo), result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Failed to add user. The user 'fooUser' already exists."));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) UserSearchModel(com.thoughtworks.go.presentation.UserSearchModel) Test(org.junit.Test)

Example 4 with UserSearchModel

use of com.thoughtworks.go.presentation.UserSearchModel in project gocd by gocd.

the class UserServiceIntegrationTest method shouldReturnErrorMessageWhenUserValidationsFail.

@Test
public void shouldReturnErrorMessageWhenUserValidationsFail() throws Exception {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    User invalidUser = new User("fooUser", "Foo User", "invalidEmail");
    UserSearchModel searchModel = new UserSearchModel(invalidUser, UserSourceType.LDAP);
    userService.create(Arrays.asList(searchModel), result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Failed to add user. Validations failed. Invalid email address."));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) UserSearchModel(com.thoughtworks.go.presentation.UserSearchModel) Test(org.junit.Test)

Example 5 with UserSearchModel

use of com.thoughtworks.go.presentation.UserSearchModel in project gocd by gocd.

the class UserSearchServiceTest method shouldNotReturnWarningMessageWhenPasswordFileIsNotConfigured.

@Test
public void shouldNotReturnWarningMessageWhenPasswordFileIsNotConfigured() throws Exception {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    when(goConfigService.isPasswordFileConfigured()).thenReturn(false);
    when(ldapUserSearch.search("foo")).thenReturn(Arrays.asList(new User("foo")));
    List<UserSearchModel> models = userSearchService.search("foo", result);
    assertThat(models.size(), is(1));
    assertThat(result.hasMessage(), is(false));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) User(com.thoughtworks.go.domain.User) UserSearchModel(com.thoughtworks.go.presentation.UserSearchModel) Test(org.junit.Test)

Aggregations

UserSearchModel (com.thoughtworks.go.presentation.UserSearchModel)17 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)16 Test (org.junit.Test)16 User (com.thoughtworks.go.domain.User)6 SecurityConfig (com.thoughtworks.go.config.SecurityConfig)1