]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6226 Updating an email with blank value store null
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 3 Feb 2016 08:46:45 +0000 (09:46 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 4 Feb 2016 09:09:53 +0000 (10:09 +0100)
server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json [new file with mode: 0644]

index 320baa05c1628e8e14fb9c2edb71fb0560b91792..e7fc83ec6bfb2fdc1481f1245b2248d3dfd1f69f 100644 (file)
@@ -36,11 +36,11 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.platform.NewUserHandler;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserGroupDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.Message;
 import org.sonar.server.exceptions.NotFoundException;
index fea3c9c5de5f1fa6473bca42afa9a675b066339e..7166a23e9a6cc404979a5afd2bbdd8bd40dc2008 100644 (file)
@@ -36,6 +36,8 @@ import org.sonar.server.user.UserUpdater;
 import org.sonar.server.user.index.UserDoc;
 import org.sonar.server.user.index.UserIndex;
 
+import static com.google.common.base.Strings.emptyToNull;
+
 public class UpdateAction implements UsersWsAction {
 
   private static final String PARAM_LOGIN = "login";
@@ -97,7 +99,7 @@ public class UpdateAction implements UsersWsAction {
       updateUser.setName(request.mandatoryParam(PARAM_NAME));
     }
     if (request.hasParam(PARAM_EMAIL)) {
-      updateUser.setEmail(request.param(PARAM_EMAIL));
+      updateUser.setEmail(emptyToNull(request.param(PARAM_EMAIL)));
     }
     if (request.hasParam(PARAM_SCM_ACCOUNTS) || request.hasParam(PARAM_SCM_ACCOUNTS_DEPRECATED)) {
       updateUser.setScmAccounts(request.paramAsStrings(PARAM_SCM_ACCOUNTS));
index 618015af260673ee389a6561b7a4972e7ec887c9..f25623df45512fa71290ba466f06f2322017c80e 100644 (file)
@@ -28,27 +28,28 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDao;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.GroupMembershipDao;
+import org.sonar.db.user.UserDao;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserGroupDao;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.user.NewUserNotifier;
 import org.sonar.server.user.SecurityRealmFactory;
 import org.sonar.server.user.UserUpdater;
-import org.sonar.db.user.GroupDao;
-import org.sonar.db.user.UserDao;
 import org.sonar.server.user.index.UserIndex;
 import org.sonar.server.user.index.UserIndexDefinition;
 import org.sonar.server.user.index.UserIndexer;
 import org.sonar.server.ws.WsTester;
 
 import static com.google.common.collect.Lists.newArrayList;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
 public class UpdateActionTest {
@@ -61,7 +62,7 @@ public class UpdateActionTest {
   public static final EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(settings));
   @Rule
   public final UserSessionRule userSessionRule = UserSessionRule.standalone().login("admin")
-      .setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+    .setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
   WebService.Controller controller;
 
@@ -147,6 +148,20 @@ public class UpdateActionTest {
       .assertJson(getClass(), "update_email.json");
   }
 
+  @Test
+  public void blank_email_is_updated_to_null() throws Exception {
+    createUser();
+
+    tester.newPostRequest("api/users", "update")
+      .setParam("login", "john")
+      .setParam("email", "")
+      .execute()
+      .assertJson(getClass(), "blank_email_is_updated_to_null.json");
+
+    UserDto userDto = dbClient.userDao().selectByLogin(session, "john");
+    assertThat(userDto.getEmail()).isNull();
+  }
+
   @Test
   public void update_only_scm_accounts() throws Exception {
     createUser();
@@ -175,7 +190,9 @@ public class UpdateActionTest {
       .setLogin("john")
       .setName("John")
       .setScmAccounts(newArrayList("jn"))
-      .setActive(true));
+      .setActive(true))
+      .setExternalIdentity("jo")
+      .setExternalIdentityProvider("sonarqube");
     session.commit();
     userIndexer.index();
   }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json
new file mode 100644 (file)
index 0000000..48bfd74
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "user": {
+    "login": "john",
+    "name": "John",
+    "active": true,
+    "scmAccounts": [
+      "jn"
+    ]
+  }
+}