]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17385 Ensure duplicate and empty values can be provided as SCM account data
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Wed, 12 Oct 2022 09:59:53 +0000 (11:59 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 13 Oct 2022 20:03:18 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CreateAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/UpdateAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CreateActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java

index e785f342e2647d4af117aec21c903c0fa213b078..5f358f5f6802c7a1e7956940a6c6d5042ba0c787 100644 (file)
@@ -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 {
index ff16e14a93eff5ff131baefde5adf9488ea581f2..e72edefa93bb6aaf286bfa769d4603cd6c4ca996 100644 (file)
@@ -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;
index ca4b5d5edeee68c4bdaaa93bca7a849714d01eb9..d18c7d4f472f01af8c3c7ebf75c6eec5c3930c27 100644 (file)
@@ -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();
index 3b55776fb80bff057e25198976b5033c3a988958..dd05ba411e6ffe4c424d3d3eed8f16441681da31 100644 (file)
@@ -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();