]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8363 Mark just created projects as favorite when there's a project creator... 1401/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 17 Nov 2016 10:18:29 +0000 (11:18 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 17 Nov 2016 12:28:45 +0000 (13:28 +0100)
it/it-tests/src/test/java/it/analysis/FavoriteTest.java
server/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java
server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java
server/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java

index 8ee6eaaad2b8c1d9e8a26497c2109412d4814ac8..f066888a61a3bcb0e8403a5ba5e8c9d012908d3f 100644 (file)
@@ -23,13 +23,18 @@ package it.analysis;
 import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.SonarScanner;
 import it.Category3Suite;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsPermissions;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.permission.AddProjectCreatorToTemplateWsRequest;
+import org.sonarqube.ws.client.permission.RemoveProjectCreatorFromTemplateWsRequest;
+import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
 
 import static com.sonar.orchestrator.container.Server.ADMIN_LOGIN;
 import static com.sonar.orchestrator.container.Server.ADMIN_PASSWORD;
@@ -49,32 +54,71 @@ public class FavoriteTest {
     orchestrator.resetData();
   }
 
+  @After
+  public void tearDown() {
+    removeProjectCreatorPermission();
+  }
+
   @BeforeClass
   public static void classSetUp() {
     adminWsClient = newAdminWsClient(orchestrator);
   }
 
   @Test
-  public void project_as_favorite_when_authenticated_and_first_analysis() {
-    SonarScanner sampleProject = SonarScanner.create(projectDir("shared/xoo-sample"))
-      .setProperty("sonar.login", ADMIN_LOGIN)
-      .setProperty("sonar.password", ADMIN_PASSWORD);
+  public void project_as_favorite_when_authenticated_and_first_analysis_and_a_project_creator_permission() {
+    SonarScanner sampleProject = createScannerWithUserCredentials();
+    addProjectCreatorPermission();
+
     orchestrator.executeBuild(sampleProject);
 
     String response = adminWsClient.wsConnector().call(new GetRequest("api/favourites").setMediaType(MediaTypes.JSON)).content();
     assertThat(response).contains(PROJECT_KEY);
   }
 
+  @Test
+  public void no_project_as_favorite_when_no_project_creator_permission() {
+    SonarScanner sampleProject = createScannerWithUserCredentials();
+
+    orchestrator.executeBuild(sampleProject);
+
+    String response = adminWsClient.wsConnector().call(new GetRequest("api/favourites").setMediaType(MediaTypes.JSON)).content();
+    assertThat(response).doesNotContain(PROJECT_KEY);
+  }
+
   @Test
   public void no_project_as_favorite_when_second_analysis() {
     SonarScanner sampleProject = SonarScanner.create(projectDir("shared/xoo-sample"));
     orchestrator.executeBuild(sampleProject);
-    sampleProject
-      .setProperty("sonar.login", ADMIN_LOGIN)
-      .setProperty("sonar.password", ADMIN_PASSWORD);
+    sampleProject = createScannerWithUserCredentials();
+    addProjectCreatorPermission();
+
     orchestrator.executeBuild(sampleProject);
 
     String response = adminWsClient.wsConnector().call(new GetRequest("api/favourites").setMediaType(MediaTypes.JSON)).content();
     assertThat(response).doesNotContain(PROJECT_KEY);
   }
+
+  private static SonarScanner createScannerWithUserCredentials() {
+    return SonarScanner.create(projectDir("shared/xoo-sample"))
+      .setProperty("sonar.login", ADMIN_LOGIN)
+      .setProperty("sonar.password", ADMIN_PASSWORD);
+  }
+
+  private void addProjectCreatorPermission() {
+    WsPermissions.SearchTemplatesWsResponse permissionTemplates = adminWsClient.permissions().searchTemplates(new SearchTemplatesWsRequest());
+    assertThat(permissionTemplates.getDefaultTemplatesCount()).isEqualTo(1);
+    adminWsClient.permissions().addProjectCreatorToTemplate(AddProjectCreatorToTemplateWsRequest.builder()
+      .setTemplateId(permissionTemplates.getDefaultTemplates(0).getTemplateId())
+      .setPermission("admin")
+      .build());
+  }
+
+  private void removeProjectCreatorPermission() {
+    WsPermissions.SearchTemplatesWsResponse permissionTemplates = adminWsClient.permissions().searchTemplates(new SearchTemplatesWsRequest());
+    assertThat(permissionTemplates.getDefaultTemplatesCount()).isEqualTo(1);
+    adminWsClient.permissions().removeProjectCreatorFromTemplate(RemoveProjectCreatorFromTemplateWsRequest.builder()
+      .setTemplateId(permissionTemplates.getDefaultTemplates(0).getTemplateId())
+      .setPermission("admin")
+      .build());
+  }
 }
