From bb6cf5986337960db99e1fd7b7da3e691a305dbd Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 9 Nov 2016 14:34:13 +0100 Subject: [PATCH] SONAR-8361 WS api/components/search_projects no isFavorite in response when anonymous user --- .../component/ws/SearchProjectsAction.java | 20 ++++++++++------ .../ws/SearchProjectsActionTest.java | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java index 0c6142362b7..3f1f5256afe 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java @@ -150,8 +150,8 @@ public class SearchProjectsAction implements ComponentsWsAction { return request.build(); } - private static SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults) { - Function dbToWsComponent = new DbToWsComponent(searchResults.favoriteProjectUuids); + private SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults) { + Function dbToWsComponent = new DbToWsComponent(searchResults.favoriteProjectUuids, userSession.isLoggedIn()); return Stream.of(SearchProjectsWsResponse.newBuilder()) .map(response -> response.setPaging(Common.Paging.newBuilder() @@ -231,21 +231,27 @@ public class SearchProjectsAction implements ComponentsWsAction { private static class DbToWsComponent implements Function { private final Component.Builder wsComponent; private final Set favoriteProjectUuids; + private final boolean isUserLoggedIn; - private DbToWsComponent(Set favoriteProjectUuids) { + private DbToWsComponent(Set favoriteProjectUuids, boolean isUserLoggedIn) { + this.isUserLoggedIn = isUserLoggedIn; this.wsComponent = Component.newBuilder(); this.favoriteProjectUuids = favoriteProjectUuids; } @Override public Component apply(ComponentDto dbComponent) { - return wsComponent + wsComponent .clear() .setId(dbComponent.uuid()) .setKey(dbComponent.key()) - .setName(dbComponent.name()) - .setIsFavorite(favoriteProjectUuids.contains(dbComponent.uuid())) - .build(); + .setName(dbComponent.name()); + + if (isUserLoggedIn) { + wsComponent.setIsFavorite(favoriteProjectUuids.contains(dbComponent.uuid())); + } + + return wsComponent.build(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java index d5c1285d532..e233b71bc6f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java @@ -208,6 +208,30 @@ public class SearchProjectsActionTest { assertThat(result.getComponentsList()).extracting(Component::getId).containsExactly("java-id", "markdown-id"); } + @Test + public void do_not_return_isFavorite_if_anonymous_user() { + insertProjectInDbAndEs(newProjectDto().setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81))); + insertMetrics(COVERAGE); + userSession.anonymous(); + + SearchProjectsWsResponse result = call(request); + + assertThat(result.getComponentsCount()).isEqualTo(1); + assertThat(result.getComponents(0).hasIsFavorite()).isFalse(); + } + + @Test + public void empty_list_if_isFavorite_filter_and_anonymous_user() { + insertProjectInDbAndEs(newProjectDto().setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81))); + insertMetrics(COVERAGE); + userSession.anonymous(); + request.setFilter("isFavorite"); + + SearchProjectsWsResponse result = call(request); + + assertThat(result.getComponentsCount()).isEqualTo(0); + } + @Test public void return_nloc_facet() { insertProjectInDbAndEs(newProjectDto().setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d))); -- 2.39.5