From 8bd55739384b49e5aef9508672c04b2f0ee612e8 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 3 Oct 2017 10:42:53 +0200 Subject: [PATCH] Avatar resolver fails when email is empty --- .../sonar/server/issue/ws/AvatarResolverImpl.java | 3 ++- .../server/issue/ws/SearchResponseFormat.java | 2 +- .../server/organization/ws/AddMemberAction.java | 3 ++- .../organization/ws/SearchMembersAction.java | 3 ++- .../qualityprofile/ws/SearchUsersAction.java | 3 ++- .../org/sonar/server/user/ws/SearchAction.java | 3 ++- .../server/issue/ws/AvatarResolverImplTest.java | 15 ++++++++------- .../qualityProfile/QualityProfilesEditTest.java | 2 +- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java index 427a6ef1d42..0e623ba26d1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AvatarResolverImpl.java @@ -23,6 +23,7 @@ package org.sonar.server.issue.ws; import com.google.common.hash.Hashing; import org.sonar.db.user.UserDto; +import static com.google.common.base.Strings.emptyToNull; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Locale.ENGLISH; import static java.util.Objects.requireNonNull; @@ -32,7 +33,7 @@ public class AvatarResolverImpl implements AvatarResolver { @Override public String create(UserDto user) { UserDto userDto = requireNonNull(user, "User cannot be null"); - return hash(requireNonNull(userDto.getEmail(), "Email cannot be null")); + return hash(requireNonNull(emptyToNull(userDto.getEmail()), "Email cannot be null")); } private static String hash(String text) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java index 3c9dc8e26da..2b9190efa43 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java @@ -336,7 +336,7 @@ public class SearchResponseFormat { .setLogin(user.getLogin()) .setName(nullToEmpty(user.getName())) .setActive(user.isActive()); - setNullable(user.getEmail(), email -> builder.setAvatar(avatarFactory.create(user))); + setNullable(emptyToNull(user.getEmail()), email -> builder.setAvatar(avatarFactory.create(user))); return builder; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/AddMemberAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/AddMemberAction.java index 535ace66812..4ca1e753bb3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/AddMemberAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/AddMemberAction.java @@ -39,6 +39,7 @@ import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonarqube.ws.Organizations.AddMemberWsResponse; import org.sonarqube.ws.Organizations.User; +import static com.google.common.base.Strings.emptyToNull; import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.db.user.GroupMembershipQuery.IN; import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_LOGIN; @@ -127,7 +128,7 @@ public class AddMemberAction implements OrganizationsWsAction { .setLogin(user.getLogin()) .setName(user.getName()) .setGroupCount(groups); - setNullable(user.getEmail(), text -> wsUser.setAvatar(avatarResolver.create(user))); + setNullable(emptyToNull(user.getEmail()), text -> wsUser.setAvatar(avatarResolver.create(user))); response.setUser(wsUser); return response.build(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java index f8ed7c843ee..1d87cfaac36 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/SearchMembersAction.java @@ -49,6 +49,7 @@ import org.sonarqube.ws.Organizations.SearchMembersWsResponse; import org.sonarqube.ws.Organizations.User; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Strings.emptyToNull; import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.server.es.SearchOptions.MAX_LIMIT; import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; @@ -132,7 +133,7 @@ public class SearchMembersAction implements OrganizationsWsAction { .clear() .setLogin(login) .setName(userDto.getName()); - setNullable(userDto.getEmail(), text -> wsUser.setAvatar(avatarResolver.create(userDto))); + setNullable(emptyToNull(userDto.getEmail()), text -> wsUser.setAvatar(avatarResolver.create(userDto))); setNullable(groupCountByLogin, count -> wsUser.setGroupCount(groupCountByLogin.count(login))); return wsUser; }) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchUsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchUsersAction.java index 9d5cc5c5025..d031e80e188 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchUsersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchUsersAction.java @@ -41,6 +41,7 @@ import org.sonarqube.ws.Common; import org.sonarqube.ws.QualityProfiles.SearchUsersResponse; import org.sonarqube.ws.client.qualityprofile.SearchUsersRequest; +import static com.google.common.base.Strings.emptyToNull; import static org.sonar.api.server.ws.WebService.Param.PAGE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; import static org.sonar.api.server.ws.WebService.Param.SELECTED; @@ -158,7 +159,7 @@ public class SearchUsersAction implements QProfileWsAction { .setLogin(user.getLogin()) .setName(user.getName()) .setSelected(isSelected); - setNullable(user.getEmail(), e -> builder.setAvatar(avatarResolver.create(user))); + setNullable(emptyToNull(user.getEmail()), e -> builder.setAvatar(avatarResolver.create(user))); return builder .build(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java index 753d388f67e..49f2a4ce7b1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java @@ -46,6 +46,7 @@ import org.sonarqube.ws.client.user.SearchRequest; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Strings.emptyToNull; import static org.sonar.api.server.ws.WebService.Param.FIELDS; import static org.sonar.api.server.ws.WebService.Param.PAGE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; @@ -144,7 +145,7 @@ public class SearchAction implements UsersWsAction { .setLogin(user.getLogin()); setIfNeeded(FIELD_NAME, fields, user.getName(), userBuilder::setName); if (userSession.isLoggedIn()) { - setIfNeeded(FIELD_AVATAR, fields, user.getEmail(), u -> userBuilder.setAvatar(avatarResolver.create(user))); + setIfNeeded(FIELD_AVATAR, fields, emptyToNull(user.getEmail()), u -> userBuilder.setAvatar(avatarResolver.create(user))); setIfNeeded(FIELD_ACTIVE, fields, user.isActive(), userBuilder::setActive); setIfNeeded(FIELD_LOCAL, fields, user.isLocal(), userBuilder::setLocal); setIfNeeded(FIELD_EXTERNAL_IDENTITY, fields, user.getExternalIdentity(), userBuilder::setExternalIdentity); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java index bd3e5c2bb9f..9ace73398a8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AvatarResolverImplTest.java @@ -41,13 +41,6 @@ public class AvatarResolverImplTest { assertThat(avatar).isEqualTo("9297bfb538f650da6143b604e82a355d"); } - @Test - public void create_when_empty_email() throws Exception { - String avatar = underTest.create(newUserDto("john", "John", "")); - - assertThat(avatar).isEqualTo("d41d8cd98f00b204e9800998ecf8427e"); - } - @Test public void create_is_case_insensitive() throws Exception { assertThat(underTest.create(newUserDto("john", "John", "john@doo.com"))).isEqualTo(underTest.create(newUserDto("john", "John", "John@Doo.com"))); @@ -68,4 +61,12 @@ public class AvatarResolverImplTest { underTest.create(newUserDto("john", "John", null)); } + + @Test + public void fail_when_email_is_empty() throws Exception { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("Email cannot be null"); + + underTest.create(newUserDto("john", "John", "")); + } } diff --git a/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesEditTest.java b/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesEditTest.java index bcf23e3e4cc..17298e22211 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesEditTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesEditTest.java @@ -79,7 +79,7 @@ public class QualityProfilesEditTest { .containsExactlyInAnyOrder( tuple(user1.getLogin(), user1.getName(), "3acc837f898bdaa338b7cd7a9ab6dd5b", true), tuple(user2.getLogin(), user2.getName(), "fd6926c24d76d650a365ae350784e048", false), - tuple("admin", "Administrator", "d41d8cd98f00b204e9800998ecf8427e", false)); + tuple("admin", "Administrator", "", false)); assertThat(users.getPaging()).extracting(Common.Paging::getPageIndex, Common.Paging::getPageSize, Common.Paging::getTotal) .containsExactlyInAnyOrder(1, 25, 3); } -- 2.39.5