index 7f5399a64f723414c5533cdca7035c7aef7cdc71..d1019e520060b8db8da57062d90b6d7bfa624f6f 100644 (file)
@@ -85,7 +85,7 @@ public class DefaultRubyComponentService implements RubyComponentService {
   public long createComponent(DbSession dbSession, String key, @Nullable String branch, String name, @Nullable String qualifier) {
     ComponentDto provisionedComponent = componentService.create(dbSession, NewComponent.create(key, name).setQualifier(qualifier).setBranch(branch));
     permissionTemplateService.applyDefaultPermissionTemplate(dbSession, provisionedComponent.getKey());
-    if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier())) {
+    if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier()) && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, provisionedComponent)) {
       favoriteService.put(dbSession, provisionedComponent.getId());
       dbSession.commit();
     }
index 6997d28bf3519f5998c7e509db5b3829955a4332..64e16746c8adcf083c209f2003a47f881c68ae8a 100644 (file)
@@ -53,7 +53,7 @@ public class ReportSubmitter {
   private final FavoriteService favoriteService;
 
   public ReportSubmitter(CeQueue queue, UserSession userSession,
-                         ComponentService componentService, PermissionTemplateService permissionTemplateService, DbClient dbClient, FavoriteService favoriteService) {
+    ComponentService componentService, PermissionTemplateService permissionTemplateService, DbClient dbClient, FavoriteService favoriteService) {
     this.queue = queue;
     this.userSession = userSession;
     this.componentService = componentService;
@@ -87,8 +87,10 @@ public class ReportSubmitter {
     newProject.setQualifier(Qualifiers.PROJECT);
     // "provisioning" permission is check in ComponentService
     ComponentDto project = componentService.create(dbSession, newProject);
-    favoriteService.put(dbSession, project.getId());
-    dbSession.commit();
+    if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, project)) {
+      favoriteService.put(dbSession, project.getId());
+      dbSession.commit();
+    }
 
     permissionTemplateService.applyDefault(dbSession, project, projectCreatorUserId);
 
index c3c1dba76a9276de7a9911238fd297a2d93657f6..b0bb18c690f0daef161c8ac168f1dfde59a3d964 100644 (file)
@@ -50,6 +50,7 @@ import org.sonar.server.user.UserSession;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
 import static org.sonar.api.security.DefaultGroups.isAnyone;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
@@ -155,6 +156,16 @@ public class PermissionTemplateService {
     indexProjectPermissions(dbSession, asList(component.uuid()));
   }
 
+  public boolean hasDefaultTemplateWithPermissionOnProjectCreator(DbSession dbSession, ComponentDto component) {
+    PermissionTemplateDto template = findDefaultTemplate(dbSession, component);
+    return hasProjectCreatorPermission(dbSession, template);
+  }
+
+  private boolean hasProjectCreatorPermission(DbSession dbSession, @Nullable PermissionTemplateDto template) {
+    return template != null && dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, singletonList(template.getId())).stream()
+      .anyMatch(PermissionTemplateCharacteristicDto::getWithProjectCreator);
+  }
+
   private void indexProjectPermissions(DbSession dbSession, List<String> projectUuids) {
     permissionIndexer.index(dbSession, projectUuids);
   }
