From 2c9a70a4bac47b14dec82cd9d4b7fce1e4905f8e Mon Sep 17 00:00:00 2001 From: Jacek Date: Fri, 11 Dec 2020 14:32:37 +0100 Subject: [PATCH] SONAR-13999 move project default visibility to global settings --- .../db/organization/OrganizationDao.java | 16 +-- .../db/organization/OrganizationDbTester.java | 9 -- .../MigrationConfigurationModule.java | 2 + .../db/migration/version/v87/DbVersion87.java | 34 +++++++ ...ltProjectVisibilityToGlobalProperties.java | 58 +++++++++++ .../version/v87/DbVersion87Test.java | 42 ++++++++ ...ojectVisibilityToGlobalPropertiesTest.java | 97 +++++++++++++++++++ .../schema.sql | 33 +++++++ .../project/ProjectDefaultVisibility.java | 51 ++++++++++ .../project/ProjectDefaultVisibilityTest.java | 63 ++++++++++++ .../server/ce/queue/ReportSubmitter.java | 24 ++--- .../sonar/server/project/ws/CreateAction.java | 21 ++-- .../server/project/ws/ProjectsWsModule.java | 18 ++-- .../server/project/ws/ProjectsWsSupport.java | 14 +-- .../ws/UpdateDefaultVisibilityAction.java | 17 ++-- .../project/ws/UpdateVisibilityAction.java | 9 +- .../server/ui/ws/OrganizationAction.java | 7 +- .../ce/queue/BranchReportSubmitterTest.java | 20 ++-- .../server/ce/queue/ReportSubmitterTest.java | 11 ++- .../project/ws/BulkDeleteActionTest.java | 13 ++- .../server/project/ws/CreateActionTest.java | 48 ++++----- .../project/ws/ProjectsWsModuleTest.java | 18 +--- .../server/project/ws/SearchActionTest.java | 8 +- .../ws/UpdateDefaultVisibilityActionTest.java | 36 ++++--- .../ws/UpdateVisibilityActionTest.java | 72 ++------------ .../server/ui/ws/OrganizationActionTest.java | 28 ++++-- 26 files changed, 524 insertions(+), 245 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalProperties.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87Test.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest/schema.sql create mode 100644 server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectDefaultVisibility.java create mode 100644 server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectDefaultVisibilityTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java index 7678f09e2c3..0fdbd59b054 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java @@ -124,18 +124,6 @@ public class OrganizationDao implements Dao { getMapper(dbSession).updateDefaultQualityGate(organization.getUuid(), qualityGate.getUuid(), system2.now()); } - public boolean getNewProjectPrivate(DbSession dbSession, String organizationUuid) { - return getMapper(dbSession).selectNewProjectPrivateByUuid(organizationUuid); - } - - public boolean getNewProjectPrivate(DbSession dbSession, OrganizationDto organization) { - return getMapper(dbSession).selectNewProjectPrivateByUuid(organization.getUuid()); - } - - public void setNewProjectPrivate(DbSession dbSession, OrganizationDto organization, boolean newProjectPrivate) { - getMapper(dbSession).updateNewProjectPrivate(organization.getUuid(), newProjectPrivate, system2.now()); - } - public int update(DbSession dbSession, OrganizationDto organization) { checkDto(organization); organization.setUpdatedAt(system2.now()); @@ -148,9 +136,7 @@ public class OrganizationDao implements Dao { public List selectOrganizationsWithNcloc(DbSession dbSession, List organizationUuids) { List result = new ArrayList<>(); - executeLargeUpdates(organizationUuids, chunk -> - result.addAll(getMapper(dbSession).selectOrganizationsWithNcloc(NCLOC_KEY, chunk, BranchType.BRANCH)) - ); + executeLargeUpdates(organizationUuids, chunk -> result.addAll(getMapper(dbSession).selectOrganizationsWithNcloc(NCLOC_KEY, chunk, BranchType.BRANCH))); return result; } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/organization/OrganizationDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/organization/OrganizationDbTester.java index 173c66d2b49..61bfb7ce7f4 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/organization/OrganizationDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/organization/OrganizationDbTester.java @@ -87,13 +87,4 @@ public class OrganizationDbTester { u -> db.getDbClient().organizationMemberDao().insert(db.getSession(), new OrganizationMemberDto().setOrganizationUuid(organization.getUuid()).setUserUuid(u.getUuid()))); db.commit(); } - - public void setNewProjectPrivate(OrganizationDto organization, boolean newProjectPrivate) { - db.getDbClient().organizationDao().setNewProjectPrivate(db.getSession(), organization, newProjectPrivate); - db.commit(); - } - - public boolean getNewProjectPrivate(OrganizationDto organization) { - return db.getDbClient().organizationDao().getNewProjectPrivate(db.getSession(), organization); - } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java index 373250ca965..09d0212fd9e 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java @@ -34,6 +34,7 @@ import org.sonar.server.platform.db.migration.version.v84.util.DropPrimaryKeySql import org.sonar.server.platform.db.migration.version.v84.util.SqlHelper; import org.sonar.server.platform.db.migration.version.v85.DbVersion85; import org.sonar.server.platform.db.migration.version.v86.DbVersion86; +import org.sonar.server.platform.db.migration.version.v87.DbVersion87; public class MigrationConfigurationModule extends Module { @Override @@ -48,6 +49,7 @@ public class MigrationConfigurationModule extends Module { DbVersion84.class, DbVersion85.class, DbVersion86.class, + DbVersion87.class, // migration steps MigrationStepRegistryImpl.class, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java new file mode 100644 index 00000000000..337faf86be2 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.platform.db.migration.version.v87; + +import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; +import org.sonar.server.platform.db.migration.version.DbVersion; + +public class DbVersion87 implements DbVersion { + + @Override + public void addSteps(MigrationStepRegistry registry) { + registry + .add(4200, "Move default project visibility to global properties", MoveDefaultProjectVisibilityToGlobalProperties.class) + + ; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalProperties.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalProperties.java new file mode 100644 index 00000000000..ae8a24a077f --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalProperties.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.platform.db.migration.version.v87; + +import java.sql.SQLException; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; + +public class MoveDefaultProjectVisibilityToGlobalProperties extends DataChange { + + private final UuidFactory uuidFactory; + private final System2 system2; + + public MoveDefaultProjectVisibilityToGlobalProperties(Database db, UuidFactory uuidFactory, System2 system2) { + super(db); + this.uuidFactory = uuidFactory; + this.system2 = system2; + } + + @Override + public void execute(Context context) throws SQLException { + Boolean newProjectPrivate = context.prepareSelect("select new_project_private from organizations where kee = ?") + .setString(1, "default-organization") + .get(row -> row.getNullableBoolean(1)); + + if (newProjectPrivate == null) { + throw new IllegalStateException("Default organization is missing"); + } else { + context.prepareUpsert("insert into properties (uuid, prop_key, is_empty, text_value, created_at) values (?, ?, ?, ?, ?)") + .setString(1, uuidFactory.create()) + .setString(2, "projects.default.visibility") + .setBoolean(3, false) + .setString(4, Boolean.TRUE.equals(newProjectPrivate) ? "private" : "public") + .setLong(5, system2.now()) + .execute() + .commit(); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87Test.java new file mode 100644 index 00000000000..290d88149fa --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/DbVersion87Test.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.platform.db.migration.version.v87; + +import org.junit.Test; +import org.sonar.server.platform.db.migration.version.DbVersion; + +import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty; +import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber; + +public class DbVersion87Test { + + private final DbVersion underTest = new DbVersion87(); + + @Test + public void migrationNumber_starts_at_4100() { + verifyMinimumMigrationNumber(underTest, 4200); + } + + @Test + public void verify_migration_count() { + verifyMigrationNotEmpty(underTest); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest.java new file mode 100644 index 00000000000..1738dc272df --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest.java @@ -0,0 +1,97 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.platform.db.migration.version.v87; + +import java.sql.SQLException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.impl.utils.TestSystem2; +import org.sonar.core.util.UuidFactory; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class MoveDefaultProjectVisibilityToGlobalPropertiesTest { + + private final UuidFactory uuidFactory = UuidFactoryFast.getInstance(); + private final TestSystem2 system2 = new TestSystem2(); + private final long NOW = 1606375781L; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MoveDefaultProjectVisibilityToGlobalPropertiesTest.class, "schema.sql"); + + private final MoveDefaultProjectVisibilityToGlobalProperties underTest = new MoveDefaultProjectVisibilityToGlobalProperties(db.database(), + UuidFactoryFast.getInstance(), system2); + + @Before + public void before() { + system2.setNow(NOW); + } + + @Test + public void fail_if_organization_not_exist() { + assertThatThrownBy(underTest::execute) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Default organization is missing"); + } + + @Test + public void migrate_default_project_visibility_if_set_to_true_on_organization() throws SQLException { + insertDefaultOrganization(true); + + underTest.execute(); + + assertThatDefaultProjectVisibilityIsEqualTo("private"); + } + + @Test + public void migrate_default_project_visibility_if_set_to_false_on_organization() throws SQLException { + insertDefaultOrganization(false); + + underTest.execute(); + + assertThatDefaultProjectVisibilityIsEqualTo("public"); + } + + private void insertDefaultOrganization(boolean newProjectPrivate) { + String uuid = uuidFactory.create(); + db.executeInsert("organizations", + "uuid", uuid, + "kee", "default-organization", + "name", "name" + uuid, + "default_perm_template_project", uuidFactory.create(), + "default_perm_template_port", uuidFactory.create(), + "default_perm_template_app", uuidFactory.create(), + "default_quality_gate_uuid", uuid, + "new_project_private", newProjectPrivate, + "subscription", uuid, + "created_at", NOW, + "updated_at", NOW); + } + + private void assertThatDefaultProjectVisibilityIsEqualTo(String s) { + assertThat(db.selectFirst("select p.text_value, p.created_at from properties p where p.prop_key = 'projects.default.visibility'")) + .containsEntry("TEXT_VALUE", s) + .containsEntry("CREATED_AT", NOW); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest/schema.sql new file mode 100644 index 00000000000..09e88e08ef1 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v87/MoveDefaultProjectVisibilityToGlobalPropertiesTest/schema.sql @@ -0,0 +1,33 @@ +CREATE TABLE "PROPERTIES"( + "PROP_KEY" VARCHAR(512) NOT NULL, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(255) +); +ALTER TABLE "PROPERTIES" ADD CONSTRAINT "PK_PROPERTIES" PRIMARY KEY("UUID"); +CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES"("PROP_KEY"); + +CREATE TABLE "ORGANIZATIONS"( + "UUID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(255) NOT NULL, + "NAME" VARCHAR(255) NOT NULL, + "DESCRIPTION" VARCHAR(256), + "URL" VARCHAR(256), + "AVATAR_URL" VARCHAR(256), + "GUARDED" BOOLEAN, + "DEFAULT_QUALITY_GATE_UUID" VARCHAR(40) NOT NULL, + "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_APP" VARCHAR(40), + "DEFAULT_PERM_TEMPLATE_PORT" VARCHAR(40), + "NEW_PROJECT_PRIVATE" BOOLEAN NOT NULL, + "SUBSCRIPTION" VARCHAR(40) NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "DEFAULT_GROUP_UUID" VARCHAR(40) +); +ALTER TABLE "ORGANIZATIONS" ADD CONSTRAINT "PK_ORGANIZATIONS" PRIMARY KEY("UUID"); +CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS"("KEE"); diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectDefaultVisibility.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectDefaultVisibility.java new file mode 100644 index 00000000000..159df961757 --- /dev/null +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/project/ProjectDefaultVisibility.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.project; + +import java.util.Optional; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.property.PropertyDto; + +public class ProjectDefaultVisibility { + private static final String PROJECTS_DEFAULT_VISIBILITY_PROPERTY_NAME = "projects.default.visibility"; + private final DbClient dbClient; + + public ProjectDefaultVisibility(DbClient dbClient) { + this.dbClient = dbClient; + } + + public Visibility get(DbSession dbSession) { + PropertyDto defaultProjectVisibility = Optional + .ofNullable(dbClient.propertiesDao().selectGlobalProperty(dbSession, PROJECTS_DEFAULT_VISIBILITY_PROPERTY_NAME)) + .orElseThrow(() -> new IllegalStateException("Could not find default project visibility setting")); + return Visibility.parseVisibility(defaultProjectVisibility.getValue()); + } + + public void set(DbSession dbSession, String visibilityLabel) { + set(dbSession, Visibility.parseVisibility(visibilityLabel)); + } + + public void set(DbSession dbSession, Visibility visibility) { + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() + .setKey(PROJECTS_DEFAULT_VISIBILITY_PROPERTY_NAME) + .setValue(visibility.getLabel())); + } +} diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectDefaultVisibilityTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectDefaultVisibilityTest.java new file mode 100644 index 00000000000..66fc1ab6c94 --- /dev/null +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectDefaultVisibilityTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 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.project; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class ProjectDefaultVisibilityTest { + + @Rule + public final DbTester db = DbTester.create(); + + private final ProjectDefaultVisibility underTest = new ProjectDefaultVisibility(db.getDbClient()); + + @Test + public void fail_if_project_visibility_property_not_exist() { + DbSession dbSession = db.getSession(); + assertThatThrownBy(() -> underTest.get(dbSession)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Could not find default project visibility setting"); + } + + @Test + public void set_default_project_visibility() { + underTest.set(db.getSession(), Visibility.PUBLIC); + assertThat(underTest.get(db.getSession())).isEqualTo(Visibility.PUBLIC); + + underTest.set(db.getSession(), Visibility.PRIVATE); + assertThat(underTest.get(db.getSession())).isEqualTo(Visibility.PRIVATE); + } + + @Test + public void set_default_project_visibility_by_string() { + underTest.set(db.getSession(), "private"); + assertThat(underTest.get(db.getSession())).isEqualTo(Visibility.PRIVATE); + + underTest.set(db.getSession(), "public"); + assertThat(underTest.get(db.getSession())).isEqualTo(Visibility.PUBLIC); + } + +} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java index 027fbd8564f..29791529ea1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java @@ -41,9 +41,9 @@ import org.sonar.db.permission.GlobalPermission; import org.sonar.server.component.ComponentUpdater; import org.sonar.server.component.NewComponent; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import static java.lang.String.format; @@ -60,18 +60,18 @@ public class ReportSubmitter { private final PermissionTemplateService permissionTemplateService; private final DbClient dbClient; private final BranchSupport branchSupport; - private final DefaultOrganizationProvider defaultOrganizationProvider; + private final ProjectDefaultVisibility projectDefaultVisibility; public ReportSubmitter(CeQueue queue, UserSession userSession, ComponentUpdater componentUpdater, PermissionTemplateService permissionTemplateService, DbClient dbClient, BranchSupport branchSupport, - DefaultOrganizationProvider defaultOrganizationProvider) { + ProjectDefaultVisibility projectDefaultVisibility) { this.queue = queue; this.userSession = userSession; this.componentUpdater = componentUpdater; this.permissionTemplateService = permissionTemplateService; this.dbClient = dbClient; this.branchSupport = branchSupport; - this.defaultOrganizationProvider = defaultOrganizationProvider; + this.projectDefaultVisibility = projectDefaultVisibility; } public CeTask submit(String projectKey, @Nullable String projectName, Map characteristics, InputStream reportInput) { @@ -122,10 +122,10 @@ public class ReportSubmitter { private void checkScanPermission(ComponentDto project) { // this is a specific and inconsistent behavior. For legacy reasons, "technical users" - // defined on an organization should be able to analyze a project even if + // defined with global scan permission should be able to analyze a project even if // they don't have the direct permission on the project. // That means that dropping the permission on the project does not have any effects - // if user has still the permission on the organization + // if user has still the global permission if (!userSession.hasComponentPermission(UserRole.SCAN, project) && !userSession.hasPermission(GlobalPermission.SCAN)) { throw insufficientPrivilegesException(); } @@ -159,20 +159,20 @@ public class ReportSubmitter { throw insufficientPrivilegesException(); } - // TODO:: remove once we move organization settings somewhere else - String defaultOrgUuid = defaultOrganizationProvider.get().getUuid(); - boolean newProjectPrivate = dbClient.organizationDao().getNewProjectPrivate(dbSession, defaultOrgUuid); - NewComponent newProject = newComponentBuilder() .setKey(componentKey.getKey()) .setName(defaultIfBlank(projectName, componentKey.getKey())) .setQualifier(Qualifiers.PROJECT) - .setPrivate(newProjectPrivate) + .setPrivate(getDefaultVisibility(dbSession).isPrivate()) .build(); return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, c -> { }); } + private Visibility getDefaultVisibility(DbSession dbSession) { + return projectDefaultVisibility.get(dbSession); + } + private CeTask submitReport(DbSession dbSession, InputStream reportInput, ComponentDto project, Map characteristics) { CeTaskSubmit.Builder submit = queue.prepareSubmit(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java index a2a115d1cf2..a0f1f89c958 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java @@ -28,8 +28,8 @@ import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; -import org.sonar.db.organization.OrganizationDto; import org.sonar.server.component.ComponentUpdater; +import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Projects.CreateWsResponse; @@ -55,12 +55,15 @@ public class CreateAction implements ProjectsWsAction { private final DbClient dbClient; private final UserSession userSession; private final ComponentUpdater componentUpdater; + private final ProjectDefaultVisibility projectDefaultVisibility; - public CreateAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater) { + public CreateAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater, + ProjectDefaultVisibility projectDefaultVisibility) { this.support = support; this.dbClient = dbClient; this.userSession = userSession; this.componentUpdater = componentUpdater; + this.projectDefaultVisibility = projectDefaultVisibility; } @Override @@ -105,18 +108,16 @@ public class CreateAction implements ProjectsWsAction { private CreateWsResponse doHandle(CreateRequest request) { try (DbSession dbSession = dbClient.openSession(false)) { - OrganizationDto organization = support.getOrganization(dbSession, request.getOrganization()); userSession.checkPermission(PROVISION_PROJECTS); String visibility = request.getVisibility(); - boolean changeToPrivate = visibility == null ? dbClient.organizationDao().getNewProjectPrivate(dbSession, organization) : "private".equals(visibility); - support.checkCanUpdateProjectsVisibility(organization, changeToPrivate); + boolean changeToPrivate = visibility == null ? projectDefaultVisibility.get(dbSession).isPrivate() : "private".equals(visibility); ComponentDto componentDto = componentUpdater.create(dbSession, newComponentBuilder() - .setKey(request.getProjectKey()) - .setName(request.getName()) - .setPrivate(changeToPrivate) - .setQualifier(PROJECT) - .build(), + .setKey(request.getProjectKey()) + .setName(request.getName()) + .setPrivate(changeToPrivate) + .setQualifier(PROJECT) + .build(), userSession.isLoggedIn() ? userSession.getUuid() : null); return toCreateResponse(componentDto); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java index aebd20fd0aa..ae0e08b31c3 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java @@ -19,22 +19,20 @@ */ package org.sonar.server.project.ws; -import org.sonar.api.config.Configuration; import org.sonar.core.platform.Module; -import org.sonar.process.ProcessProperties; +import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.ProjectLifeCycleListenersImpl; public class ProjectsWsModule extends Module { - private final Configuration configuration; - - public ProjectsWsModule(Configuration configuration) { - this.configuration = configuration; + public ProjectsWsModule() { + // nothing to do } @Override protected void configureModule() { add( + ProjectDefaultVisibility.class, ProjectLifeCycleListenersImpl.class, ProjectsWsSupport.class, ProjectsWs.class, @@ -45,11 +43,7 @@ public class ProjectsWsModule extends Module { BulkUpdateKeyAction.class, SearchMyProjectsAction.class, SearchAction.class, - UpdateVisibilityAction.class); - - if (!configuration.getBoolean(ProcessProperties.Property.SONARCLOUD_ENABLED.getKey()).orElse(false)) { - // Updating default visibility is not available in SonarCloud - add(UpdateDefaultVisibilityAction.class); - } + UpdateVisibilityAction.class, + UpdateDefaultVisibilityAction.class); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java index 7771b63fdb8..3df2a7dc946 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java @@ -25,8 +25,6 @@ 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.server.organization.BillingValidations; -import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.DefaultOrganizationProvider; import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional; @@ -37,12 +35,10 @@ public class ProjectsWsSupport { private final DbClient dbClient; private final DefaultOrganizationProvider organizationProvider; - private final BillingValidationsProxy billingValidations; - public ProjectsWsSupport(DbClient dbClient, DefaultOrganizationProvider organizationProvider, BillingValidationsProxy billingValidations) { + public ProjectsWsSupport(DbClient dbClient, DefaultOrganizationProvider organizationProvider) { this.dbClient = dbClient; this.organizationProvider = organizationProvider; - this.billingValidations = billingValidations; } void addOrganizationParam(WebService.NewAction action) { @@ -60,12 +56,4 @@ public class ProjectsWsSupport { "No organization for key '%s'", organizationKey); } - public void checkCanUpdateProjectsVisibility(OrganizationDto organizationDto, boolean newProjectsPrivate) { - try { - BillingValidations.Organization organization = new BillingValidations.Organization(organizationDto.getKey(), organizationDto.getUuid(), organizationDto.getName()); - billingValidations.checkCanUpdateProjectVisibility(organization, newProjectsPrivate); - } catch (BillingValidations.BillingValidationsException e) { - throw new IllegalArgumentException(e.getMessage()); - } - } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateDefaultVisibilityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateDefaultVisibilityAction.java index 692e583bccc..a91a8f23d57 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateDefaultVisibilityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateDefaultVisibilityAction.java @@ -19,20 +19,17 @@ */ package org.sonar.server.project.ws; -import java.util.Optional; import org.sonar.api.server.ws.Change; 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.server.organization.DefaultOrganizationProvider; +import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; -import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional; public class UpdateDefaultVisibilityAction implements ProjectsWsAction { static final String ACTION = "update_default_visibility"; @@ -40,12 +37,12 @@ public class UpdateDefaultVisibilityAction implements ProjectsWsAction { private final UserSession userSession; private final DbClient dbClient; - private final DefaultOrganizationProvider defaultOrganizationProvider; + private final ProjectDefaultVisibility projectDefaultVisibility; - public UpdateDefaultVisibilityAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider) { + public UpdateDefaultVisibilityAction(UserSession userSession, DbClient dbClient, ProjectDefaultVisibility projectDefaultVisibility) { this.userSession = userSession; this.dbClient = dbClient; - this.defaultOrganizationProvider = defaultOrganizationProvider; + this.projectDefaultVisibility = projectDefaultVisibility; } @Override @@ -67,15 +64,13 @@ public class UpdateDefaultVisibilityAction implements ProjectsWsAction { @Override public void handle(Request request, Response response) throws Exception { - boolean newProjectsPrivate = Visibility.isPrivate(request.mandatoryParam(PARAM_PROJECT_VISIBILITY)); + String newDefaultProjectVisibility = request.mandatoryParam(PARAM_PROJECT_VISIBILITY); try (DbSession dbSession = dbClient.openSession(false)) { - Optional optionalOrganization = dbClient.organizationDao().selectByKey(dbSession, defaultOrganizationProvider.get().getKey()); - OrganizationDto organization = checkFoundWithOptional(optionalOrganization, "No default organization."); if (!userSession.isSystemAdministrator()) { throw insufficientPrivilegesException(); } - dbClient.organizationDao().setNewProjectPrivate(dbSession, organization, newProjectsPrivate); + projectDefaultVisibility.set(dbSession, newDefaultProjectVisibility); dbSession.commit(); } response.noContent(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java index 4496bdec69b..c796084056a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java @@ -33,7 +33,6 @@ import org.sonar.db.DbSession; import org.sonar.db.component.BranchMapper; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentMapper; -import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.GroupPermissionDto; import org.sonar.db.permission.UserPermissionDto; import org.sonar.server.component.ComponentFinder; @@ -43,7 +42,6 @@ import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import org.sonarqube.ws.client.project.ProjectsWsParameters; -import static java.lang.String.format; import static java.util.Collections.singletonList; import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS; import static org.sonar.server.exceptions.BadRequestException.checkRequest; @@ -58,16 +56,14 @@ public class UpdateVisibilityAction implements ProjectsWsAction { private final ComponentFinder componentFinder; private final UserSession userSession; private final ProjectIndexers projectIndexers; - private final ProjectsWsSupport projectsWsSupport; private final UuidFactory uuidFactory; public UpdateVisibilityAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, - ProjectIndexers projectIndexers, ProjectsWsSupport projectsWsSupport, UuidFactory uuidFactory) { + ProjectIndexers projectIndexers, UuidFactory uuidFactory) { this.dbClient = dbClient; this.componentFinder = componentFinder; this.userSession = userSession; this.projectIndexers = projectIndexers; - this.projectsWsSupport = projectsWsSupport; this.uuidFactory = uuidFactory; } @@ -104,9 +100,6 @@ public class UpdateVisibilityAction implements ProjectsWsAction { checkRequest(noPendingTask(dbSession, component), "Component visibility can't be changed as long as it has background task(s) pending or in progress"); if (changeToPrivate != component.isPrivate()) { - OrganizationDto organization = dbClient.organizationDao().selectByUuid(dbSession, component.getOrganizationUuid()) - .orElseThrow(() -> new IllegalStateException(format("Could not find organization with uuid '%s' of project '%s'", component.getOrganizationUuid(), projectKey))); - projectsWsSupport.checkCanUpdateProjectsVisibility(organization, changeToPrivate); setPrivateForRootComponentUuid(dbSession, component, changeToPrivate); if (changeToPrivate) { updatePermissionsToPrivate(dbSession, component); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java index 851224770fe..18f1be6c7dd 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java @@ -37,6 +37,7 @@ import org.sonar.db.organization.OrganizationDto; import org.sonar.server.organization.BillingValidations; import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.DefaultOrganizationProvider; +import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.ui.PageRepository; import org.sonar.server.user.UserSession; @@ -55,14 +56,16 @@ public class OrganizationAction implements NavigationWsAction { private final UserSession userSession; private final PageRepository pageRepository; private final BillingValidationsProxy billingValidations; + private final ProjectDefaultVisibility projectDefaultVisibility; public OrganizationAction(DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, UserSession userSession, PageRepository pageRepository, - BillingValidationsProxy billingValidations) { + BillingValidationsProxy billingValidations, ProjectDefaultVisibility projectDefaultVisibility) { this.dbClient = dbClient; this.defaultOrganizationProvider = defaultOrganizationProvider; this.userSession = userSession; this.pageRepository = pageRepository; this.billingValidations = billingValidations; + this.projectDefaultVisibility = projectDefaultVisibility; } @Override @@ -89,7 +92,7 @@ public class OrganizationAction implements NavigationWsAction { dbClient.organizationDao().selectByKey(dbSession, organizationKey), "No organization with key '%s'", organizationKey); - boolean newProjectPrivate = dbClient.organizationDao().getNewProjectPrivate(dbSession, organization); + boolean newProjectPrivate = projectDefaultVisibility.get(dbSession).isPrivate(); Optional optOrganizationAlmBinding = dbClient.organizationAlmBindingDao().selectByOrganization(dbSession, organization); JsonWriter json = response.newJsonWriter(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchReportSubmitterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchReportSubmitterTest.java index 830cc6a4926..c10bf031576 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchReportSubmitterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/BranchReportSubmitterTest.java @@ -28,6 +28,7 @@ import java.util.Optional; import java.util.Random; import java.util.stream.IntStream; import org.apache.commons.io.IOUtils; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,9 +48,9 @@ import org.sonar.db.user.UserDto; import org.sonar.server.component.ComponentUpdater; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.favorite.FavoriteUpdater; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; import static java.util.Collections.emptyMap; @@ -86,7 +87,7 @@ public class BranchReportSubmitterTest { @Rule public final DbTester db = DbTester.create(System2.INSTANCE); - private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); private final CeQueue queue = mock(CeQueueImpl.class); private final ComponentUpdater componentUpdater = mock(ComponentUpdater.class); @@ -96,7 +97,12 @@ public class BranchReportSubmitterTest { private final BranchSupport branchSupport = spy(new BranchSupport(branchSupportDelegate)); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), branchSupport, - defaultOrganizationProvider); + projectDefaultVisibility); + + @Before + public void before() { + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); + } @Test public void submit_does_not_use_delegate_if_characteristics_are_empty() { @@ -165,8 +171,8 @@ public class BranchReportSubmitterTest { } @Test - public void submit_report_on_missing_branch_of_missing_project_provisions_project_when_org_PROVISION_PROJECT_perm() { - ComponentDto nonExistingProject = newPrivateProjectDto(db.getDefaultOrganization()); + public void submit_report_on_missing_branch_of_missing_project_provisions_project_when_PROVISION_PROJECT_perm() { + ComponentDto nonExistingProject = newPrivateProjectDto(); UserDto user = db.users().insertUser(); userSession.logIn(user) .addPermission(PROVISION_PROJECTS) @@ -218,7 +224,7 @@ public class BranchReportSubmitterTest { @Test public void submit_report_on_missing_branch_of_missing_project_fails_with_ForbiddenException_if_only_scan_permission() { - ComponentDto nonExistingProject = newPrivateProjectDto(db.getDefaultOrganization()); + ComponentDto nonExistingProject = newPrivateProjectDto(); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(SCAN_EXECUTION, nonExistingProject); Map randomCharacteristics = randomNonEmptyMap(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java index b52c3099a7b..46d8e362b44 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Random; import java.util.stream.IntStream; import org.apache.commons.io.IOUtils; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -47,6 +48,8 @@ import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; import static java.lang.String.format; @@ -82,6 +85,7 @@ public class ReportSubmitterTest { public final DbTester db = DbTester.create(); private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); private final CeQueue queue = mock(CeQueueImpl.class); private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); @@ -91,7 +95,12 @@ public class ReportSubmitterTest { private final BranchSupport ossEditionBranchSupport = new BranchSupport(); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), ossEditionBranchSupport, - defaultOrganizationProvider); + projectDefaultVisibility); + + @Before + public void before() { + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); + } @Test public void submit_with_characteristics_fails_with_ISE_when_no_branch_support_delegate() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java index 3cb2e6c5272..213935e18e0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java @@ -46,7 +46,6 @@ import org.sonar.db.project.ProjectDto; import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; -import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.project.Project; import org.sonar.server.project.ProjectLifeCycleListeners; @@ -79,13 +78,13 @@ public class BulkDeleteActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - private ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class); - private DbClient dbClient = db.getDbClient(); - private ProjectsWsSupport support = new ProjectsWsSupport(dbClient, TestDefaultOrganizationProvider.from(db), mock(BillingValidationsProxy.class)); - private ProjectLifeCycleListeners projectLifeCycleListeners = mock(ProjectLifeCycleListeners.class); + private final ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class); + private final DbClient dbClient = db.getDbClient(); + private final ProjectsWsSupport support = new ProjectsWsSupport(dbClient, TestDefaultOrganizationProvider.from(db)); + private final ProjectLifeCycleListeners projectLifeCycleListeners = mock(ProjectLifeCycleListeners.class); - private BulkDeleteAction underTest = new BulkDeleteAction(componentCleanerService, dbClient, userSession, support, projectLifeCycleListeners); - private WsActionTester ws = new WsActionTester(underTest); + private final BulkDeleteAction underTest = new BulkDeleteAction(componentCleanerService, dbClient, userSession, support, projectLifeCycleListeners); + private final WsActionTester ws = new WsActionTester(underTest); private OrganizationDto org1; private OrganizationDto org2; diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java index 3175c3a8c50..6b287296cda 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java @@ -21,6 +21,7 @@ package org.sonar.server.project.ws; import com.google.common.base.Strings; import javax.annotation.Nullable; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -44,6 +45,8 @@ import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; import org.sonar.server.project.ws.CreateAction.CreateRequest; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; @@ -73,7 +76,7 @@ public class CreateActionTest { private static final String DEFAULT_PROJECT_KEY = "project-key"; private static final String DEFAULT_PROJECT_NAME = "project-name"; - private System2 system2 = System2.INSTANCE; + private final System2 system2 = System2.INSTANCE; @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -84,16 +87,23 @@ public class CreateActionTest { @Rule public I18nRule i18n = new I18nRule().put("qualifier.TRK", "Project"); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); - private TestProjectIndexers projectIndexers = new TestProjectIndexers(); - private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); - private WsActionTester ws = new WsActionTester( + private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); + private final BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); + private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); + private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); + private final WsActionTester ws = new WsActionTester( new CreateAction( - new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider, billingValidations), + new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider), db.getDbClient(), userSession, new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), - projectIndexers, new SequenceUuidFactory(), defaultOrganizationProvider))); + projectIndexers, new SequenceUuidFactory(), defaultOrganizationProvider), + projectDefaultVisibility)); + + @Before + public void before() { + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); + } @Test public void create_project() { @@ -145,7 +155,7 @@ public class CreateActionTest { @Test public void apply_default_project_visibility_public() { OrganizationDto organization = db.organizations().insert(); - db.organizations().setNewProjectPrivate(organization, false); + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); userSession.addPermission(PROVISION_PROJECTS); CreateWsResponse result = ws.newRequest() @@ -160,7 +170,7 @@ public class CreateActionTest { @Test public void apply_default_project_visibility_private() { OrganizationDto organization = db.organizations().insert(); - db.organizations().setNewProjectPrivate(organization, true); + when(projectDefaultVisibility.get(any())).thenReturn(PRIVATE); userSession.addPermission(PROVISION_PROJECTS); CreateWsResponse result = ws.newRequest() @@ -240,24 +250,6 @@ public class CreateActionTest { assertThat(db.favorites().hasNoFavorite(project)).isTrue(); } - @Test - public void fail_to_create_private_projects_when_organization_is_not_allowed_to_use_private_projects() { - OrganizationDto organization = db.organizations().insert(); - userSession.addPermission(PROVISION_PROJECTS); - doThrow(new BillingValidationsException("This organization cannot use project private")).when(billingValidations) - .checkCanUpdateProjectVisibility(any(BillingValidations.Organization.class), eq(true)); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("This organization cannot use project private"); - - ws.newRequest() - .setParam("project", DEFAULT_PROJECT_KEY) - .setParam("name", DEFAULT_PROJECT_NAME) - .setParam("organization", organization.getKey()) - .setParam("visibility", "private") - .executeProtobuf(CreateWsResponse.class); - } - @Test public void fail_when_project_already_exists() { OrganizationDto organization = db.organizations().insert(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java index 9de4490ac8c..f9f1004d616 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java @@ -20,32 +20,18 @@ package org.sonar.server.project.ws; import org.junit.Test; -import org.sonar.api.config.internal.ConfigurationBridge; -import org.sonar.api.config.internal.MapSettings; import org.sonar.core.platform.ComponentContainer; -import org.sonar.process.ProcessProperties; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class ProjectsWsModuleTest { - private MapSettings settings = new MapSettings(); - @Test public void verify_count_of_added_components_on_SonarQube() { ComponentContainer container = new ComponentContainer(); - new ProjectsWsModule(new ConfigurationBridge(settings)).configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 12); - } - - @Test - public void verify_count_of_added_components_on_SonarCloud() { - ComponentContainer container = new ComponentContainer(); - settings.setProperty(ProcessProperties.Property.SONARCLOUD_ENABLED.getKey(), true); - - new ProjectsWsModule(new ConfigurationBridge(settings)).configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11); + new ProjectsWsModule().configure(container); + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 13); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/SearchActionTest.java index 67a52e3fb8a..b04b54364df 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/SearchActionTest.java @@ -37,7 +37,6 @@ import org.sonar.db.component.ComponentTesting; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.tester.UserSessionRule; @@ -52,7 +51,6 @@ import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static java.util.Optional.ofNullable; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; import static org.sonar.api.server.ws.WebService.Param.PAGE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; @@ -88,10 +86,10 @@ public class SearchActionTest { @Rule public DbTester db = DbTester.create(); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private WsActionTester ws = new WsActionTester( - new SearchAction(db.getDbClient(), userSession, new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider, mock(BillingValidationsProxy.class)))); + private final WsActionTester ws = new WsActionTester( + new SearchAction(db.getDbClient(), userSession, new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider))); @Test public void search_by_key_query_with_partial_match_case_insensitive() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateDefaultVisibilityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateDefaultVisibilityActionTest.java index de8a8a99aec..285023d242c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateDefaultVisibilityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateDefaultVisibilityActionTest.java @@ -21,66 +21,70 @@ package org.sonar.server.project.ws; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbTester; import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.DefaultOrganizationProviderImpl; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; 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.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.server.project.ws.UpdateDefaultVisibilityAction.ACTION; import static org.sonar.server.project.ws.UpdateDefaultVisibilityAction.PARAM_PROJECT_VISIBILITY; public class UpdateDefaultVisibilityActionTest { @Rule - public DbTester dbTester = DbTester.create(); + public final DbTester dbTester = DbTester.create(); @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); + public final UserSessionRule userSession = UserSessionRule.standalone(); + public final ProjectDefaultVisibility projectDefaultVisibility = new ProjectDefaultVisibility(dbTester.getDbClient()); - private DefaultOrganizationProvider defaultOrganizationProvider = new DefaultOrganizationProviderImpl(dbTester.getDbClient()); - private UpdateDefaultVisibilityAction underTest = new UpdateDefaultVisibilityAction(userSession, dbTester.getDbClient(), defaultOrganizationProvider); - private WsActionTester wsTester = new WsActionTester(underTest); + private final UpdateDefaultVisibilityAction underTest = new UpdateDefaultVisibilityAction(userSession, dbTester.getDbClient(), + projectDefaultVisibility); + private final WsActionTester wsTester = new WsActionTester(underTest); @Test public void change_project_visibility_to_private() { + projectDefaultVisibility.set(dbTester.getSession(), Visibility.PUBLIC); + dbTester.commit(); + userSession.logIn().setSystemAdministrator(); wsTester.newRequest() .setParam(PARAM_PROJECT_VISIBILITY, "private") .execute(); - assertThat(dbTester.getDbClient().organizationDao().getNewProjectPrivate(dbTester.getSession(), dbTester.getDefaultOrganization())).isTrue(); + assertThat(projectDefaultVisibility.get(dbTester.getSession())).isEqualTo(Visibility.PRIVATE); } @Test public void change_project_visibility_to_public() { - dbTester.organizations().setNewProjectPrivate(dbTester.getDefaultOrganization(), true); + projectDefaultVisibility.set(dbTester.getSession(), Visibility.PRIVATE); + dbTester.commit(); + userSession.logIn().setSystemAdministrator(); wsTester.newRequest() .setParam(PARAM_PROJECT_VISIBILITY, "public") .execute(); - assertThat(dbTester.organizations().getNewProjectPrivate(dbTester.getDefaultOrganization())).isFalse(); + assertThat(projectDefaultVisibility.get(dbTester.getSession())).isEqualTo(Visibility.PUBLIC); } @Test - public void fail_if_not_loggued_as_system_administrator() { + public void fail_if_not_logged_as_system_administrator() { userSession.logIn(); TestRequest request = wsTester.newRequest() .setParam(PARAM_PROJECT_VISIBILITY, "private"); - expectedException.expect(ForbiddenException.class); - request.execute(); + assertThatThrownBy(request::execute) + .isInstanceOf(ForbiddenException.class); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java index 35af1b3a973..9cbc0517ef4 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java @@ -57,9 +57,6 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.UnauthorizedException; -import org.sonar.server.organization.BillingValidations; -import org.sonar.server.organization.BillingValidationsProxy; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.PermissionServiceImpl; import org.sonar.server.permission.index.FooIndexDefinition; @@ -71,12 +68,7 @@ import static java.lang.String.format; import static java.util.Arrays.stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; import static org.sonar.db.component.ComponentTesting.newProjectCopy; -import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto; public class UpdateVisibilityActionTest { private static final String PARAM_VISIBILITY = "visibility"; @@ -96,21 +88,19 @@ public class UpdateVisibilityActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT); - private PermissionService permissionService = new PermissionServiceImpl(resourceTypes); + private final ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT); + private final PermissionService permissionService = new PermissionServiceImpl(resourceTypes); private final Set PROJECT_PERMISSIONS_BUT_USER_AND_CODEVIEWER = permissionService.getAllProjectPermissions().stream() .filter(perm -> !perm.equals(UserRole.USER) && !perm.equals(UserRole.CODEVIEWER)) .collect(MoreCollectors.toSet(permissionService.getAllProjectPermissions().size() - 2)); - private DbClient dbClient = dbTester.getDbClient(); - private DbSession dbSession = dbTester.getSession(); - private TestProjectIndexers projectIndexers = new TestProjectIndexers(); - private BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); + private final DbClient dbClient = dbTester.getDbClient(); + private final DbSession dbSession = dbTester.getSession(); + private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); - private ProjectsWsSupport wsSupport = new ProjectsWsSupport(dbClient, TestDefaultOrganizationProvider.from(dbTester), billingValidations); - private UpdateVisibilityAction underTest = new UpdateVisibilityAction(dbClient, TestComponentFinder.from(dbTester), - userSessionRule, projectIndexers, wsSupport, new SequenceUuidFactory()); - private WsActionTester ws = new WsActionTester(underTest); + private final UpdateVisibilityAction underTest = new UpdateVisibilityAction(dbClient, TestComponentFinder.from(dbTester), + userSessionRule, projectIndexers, new SequenceUuidFactory()); + private final WsActionTester ws = new WsActionTester(underTest); private final Random random = new Random(); private final String randomVisibility = random.nextBoolean() ? PUBLIC : PRIVATE; @@ -292,21 +282,6 @@ public class UpdateVisibilityActionTest { request.execute(); } - @Test - public void execute_throws_ISE_when_project_organization_uuid_does_not_match_existing_organization() { - // Organization is not persisted - OrganizationDto organization = newOrganizationDto(); - ComponentDto project = dbTester.components().insertPublicProject(organization); - userSessionRule.addProjectPermission(UserRole.ADMIN, project); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage(format("Could not find organization with uuid '%s' of project '%s'", organization.getUuid(), project.getDbKey())); - - request.setParam(PARAM_PROJECT, project.getDbKey()) - .setParam(PARAM_VISIBILITY, PRIVATE) - .execute(); - } - @Test public void execute_changes_private_flag_of_specified_project_and_all_children_to_specified_new_visibility() { ComponentDto project = randomPublicOrPrivateProject(); @@ -595,37 +570,6 @@ public class UpdateVisibilityActionTest { .containsOnly(UserRole.ADMIN); } - @Test - public void fail_to_update_visibility_to_private_when_organization_is_not_allowed_to_use_private_projects() { - OrganizationDto organization = dbTester.organizations().insert(); - ComponentDto project = dbTester.components().insertPublicProject(organization); - dbTester.organizations().setNewProjectPrivate(organization, true); - userSessionRule.addProjectPermission(UserRole.ADMIN, project); - doThrow(new BillingValidations.BillingValidationsException("This organization cannot use project private")).when(billingValidations) - .checkCanUpdateProjectVisibility(any(BillingValidations.Organization.class), eq(true)); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("This organization cannot use project private"); - - request.setParam(PARAM_PROJECT, project.getDbKey()) - .setParam(PARAM_VISIBILITY, PRIVATE) - .execute(); - } - - @Test - public void does_not_fail_to_update_visibility_to_public_when_organization_is_not_allowed_to_use_private_projects() { - OrganizationDto organization = dbTester.organizations().insert(); - ComponentDto project = dbTester.components().insertPublicProject(organization); - dbTester.organizations().setNewProjectPrivate(organization, true); - userSessionRule.addProjectPermission(UserRole.ADMIN, project); - doThrow(new BillingValidations.BillingValidationsException("This organization cannot use project private")).when(billingValidations) - .checkCanUpdateProjectVisibility(any(BillingValidations.Organization.class), eq(true)); - - request.setParam(PARAM_PROJECT, project.getDbKey()) - .setParam(PARAM_VISIBILITY, PUBLIC) - .execute(); - } - @Test public void fail_when_using_branch_db_key() { ComponentDto project = dbTester.components().insertPrivateProject(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java index 0624c079054..e9cfbbea51d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java @@ -20,6 +20,7 @@ package org.sonar.server.ui.ws; import javax.annotation.Nullable; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -40,6 +41,8 @@ import org.sonar.server.organization.BillingValidations; import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.project.ProjectDefaultVisibility; +import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ui.PageRepository; import org.sonar.server.ws.TestRequest; @@ -68,12 +71,19 @@ public class OrganizationActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - private DbClient dbClient = db.getDbClient(); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private PageRepository pageRepository = mock(PageRepository.class); - private BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); + private final DbClient dbClient = db.getDbClient(); + private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private final PageRepository pageRepository = mock(PageRepository.class); + private final BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); + private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); - private WsActionTester ws = new WsActionTester(new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations)); + private WsActionTester ws = new WsActionTester( + new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations, projectDefaultVisibility)); + + @Before + public void before() { + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); + } @Test public void filter_out_admin_pages_when_user_is_not_admin() { @@ -94,9 +104,9 @@ public class OrganizationActionTest { @Test public void returns_project_visibility_private() { OrganizationDto organization = db.organizations().insert(); - db.organizations().setNewProjectPrivate(organization, true); userSession.logIn().addPermission(PROVISION_PROJECTS); + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); TestResponse response = executeRequest(organization); assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"projectVisibility\": \"private\"}}"); @@ -104,10 +114,10 @@ public class OrganizationActionTest { @Test public void returns_project_visibility_public() { - OrganizationDto organization = db.organizations().insert(); - db.organizations().setNewProjectPrivate(organization, false); + when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); userSession.logIn().addPermission(PROVISION_PROJECTS); + OrganizationDto organization = db.organizations().insert(); TestResponse response = executeRequest(organization); assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"projectVisibility\": \"public\"}}"); @@ -240,7 +250,7 @@ public class OrganizationActionTest { } }}); pageRepository.start(); - ws = new WsActionTester(new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations)); + ws = new WsActionTester(new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations, projectDefaultVisibility)); } private TestResponse executeRequest(@Nullable OrganizationDto organization) { -- 2.39.5