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()
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();
}
}
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)));