index 2955d6d959b8013bd8d2595006116e3714325b42..636243d74267c0e16d7218a9b353bf73cda0031e 100644 (file)
@@ -48,6 +48,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 
 public class DefaultRubyComponentServiceTest {
@@ -76,27 +77,27 @@ public class DefaultRubyComponentServiceTest {
 
   ComponentDbTester componentDb = new ComponentDbTester(db);
 
-  DefaultRubyComponentService service = new DefaultRubyComponentService(dbClient, resourceDao, componentService, permissionTemplateService, favoriteService);
+  DefaultRubyComponentService underTest = new DefaultRubyComponentService(dbClient, resourceDao, componentService, permissionTemplateService, favoriteService);
 
   @Test
   public void find_by_key() {
     ComponentDto componentDto = componentDb.insertProject();
 
-    assertThat(service.findByKey(componentDto.getKey())).isNotNull();
+    assertThat(underTest.findByKey(componentDto.getKey())).isNotNull();
   }
 
   @Test
   public void find_by_uuid() {
     ComponentDto componentDto = componentDb.insertProject();
 
-    assertThat(service.findByUuid(componentDto.uuid())).isNotNull();
+    assertThat(underTest.findByUuid(componentDto.uuid())).isNotNull();
   }
 
   @Test
   public void not_find_by_uuid() {
     componentDb.insertProject();
 
-    assertThat(service.findByUuid("UNKNOWN")).isNull();
+    assertThat(underTest.findByUuid("UNKNOWN")).isNull();
   }
 
   @Test
@@ -105,8 +106,9 @@ public class DefaultRubyComponentServiceTest {
     String componentKey = "new-project";
     String componentName = "New Project";
     String qualifier = Qualifiers.PROJECT;
+    when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ComponentDto.class))).thenReturn(true);
 
-    Long result = service.createComponent(componentKey, componentName, qualifier);
+    Long result = underTest.createComponent(componentKey, componentName, qualifier);
 
     ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, componentKey);
     assertThat(project.key()).isEqualTo(componentKey);
@@ -120,7 +122,7 @@ public class DefaultRubyComponentServiceTest {
   @Test(expected = BadRequestException.class)
   public void should_throw_if_malformed_key1() {
     userSession.login("john").setGlobalPermissions(PROVISIONING);
-    service.createComponent("1234", "New Project", Qualifiers.PROJECT);
+    underTest.createComponent("1234", "New Project", Qualifiers.PROJECT);
   }
 
   @Test
@@ -130,7 +132,7 @@ public class DefaultRubyComponentServiceTest {
     Map<String, Object> map = newHashMap();
     map.put("qualifiers", qualifiers);
 
-    List<ResourceDto> resourceDtos = service.findProvisionedProjects(map);
+    List<ResourceDto> resourceDtos = underTest.findProvisionedProjects(map);
     assertThat(resourceDtos).hasSize(1);
   }
 
index d8609659c747d46db132e8695725ab4fcda2eca6..4ac952e85727539ae7a8f99b279bfdb34c64800e 100644 (file)
@@ -113,6 +113,7 @@ public class ReportSubmitterTest {
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
     when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
       .thenReturn(true);
+    when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ComponentDto.class))).thenReturn(true);
 
     underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
 
@@ -133,6 +134,22 @@ public class ReportSubmitterTest {
     }));
   }
 
+  @Test
+  public void no_favorite_when_no_project_creator_permission_on_permission_template() {
+    userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
+
+    when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
+    ComponentDto createdProject = new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
+    when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
+    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+      .thenReturn(true);
+    when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ComponentDto.class))).thenReturn(false);
+
+    underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+
+    verifyZeroInteractions(favoriteService);
+  }
+
   @Test
   public void submit_a_report_on_new_project_with_global_scan_permission() {
     userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);