aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-03 09:46:45 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-04 10:09:53 +0100
commit52022312cc34f18744707f25bad36d3dc585b56e (patch)
treee3c2cd42396953aab9b22142eb9a8d2e0c5cb5c2
parent5f612c9da30cca1d5d62851d644a7e729a9a78ab (diff)
downloadsonarqube-52022312cc34f18744707f25bad36d3dc585b56e.tar.gz
sonarqube-52022312cc34f18744707f25bad36d3dc585b56e.zip
SONAR-6226 Updating an email with blank value store null
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java27
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json10
4 files changed, 36 insertions, 7 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
index 320baa05c16..e7fc83ec6bf 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
@@ -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;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java
index fea3c9c5de5..7166a23e9a6 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/UpdateAction.java
@@ -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));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java
index 618015af260..f25623df455 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java
@@ -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;
@@ -148,6 +149,20 @@ public class UpdateActionTest {
}
@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
index 00000000000..48bfd743347
--- /dev/null
+++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json
@@ -0,0 +1,10 @@
+{
+ "user": {
+ "login": "john",
+ "name": "John",
+ "active": true,
+ "scmAccounts": [
+ "jn"
+ ]
+ }
+}