From: Klaudio Sinani Date: Wed, 12 Oct 2022 09:59:53 +0000 (+0200) Subject: SONAR-17385 Ensure duplicate and empty values can be provided as SCM account data X-Git-Tag: 9.7.0.61563~42 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=76dbf1093000a2b8d655532fa2e7984289e452e7;p=sonarqube.git SONAR-17385 Ensure duplicate and empty values can be provided as SCM account data --- diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CreateAction.java index e785f342e26..5f358f5f680 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CreateAction.java @@ -19,8 +19,8 @@ */ 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; @@ -164,17 +164,24 @@ public class CreateAction implements UsersWsAction { .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 getScmAccounts(Request request) { + public static List 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 formatScmAccounts(List scmAccounts) { + return scmAccounts + .stream() + .map(String::trim) + .collect(Collectors.toList()); } static class CreateRequest { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UpdateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UpdateAction.java index ff16e14a93e..e72edefa93b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UpdateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UpdateAction.java @@ -20,8 +20,6 @@ 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; @@ -50,6 +48,7 @@ import static java.util.Collections.singletonList; 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; @@ -159,18 +158,10 @@ public class UpdateAction implements UsersWsAction { .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 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; diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java index ca4b5d5edee..d18c7d4f472 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.user.ws; +import java.util.List; import java.util.Optional; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; @@ -165,6 +166,22 @@ public class CreateActionTest { 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(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java index 3b55776fb80..dd05ba411e6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java @@ -202,6 +202,19 @@ public class UpdateActionTest { 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();