]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8361 WS api/components/search_projects no isFavorite in response when anonymous...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 9 Nov 2016 13:34:13 +0000 (14:34 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 9 Nov 2016 13:34:13 +0000 (14:34 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java

index 0c6142362b77466a95b99572e6d59ee3d058cf13..3f1f5256afe47f860c95b62923ab62f2c3f225d0 100644 (file)
@@ -150,8 +150,8 @@ public class SearchProjectsAction implements ComponentsWsAction {
     return request.build();
   }
 
-  private static SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults) {
-    Function<ComponentDto, Component> dbToWsComponent = new DbToWsComponent(searchResults.favoriteProjectUuids);
+  private SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults) {
+    Function<ComponentDto, Component> 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<ComponentDto, Component> {
     private final Component.Builder wsComponent;
     private final Set<String> favoriteProjectUuids;
+    private final boolean isUserLoggedIn;
 
-    private DbToWsComponent(Set<String> favoriteProjectUuids) {
+    private DbToWsComponent(Set<String> 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();
     }
   }
 
index d5c1285d53252774935dc0132a7a1489176e1a5a..e233b71bc6f840bf7d68f99b40b84b160b3b9c46 100644 (file)
@@ -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)));