From 52022312cc34f18744707f25bad36d3dc585b56e Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 3 Feb 2016 09:46:45 +0100 Subject: [PATCH] SONAR-6226 Updating an email with blank value store null --- .../org/sonar/server/user/UserUpdater.java | 2 +- .../sonar/server/user/ws/UpdateAction.java | 4 ++- .../server/user/ws/UpdateActionTest.java | 27 +++++++++++++++---- .../blank_email_is_updated_to_null.json | 10 +++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/user/ws/UpdateActionTest/blank_email_is_updated_to_null.json 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; @@ -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 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" + ] + } +} -- 2.39.5