]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9054 WS api/navigation/organization filter out billing pages for personal organ... 1987/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Apr 2017 07:03:58 +0000 (09:03 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Apr 2017 09:29:37 +0000 (11:29 +0200)
server/sonar-server/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java
server/sonar-server/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java

index dcecc5ef1dbb1dd24ebe8b116d008f90775d5bdc..a834d1c8372f90d1cac207c2094721a0b2c9bf3f 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.ui.ws;
 
 import java.util.List;
 import java.util.function.Consumer;
+import java.util.function.Predicate;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -34,6 +35,7 @@ import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.ui.PageRepository;
 import org.sonar.server.user.UserSession;
 
+import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.server.ws.KeyExamples.KEY_ORG_EXAMPLE_001;
 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
 
@@ -92,11 +94,12 @@ public class OrganizationAction implements NavigationWsAction {
       .prop("canProvisionProjects", userSession.hasPermission(OrganizationPermission.PROVISION_PROJECTS, organization))
       .prop("canDelete", organization.isGuarded() ? userSession.isSystemAdministrator() : userSession.hasPermission(OrganizationPermission.ADMINISTER, organization))
       .prop("isDefault", organization.getKey().equals(defaultOrganizationProvider.get().getKey()));
-    List<Page> pages = pageRepository.getOrganizationPages(false);
+    Predicate<Page> personalOrgForBilling = page -> organization.getUserId() == null || !page.getKey().startsWith("billing/");
+    List<Page> pages = pageRepository.getOrganizationPages(false).stream().filter(personalOrgForBilling).collect(toList());
     json.name("pages");
     writePages(json, pages);
     if (userSession.hasPermission(OrganizationPermission.ADMINISTER, organization)) {
-      List<Page> adminPages = pageRepository.getOrganizationPages(true);
+      List<Page> adminPages = pageRepository.getOrganizationPages(true).stream().filter(personalOrgForBilling).collect(toList());
       json.name("adminPages");
       writePages(json, adminPages);
     }
index 46ab77d70f63249aeeb6ca0d8bc25edd44b83c11..813f070d5abb4200ad6b6745eda2dfd91a2e8481 100644 (file)
@@ -119,6 +119,40 @@ public class OrganizationActionTest {
       .doesNotContain("my-plugin/org-admin-page");
   }
 
+  @Test
+  public void filter_out_billing_pages_for_personal_organizations() {
+    initWithPages(
+      Page.builder("my-plugin/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+      Page.builder("my-plugin/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build(),
+      Page.builder("billing/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+      Page.builder("billing/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()
+    );
+    OrganizationDto organization = dbTester.organizations().insert(dto -> dto.setUserId(101));
+    userSession.logIn().addPermission(ADMINISTER, organization);
+
+    TestResponse response = executeRequest(organization);
+
+    assertThat(response.getInput())
+      .contains("my-plugin/org-page", "my-plugin/org-admin-page")
+      .doesNotContain("billing/org-page")
+      .doesNotContain("billing/org-admin-page");
+  }
+
+  @Test
+  public void include_billing_pages_for_non_personal_organizations() {
+    initWithPages(
+      Page.builder("billing/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+      Page.builder("billing/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()
+    );
+    OrganizationDto organization = dbTester.organizations().insert(dto -> dto.setUserId(null));
+    userSession.logIn().addPermission(ADMINISTER, organization);
+
+    TestResponse response = executeRequest(organization);
+
+    assertThat(response.getInput())
+      .contains("billing/org-page", "billing/org-admin-page");
+  }
+
   @Test
   public void returns_non_admin_and_canDelete_false_when_user_not_logged_in_and_key_is_the_default_organization() {
     TestResponse response = executeRequest(dbTester.getDefaultOrganization());