diff options
author | Klaudio Sinani <klaudio.sinani@sonarsource.com> | 2022-10-12 11:59:53 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-13 20:03:18 +0000 |
commit | 76dbf1093000a2b8d655532fa2e7984289e452e7 (patch) | |
tree | d3033aedd59b205812113250e1e996b3bd64c9b7 /server/sonar-webserver-webapi | |
parent | 01cd6321628372eff4b45cd07bc0a83c5c3c5c37 (diff) | |
download | sonarqube-76dbf1093000a2b8d655532fa2e7984289e452e7.tar.gz sonarqube-76dbf1093000a2b8d655532fa2e7984289e452e7.zip |
SONAR-17385 Ensure duplicate and empty values can be provided as SCM account data
Diffstat (limited to 'server/sonar-webserver-webapi')
4 files changed, 45 insertions, 17 deletions
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<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 { 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<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; 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 @@ -203,6 +203,19 @@ public class UpdateActionTest { } @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(); |