*/
package org.sonar.server.user.ws;
-import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Change;
.setPassword(request.param(PARAM_PASSWORD))
.setName(request.param(PARAM_NAME))
.setEmail(request.param(PARAM_EMAIL))
- .setScmAccounts(getScmAccounts(request))
+ .setScmAccounts(parseScmAccounts(request))
.setLocal(request.mandatoryParamAsBoolean(PARAM_LOCAL))
.build();
}
- private static List<String> getScmAccounts(Request request) {
+ public static List<String> parseScmAccounts(Request request) {
if (request.hasParam(PARAM_SCM_ACCOUNT)) {
- return request.multiParam(PARAM_SCM_ACCOUNT);
- } else {
- return Collections.emptyList();
+ return formatScmAccounts(request.multiParam(PARAM_SCM_ACCOUNT));
}
+
+ return emptyList();
+ }
+
+ private static List<String> formatScmAccounts(List<String> scmAccounts) {
+ return scmAccounts
+ .stream()
+ .map(String::trim)
+ .collect(Collectors.toList());
}
static class CreateRequest {
package org.sonar.server.user.ws;
import com.google.common.base.Preconditions;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.sonar.server.user.UserUpdater.EMAIL_MAX_LENGTH;
import static org.sonar.server.user.UserUpdater.LOGIN_MAX_LENGTH;
import static org.sonar.server.user.UserUpdater.NAME_MAX_LENGTH;
+import static org.sonar.server.user.ws.CreateAction.parseScmAccounts;
import static org.sonar.server.user.ws.EmailValidator.isValidIfPresent;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_UPDATE;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_EMAIL;
.setLogin(request.mandatoryParam(PARAM_LOGIN))
.setName(request.param(PARAM_NAME))
.setEmail(request.param(PARAM_EMAIL))
- .setScmAccounts(getScmAccounts(request))
+ .setScmAccounts(parseScmAccounts(request))
.build();
}
- private static List<String> getScmAccounts(Request request) {
- if (request.hasParam(PARAM_SCM_ACCOUNT)) {
- return new ArrayList<>(request.multiParam(PARAM_SCM_ACCOUNT));
- } else {
- return Collections.emptyList();
- }
- }
-
private static class UpdateRequest {
private final String login;
*/
package org.sonar.server.user.ws;
+import java.util.List;
import java.util.Optional;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
assertThat(response.getUser().getScmAccountsList()).containsOnly("j,n");
}
+
+ @Test
+ public void create_user_ignores_duplicates_containing_whitespace_characters_in_scm_account_values() {
+ logInAsSystemAdministrator();
+
+ CreateWsResponse response = call(CreateRequest.builder()
+ .setLogin("john")
+ .setName("John")
+ .setEmail("john@email.com")
+ .setScmAccounts(List.of("admin", " admin "))
+ .setPassword("1234")
+ .build());
+
+ assertThat(response.getUser().getScmAccountsList()).containsOnly("admin");
+ }
+
@Test
public void create_user_with_empty_email() {
logInAsSystemAdministrator();
assertThat(user.getScmAccountsAsList()).containsExactlyInAnyOrder("jon.jon", "jon.snow");
}
+ @Test
+ public void update_scm_account_ignores_duplicates_containing_whitespace_characters_in_scm_account_values() {
+ createUser();
+
+ ws.newRequest()
+ .setParam("login", "john")
+ .setMultiParam("scmAccount", Arrays.asList("jon.snow", "jon.snow", "jon.jon", " jon.snow "))
+ .execute();
+
+ UserDto user = dbClient.userDao().selectByLogin(dbSession, "john");
+ assertThat(user.getScmAccountsAsList()).containsExactlyInAnyOrder("jon.jon", "jon.snow");
+ }
+
@Test
public void update_scm_account_ordered_case_insensitive() {
createUser();