]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8690 add IT for analysing a project in other organization 1567/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 26 Jan 2017 13:53:31 +0000 (14:53 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 27 Jan 2017 15:55:16 +0000 (16:55 +0100)
it/it-tests/src/test/java/it/organization/OrganizationIt.java
it/it-tests/src/test/java/util/user/GroupManagement.java [new file with mode: 0644]
it/it-tests/src/test/java/util/user/UserRule.java

index d2a0d3f25a2543ed26c59caee80f9342cf666b93..e950f73cbcae6206c76be1302593c49c195770b4 100644 (file)
@@ -30,14 +30,19 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonarqube.ws.Organizations;
+import org.sonarqube.ws.WsComponents;
 import org.sonarqube.ws.client.HttpException;
+import org.sonarqube.ws.client.component.ComponentsService;
 import org.sonarqube.ws.client.organization.CreateWsRequest;
 import org.sonarqube.ws.client.organization.OrganizationService;
 import org.sonarqube.ws.client.organization.SearchWsRequest;
 import org.sonarqube.ws.client.organization.UpdateWsRequest;
 import util.ItUtils;
+import util.user.GroupManagement;
+import util.user.Groups;
 import util.user.UserRule;
 
+import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
 
@@ -237,6 +242,48 @@ public class OrganizationIt {
     expect403HttpError(() -> fooUserOrganizationService.create(createWsRequest));
   }
 
+  @Test
+  public void deleting_an_organization_also_deletes_group_permissions_and_projects_and_check_security() {
+    verifyNoExtraOrganization();
+
+    String orgKeyAndName = "org-key";
+    Organizations.Organization createdOrganization = adminOrganizationService.create(new CreateWsRequest.Builder()
+      .setName(orgKeyAndName)
+      .setKey(orgKeyAndName)
+      .build())
+      .getOrganization();
+    verifySingleSearchResult(createdOrganization, orgKeyAndName, null, null, null);
+
+    GroupManagement groupManagement = userRule.forOrganization(orgKeyAndName);
+
+    userRule.createUser("bob", "bob");
+    groupManagement.createGroup("grp1");
+    groupManagement.createGroup("grp2");
+    groupManagement.associateGroupsToUser("bob", "grp1", "grp2");
+    assertThat(groupManagement.getUserGroups("bob").getGroups())
+      .extracting(Groups.Group::getName)
+      .contains("grp1", "grp2");
+
+    ItUtils.runProjectAnalysis(orchestrator, "shared/xoo-sample",
+        "sonar.organization", orgKeyAndName);
+    ComponentsService componentsService = ItUtils.newAdminWsClient(orchestrator).components();
+    assertThat(searchSampleProject(componentsService).getComponentsList()).hasSize(1);
+
+    adminOrganizationService.delete(orgKeyAndName);
+
+    assertThat(searchSampleProject(componentsService).getComponentsList()).hasSize(0);
+    assertThat(groupManagement.getUserGroups("bob").getGroups())
+        .extracting(Groups.Group::getName)
+        .doesNotContain("grp1", "grp2");
+
+    verifyNoExtraOrganization();
+  }
+
+  private WsComponents.SearchWsResponse searchSampleProject(ComponentsService componentsService) {
+    return componentsService
+      .search(new org.sonarqube.ws.client.component.SearchWsRequest().setQualifiers(singletonList("TRK")).setQuery("sample"));
+  }
+
   private void expect403HttpError(Runnable runnable) {
     try {
       runnable.run();
diff --git a/it/it-tests/src/test/java/util/user/GroupManagement.java b/it/it-tests/src/test/java/util/user/GroupManagement.java
new file mode 100644 (file)
index 0000000..68762bc
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package util.user;
+
+import java.util.List;
+import java.util.Optional;
+import javax.annotation.Nullable;
+
+public interface GroupManagement {
+  void createGroup(String name);
+
+  void createGroup(String name, @Nullable String description);
+
+  void removeGroups(List<String> groupNames);
+
+  void removeGroups(String... groupNames);
+
+  Optional<Groups.Group> getGroupByName(String name);
+
+  Groups getGroups();
+
+  void verifyUserGroupMembership(String userLogin, String... groups);
+
+  Groups getUserGroups(String userLogin);
+
+  void associateGroupsToUser(String userLogin, String... groups);
+}
index a997b9de48c09ec169dbea104e5713b075cd5e8c..8359186e3c6874041935de1527696347fdee5045 100644 (file)
@@ -25,6 +25,7 @@ import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
 import com.sonar.orchestrator.Orchestrator;
 import java.util.List;
+import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.junit.rules.ExternalResource;
@@ -39,15 +40,17 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
 import static util.ItUtils.newAdminWsClient;
 
-public class UserRule extends ExternalResource {
+public class UserRule extends ExternalResource implements GroupManagement {
 
   public static final String ADMIN_LOGIN = "admin";
   private final Orchestrator orchestrator;
 
   private WsClient adminWsClient;
+  private final GroupManagement defaultOrganizationGroupManagement;
 
   private UserRule(Orchestrator orchestrator) {
     this.orchestrator = orchestrator;
+    this.defaultOrganizationGroupManagement = new GroupManagementImpl(null);
   }
 
   public static UserRule from(Orchestrator orchestrator) {
@@ -135,65 +138,149 @@ public class UserRule extends ExternalResource {
   // User groups
   // *****************
 
+  public GroupManagement forOrganization(String organizationKey) {
+    return new GroupManagementImpl(organizationKey);
+  }
+
+  private final class GroupManagementImpl implements GroupManagement {
+    @CheckForNull
+    private final String organizationKey;
+
+    private GroupManagementImpl(@Nullable String organizationKey) {
+      this.organizationKey = organizationKey;
+    }
+
+    @Override
+    public void createGroup(String name) {
+      createGroup(name, null);
+    }
+
+    @Override
+    public void createGroup(String name, @Nullable String description) {
+      PostRequest request = new PostRequest("api/user_groups/create")
+          .setParam("name", name)
+          .setParam("description", description);
+      addOrganizationParam(request);
+      adminWsClient().wsConnector().call(request);
+    }
+
+    private void addOrganizationParam(PostRequest request) {
+      if (organizationKey != null) {
+        request.setParam("organization", organizationKey);
+      }
+    }
+
+    private void addOrganizationParam(GetRequest request) {
+      if (organizationKey != null) {
+        request.setParam("organization", organizationKey);
+      }
+    }
+
+    @Override
+    public void removeGroups(List<String> groupNames) {
+      for (String groupName : groupNames) {
+        if (getGroupByName(groupName).isPresent()) {
+          PostRequest request = new PostRequest("api/user_groups/delete")
+              .setParam("name", groupName);
+          addOrganizationParam(request);
+          adminWsClient().wsConnector().call(request);
+        }
+      }
+    }
+
+    @Override
+    public void removeGroups(String... groupNames) {
+      removeGroups(asList(groupNames));
+    }
+
+    @Override
+    public java.util.Optional<Groups.Group> getGroupByName(String name) {
+      return getGroups().getGroups().stream().filter(new MatchGroupName(name)::apply).findFirst();
+    }
+
+    @Override
+    public Groups getGroups() {
+      GetRequest request = new GetRequest("api/user_groups/search");
+      addOrganizationParam(request);
+      WsResponse response = adminWsClient().wsConnector().call(request);
+      assertThat(response.code()).isEqualTo(200);
+      return Groups.parse(response.content());
+    }
+
+    @Override
+    public void verifyUserGroupMembership(String userLogin, String... groups) {
+      Groups userGroup = getUserGroups(userLogin);
+      List<String> userGroupName = FluentIterable.from(userGroup.getGroups()).transform(ToGroupName.INSTANCE).toList();
+      assertThat(userGroupName).containsOnly(groups);
+    }
+
+    @Override
+    public Groups getUserGroups(String userLogin) {
+      GetRequest request = new GetRequest("api/users/groups")
+          .setParam("login", userLogin)
+          .setParam("selected", "selected");
+      addOrganizationParam(request);
+      WsResponse response = adminWsClient().wsConnector().call(request);
+      assertThat(response.code()).isEqualTo(200);
+      return Groups.parse(response.content());
+    }
+
+    @Override
+    public void associateGroupsToUser(String userLogin, String... groups) {
+      for (String group : groups) {
+        PostRequest request = new PostRequest("api/user_groups/add_user")
+            .setParam("login", userLogin)
+            .setParam("name", group);
+        addOrganizationParam(request);
+        WsResponse response = adminWsClient().wsConnector().call(request);
+        assertThat(response.code()).isEqualTo(204);
+      }
+    }
+  }
+
+  @Override
   public void createGroup(String name) {
-    createGroup(name, null);
+    defaultOrganizationGroupManagement.createGroup(name);
   }
 
+  @Override
   public void createGroup(String name, @Nullable String description) {
-    adminWsClient().wsConnector().call(
-      new PostRequest("api/user_groups/create")
-        .setParam("name", name)
-        .setParam("description", description));
+    defaultOrganizationGroupManagement.createGroup(name, description);
   }
 
+  @Override
   public void removeGroups(List<String> groupNames) {
-    for (String groupName : groupNames) {
-      if (getGroupByName(groupName).isPresent()) {
-        adminWsClient().wsConnector().call(
-          new PostRequest("api/user_groups/delete")
-            .setParam("name", groupName));
-      }
-    }
+    defaultOrganizationGroupManagement.removeGroups(groupNames);
   }
 
+  @Override
   public void removeGroups(String... groupNames) {
-    removeGroups(asList(groupNames));
+    defaultOrganizationGroupManagement.removeGroups(groupNames);
   }
 
-  public Optional<Groups.Group> getGroupByName(String name) {
-    return FluentIterable.from(getGroups().getGroups()).firstMatch(new MatchGroupName(name));
+  @Override
+  public java.util.Optional<Groups.Group> getGroupByName(String name) {
+    return defaultOrganizationGroupManagement.getGroupByName(name);
   }
 
+  @Override
   public Groups getGroups() {
-    WsResponse response = adminWsClient().wsConnector().call(
-      new GetRequest("api/user_groups/search"));
-    assertThat(response.code()).isEqualTo(200);
-    return Groups.parse(response.content());
+    return defaultOrganizationGroupManagement.getGroups();
   }
 
+  @Override
   public void verifyUserGroupMembership(String userLogin, String... groups) {
-    Groups userGroup = getUserGroups(userLogin);
-    List<String> userGroupName = FluentIterable.from(userGroup.getGroups()).transform(ToGroupName.INSTANCE).toList();
-    assertThat(userGroupName).containsOnly(groups);
+    defaultOrganizationGroupManagement.verifyUserGroupMembership(userLogin, groups);
   }
 
+  @Override
   public Groups getUserGroups(String userLogin) {
-    WsResponse response = adminWsClient().wsConnector().call(
-      new GetRequest("api/users/groups")
-        .setParam("login", userLogin)
-        .setParam("selected", "selected"));
-    assertThat(response.code()).isEqualTo(200);
-    return Groups.parse(response.content());
+    return defaultOrganizationGroupManagement.getUserGroups(userLogin);
   }
 
+  @Override
   public void associateGroupsToUser(String userLogin, String... groups) {
-    for (String group : groups) {
-      WsResponse response = adminWsClient().wsConnector().call(
-        new PostRequest("api/user_groups/add_user")
-          .setParam("login", userLogin)
-          .setParam("name", group));
-      assertThat(response.code()).isEqualTo(204);
-    }
+    defaultOrganizationGroupManagement.associateGroupsToUser(userLogin, groups);
   }
 
   private WsClient adminWsClient() {