aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>2017-04-25 11:44:28 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-04-27 14:42:50 +0200
commit54ee03bb9babd668809cf6c7725f24bc218a6f02 (patch)
tree9f2d64979cd04ee8593f065ae01144e41b501ac5 /server
parent0c6a97c1ed07db396062b3cadaff94db5bfcb419 (diff)
downloadsonarqube-54ee03bb9babd668809cf6c7725f24bc218a6f02.tar.gz
sonarqube-54ee03bb9babd668809cf6c7725f24bc218a6f02.zip
SONAR-9106 new ws api/organizations/update_project_visibility
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDbTester.java8
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsModule.java3
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/organization/ws/UpdateProjectVisibilityAction.java81
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/organization/ws/OrganizationsWsModuleTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateProjectVisibilityActionTest.java123
5 files changed, 215 insertions, 2 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDbTester.java
index 7155e5ff388..af54fecab8e 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDbTester.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDbTester.java
@@ -93,4 +93,12 @@ public class OrganizationDbTester {
dbTester.commit();
}
+ public void setNewProjectPrivate(OrganizationDto organization, boolean newProjectPrivate) {
+ dbTester.getDbClient().organizationDao().setNewProjectPrivate(dbTester.getSession(), organization, newProjectPrivate);
+ dbTester.commit();
+ }
+
+ public boolean getNewProjectPrivate(OrganizationDto organization) {
+ return dbTester.getDbClient().organizationDao().getNewProjectPrivate(dbTester.getSession(), organization);
+ }
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsModule.java
index e521c412d13..dccacc427fd 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsModule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsModule.java
@@ -37,7 +37,8 @@ public class OrganizationsWsModule extends Module {
SearchAction.class,
SearchMembersAction.class,
SearchMyOrganizationsAction.class,
- UpdateAction.class);
+ UpdateAction.class,
+ UpdateProjectVisibilityAction.class);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/UpdateProjectVisibilityAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/UpdateProjectVisibilityAction.java
new file mode 100644
index 00000000000..64c195c7ac3
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/UpdateProjectVisibilityAction.java
@@ -0,0 +1,81 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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 org.sonar.server.organization.ws;
+
+import java.util.Optional;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.permission.OrganizationPermission;
+import org.sonar.server.user.UserSession;
+
+import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+
+public class UpdateProjectVisibilityAction implements OrganizationsWsAction {
+ static final String ACTION = "update_project_visibility";
+ static final String PARAM_PROJECT_VISIBILITY = "projectVisibility";
+
+ private final UserSession userSession;
+ private final DbClient dbClient;
+
+ public UpdateProjectVisibilityAction(UserSession userSession, DbClient dbClient) {
+ this.userSession = userSession;
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ WebService.NewAction action = context.createAction(ACTION)
+ .setPost(true)
+ .setDescription("Update the default visibility for new projects of the specified organization.")
+ .setInternal(true)
+ .setSince("6.4")
+ .setHandler(this);
+
+ action.createParam(PARAM_ORGANIZATION)
+ .setRequired(true)
+ .setDescription("Organization key")
+ .setExampleValue("foo-company");
+
+ action.createParam(PARAM_PROJECT_VISIBILITY)
+ .setRequired(true)
+ .setDescription("Default visibility for projects")
+ .setPossibleValues("private", "public");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION);
+ boolean newProjectsPrivate = "private".equals(request.mandatoryParam(PARAM_PROJECT_VISIBILITY));
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ Optional<OrganizationDto> optionalOrganization = dbClient.organizationDao().selectByKey(dbSession, organizationKey);
+ OrganizationDto organization = checkFoundWithOptional(optionalOrganization, "No organization with key '" + organizationKey + "' can be found.");
+ userSession.checkPermission(OrganizationPermission.ADMINISTER, organization.getUuid());
+ dbClient.organizationDao().setNewProjectPrivate(dbSession, organization, newProjectsPrivate);
+ dbSession.commit();
+ }
+ response.noContent();
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/OrganizationsWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/OrganizationsWsModuleTest.java
index 9e12ededad7..3ced226dcae 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/OrganizationsWsModuleTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/OrganizationsWsModuleTest.java
@@ -33,7 +33,7 @@ public class OrganizationsWsModuleTest {
ComponentContainer container = new ComponentContainer();
underTest.configure(container);
assertThat(container.getPicoContainer().getComponentAdapters())
- .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11);
+ .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 12);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateProjectVisibilityActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateProjectVisibilityActionTest.java
new file mode 100644
index 00000000000..7e03a0ec794
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateProjectVisibilityActionTest.java
@@ -0,0 +1,123 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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 org.sonar.server.organization.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
+import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
+import static org.sonar.server.organization.ws.UpdateProjectVisibilityAction.ACTION;
+import static org.sonar.server.organization.ws.UpdateProjectVisibilityAction.PARAM_PROJECT_VISIBILITY;
+
+public class UpdateProjectVisibilityActionTest {
+ @Rule
+ public DbTester dbTester = DbTester.create();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private UpdateProjectVisibilityAction underTest = new UpdateProjectVisibilityAction(userSession, dbTester.getDbClient());
+ private WsActionTester wsTester = new WsActionTester(underTest);
+
+ @Test
+ public void verify_define() {
+ WebService.Action action = wsTester.getDef();
+ assertThat(action.key()).isEqualTo(ACTION);
+ assertThat(action.isPost()).isTrue();
+ assertThat(action.description()).isNotEmpty();
+ assertThat(action.isInternal()).isTrue();
+ assertThat(action.since()).isEqualTo("6.4");
+ assertThat(action.handler()).isEqualTo(underTest);
+ assertThat(action.changelog()).isEmpty();
+
+ WebService.Param organization = action.param(PARAM_ORGANIZATION);
+ assertThat(organization.isRequired()).isTrue();
+ assertThat(organization.exampleValue()).isEqualTo("foo-company");
+ assertThat(organization.description()).isEqualTo("Organization key");
+
+ WebService.Param projectVisibility = action.param(PARAM_PROJECT_VISIBILITY);
+ assertThat(projectVisibility.isRequired()).isTrue();
+ assertThat(projectVisibility.possibleValues()).containsExactlyInAnyOrder("private", "public");
+ assertThat(projectVisibility.description()).isEqualTo("Default visibility for projects");
+ }
+
+ @Test
+ public void should_change_project_visibility_to_private() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ dbTester.organizations().setNewProjectPrivate(organization, false);
+ userSession.logIn().addPermission(ADMINISTER, organization);
+
+ wsTester.newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_VISIBILITY, "private")
+ .execute();
+
+ assertThat(dbTester.getDbClient().organizationDao().getNewProjectPrivate(dbTester.getSession(), organization)).isTrue();
+ }
+
+ @Test
+ public void should_change_project_visibility_to_public() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ dbTester.organizations().setNewProjectPrivate(organization, true);
+ userSession.logIn().addPermission(ADMINISTER, organization);
+
+ wsTester.newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_VISIBILITY, "public")
+ .execute();
+ dbTester.organizations().getNewProjectPrivate(organization);
+
+ assertThat(dbTester.organizations().getNewProjectPrivate(organization)).isFalse();
+ }
+
+ @Test
+ public void should_fail_if_organization_does_not_exist() {
+ TestRequest request = wsTester.newRequest()
+ .setParam(PARAM_ORGANIZATION, "does not exist")
+ .setParam(PARAM_PROJECT_VISIBILITY, "private");
+
+ expectedException.expect(NotFoundException.class);
+ request.execute();
+ }
+
+ @Test
+ public void should_fail_if_permissions_are_missing() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ TestRequest request = wsTester.newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_VISIBILITY, "private");
+
+ expectedException.expect(ForbiddenException.class);
+ request.execute();
+ }
+
+}