123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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.setting.ws;
-
- import java.util.Random;
- import javax.annotation.Nullable;
-
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
- import org.sonar.api.config.PropertyDefinition;
- import org.sonar.api.config.PropertyDefinitions;
- import org.sonar.api.server.ws.WebService;
- import org.sonar.api.server.ws.WebService.Param;
- import org.sonar.api.utils.System2;
- import org.sonar.api.web.UserRole;
- import org.sonar.db.DbClient;
- import org.sonar.db.DbSession;
- import org.sonar.db.DbTester;
- import org.sonar.db.component.ComponentDbTester;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.component.ComponentTesting;
- import org.sonar.db.property.PropertyDbTester;
- import org.sonar.db.property.PropertyQuery;
- import org.sonar.db.user.UserDto;
- import org.sonar.db.user.UserTesting;
- import org.sonar.process.ProcessProperties;
- import org.sonar.server.component.ComponentFinder;
- import org.sonar.server.component.TestComponentFinder;
- import org.sonar.server.exceptions.BadRequestException;
- import org.sonar.server.exceptions.ForbiddenException;
- import org.sonar.server.exceptions.NotFoundException;
- import org.sonar.server.l18n.I18nRule;
- import org.sonar.server.tester.UserSessionRule;
- import org.sonar.server.ws.TestRequest;
- import org.sonar.server.ws.TestResponse;
- import org.sonar.server.ws.WsActionTester;
- import org.sonarqube.ws.MediaTypes;
-
- import static java.lang.String.format;
- import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.sonar.api.resources.Qualifiers.PROJECT;
- import static org.sonar.api.resources.Qualifiers.VIEW;
- import static org.sonar.api.web.UserRole.ADMIN;
- import static org.sonar.api.web.UserRole.USER;
- import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
- import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
- import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
-
- public class ResetActionTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private I18nRule i18n = new I18nRule();
- private PropertyDbTester propertyDb = new PropertyDbTester(db);
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private PropertyDefinitions definitions = new PropertyDefinitions(System2.INSTANCE);
- private SettingsUpdater settingsUpdater = new SettingsUpdater(dbClient, definitions);
- private SettingValidations settingValidations = new SettingValidations(definitions, dbClient, i18n);
- private ComponentDto project;
- private ResetAction underTest = new ResetAction(dbClient, componentFinder, settingsUpdater, userSession, definitions, settingValidations);
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Before
- public void setUp() {
- project = componentDb.insertComponent(ComponentTesting.newPrivateProjectDto());
- }
-
- @Test
- public void remove_global_setting() {
- logInAsSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo").build());
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
-
- executeRequestOnGlobalSetting("foo");
- assertGlobalPropertyDoesNotExist("foo");
- }
-
- @Test
- public void remove_global_setting_even_if_not_defined() {
- logInAsSystemAdministrator();
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
-
- executeRequestOnGlobalSetting("foo");
- assertGlobalPropertyDoesNotExist("foo");
- }
-
- @Test
- public void remove_component_setting() {
- logInAsProjectAdmin();
- definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
- propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
-
- executeRequestOnProjectSetting("foo");
- assertProjectPropertyDoesNotExist("foo");
- }
-
- @Test
- public void remove_component_setting_even_if_not_defined() {
- logInAsProjectAdmin();
- propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
-
- executeRequestOnProjectSetting("foo");
- assertProjectPropertyDoesNotExist("foo");
- }
-
- @Test
- public void remove_hidden_setting() {
- logInAsSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo").hidden().build());
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
-
- executeRequestOnGlobalSetting("foo");
- assertGlobalPropertyDoesNotExist("foo");
- }
-
- @Test
- public void ignore_project_setting_when_removing_global_setting() {
- logInAsSystemAdministrator();
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
- propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
-
- executeRequestOnGlobalSetting("foo");
-
- assertGlobalPropertyDoesNotExist("foo");
- assertProjectPropertyExists("foo");
- }
-
- @Test
- public void ignore_global_setting_when_removing_project_setting() {
- logInAsProjectAdmin();
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
- propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
-
- executeRequestOnProjectSetting("foo");
-
- assertGlobalPropertyExists("foo");
- assertProjectPropertyDoesNotExist("foo");
- }
-
- @Test
- public void ignore_user_setting_when_removing_global_setting() {
- logInAsSystemAdministrator();
- UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto());
- propertyDb.insertProperties(newUserPropertyDto("foo", "one", user));
-
- executeRequestOnGlobalSetting("foo");
- assertUserPropertyExists("foo", user);
- }
-
- @Test
- public void ignore_user_setting_when_removing_project_setting() {
- logInAsProjectAdmin();
- UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto());
- propertyDb.insertProperties(newUserPropertyDto("foo", "one", user));
-
- executeRequestOnProjectSetting("foo");
- assertUserPropertyExists("foo", user);
- }
-
- @Test
- public void ignore_unknown_setting_key() {
- logInAsSystemAdministrator();
-
- executeRequestOnGlobalSetting("unknown");
- }
-
- @Test
- public void remove_setting_by_deprecated_key() {
- logInAsSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo").deprecatedKey("old").build());
- propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
-
- executeRequestOnGlobalSetting("old");
- assertGlobalPropertyDoesNotExist("foo");
- }
-
- @Test
- public void remove_setting_on_branch() {
- ComponentDto project = db.components().insertPublicProject();
- ComponentDto branch = db.components().insertProjectBranch(project);
- definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
- propertyDb.insertProperties(newComponentPropertyDto(branch).setKey("foo").setValue("value"));
- userSession.logIn().addProjectPermission(ADMIN, project);
-
- ws.newRequest()
- .setMediaType(MediaTypes.PROTOBUF)
- .setParam("keys", "foo")
- .setParam("component", branch.getKey())
- .setParam("branch", branch.getBranch())
- .execute();
-
- assertProjectPropertyDoesNotExist(branch, "foo");
- }
-
- @Test
- public void empty_204_response() {
- logInAsSystemAdministrator();
- TestResponse result = ws.newRequest()
- .setParam("keys", "my.key")
- .execute();
-
- assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
- assertThat(result.getInput()).isEmpty();
- }
-
- @Test
- public void test_ws_definition() {
- WebService.Action action = ws.getDef();
- assertThat(action).isNotNull();
- assertThat(action.isInternal()).isFalse();
- assertThat(action.isPost()).isTrue();
- assertThat(action.responseExampleAsString()).isNull();
- assertThat(action.params()).extracting(Param::key).containsExactlyInAnyOrder("keys", "component", "branch", "pullRequest");
- }
-
- @Test
- public void throw_ForbiddenException_if_global_setting_and_not_system_administrator() {
- userSession.logIn().setNonSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo").build());
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- executeRequestOnGlobalSetting("foo");
- }
-
- @Test
- public void throw_ForbiddenException_if_project_setting_and_not_project_administrator() {
- userSession.logIn().addProjectPermission(USER, project);
- definitions.addComponent(PropertyDefinition.builder("foo").build());
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- executeRequestOnComponentSetting("foo", project);
- }
-
- @Test
- public void throw_ForbiddenException_if_project_setting_and_system_administrator() {
- logInAsSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo").build());
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- executeRequestOnComponentSetting("foo", project);
- }
-
- @Test
- public void fail_when_not_global_and_no_component() {
- logInAsSystemAdministrator();
- definitions.addComponent(PropertyDefinition.builder("foo")
- .onlyOnQualifiers(VIEW)
- .build());
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Setting 'foo' cannot be global");
-
- executeRequestOnGlobalSetting("foo");
- }
-
- @Test
- public void fail_when_qualifier_not_included() {
- userSession.logIn().setRoot();
- definitions.addComponent(PropertyDefinition.builder("foo")
- .onQualifiers(VIEW)
- .build());
- i18n.put("qualifier." + PROJECT, "project");
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Setting 'foo' cannot be set on a project");
-
- executeRequestOnComponentSetting("foo", project);
- }
-
- @Test
- public void fail_to_reset_setting_component_when_setting_is_global() {
- userSession.logIn().setRoot();
-
- definitions.addComponent(PropertyDefinition.builder("foo").build());
- i18n.put("qualifier." + PROJECT, "project");
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Setting 'foo' cannot be set on a project");
-
- executeRequestOnComponentSetting("foo", project);
- }
-
- @Test
- public void succeed_for_property_without_definition_when_set_on_project_component() {
- ComponentDto project = randomPublicOrPrivateProject();
- succeedForPropertyWithoutDefinitionAndValidComponent(project, project);
- }
-
- @Test
- public void succeed_for_property_without_definition_when_set_on_module_component() {
- ComponentDto project = randomPublicOrPrivateProject();
- ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
- succeedForPropertyWithoutDefinitionAndValidComponent(project, module);
- }
-
- @Test
- public void fail_for_property_without_definition_when_set_on_directory_component() {
- ComponentDto project = randomPublicOrPrivateProject();
- ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(project, "A/B"));
- failForPropertyWithoutDefinitionOnUnsupportedComponent(project, directory);
- }
-
- @Test
- public void fail_for_property_without_definition_when_set_on_file_component() {
- ComponentDto project = randomPublicOrPrivateProject();
- ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
- failForPropertyWithoutDefinitionOnUnsupportedComponent(project, file);
- }
-
- @Test
- public void succeed_for_property_without_definition_when_set_on_view_component() {
- ComponentDto view = db.components().insertPublicPortfolio();
- succeedForPropertyWithoutDefinitionAndValidComponent(view, view);
- }
-
- @Test
- public void succeed_for_property_without_definition_when_set_on_subview_component() {
- ComponentDto view = db.components().insertPublicPortfolio();
- ComponentDto subview = db.components().insertComponent(ComponentTesting.newSubView(view));
- succeedForPropertyWithoutDefinitionAndValidComponent(view, subview);
- }
-
- @Test
- public void fail_for_property_without_definition_when_set_on_projectCopy_component() {
- ComponentDto view = db.components().insertPublicPortfolio();
- ComponentDto projectCopy = db.components().insertComponent(ComponentTesting.newProjectCopy("a", db.components().insertPrivateProject(), view));
-
- failForPropertyWithoutDefinitionOnUnsupportedComponent(view, projectCopy);
- }
-
- @Test
- public void fail_when_using_branch_db_key() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- ComponentDto branch = db.components().insertProjectBranch(project);
- definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
-
- ws.newRequest()
- .setParam("keys", "foo")
- .setParam("component", branch.getDbKey())
- .execute();
- }
-
- @Test
- public void fail_when_component_not_found() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'unknown' not found");
-
- ws.newRequest()
- .setParam("keys", "foo")
- .setParam("component", "unknown")
- .execute();
- }
-
- @Test
- public void fail_when_branch_not_found() {
- ComponentDto project = db.components().insertPublicProject();
- logInAsProjectAdmin(project);
- ComponentDto branch = db.components().insertProjectBranch(project);
- String settingKey = "not_allowed_on_branch";
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Component '%s' on branch 'unknown' not found", branch.getKey()));
-
- ws.newRequest()
- .setParam("keys", settingKey)
- .setParam("component", branch.getKey())
- .setParam("branch", "unknown")
- .execute();
- }
-
- @Test
- public void fail_when_setting_key_is_defined_in_sonar_properties() {
- ComponentDto project = db.components().insertPrivateProject();
- logInAsProjectAdmin(project);
- String settingKey = ProcessProperties.Property.JDBC_URL.getKey();
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(format("Setting '%s' can only be used in sonar.properties", settingKey));
-
- ws.newRequest()
- .setParam("keys", settingKey)
- .setParam("component", project.getKey())
- .execute();
- }
-
- private void succeedForPropertyWithoutDefinitionAndValidComponent(ComponentDto root, ComponentDto module) {
- logInAsProjectAdmin(root);
-
- executeRequestOnComponentSetting("foo", module);
- }
-
- private void failForPropertyWithoutDefinitionOnUnsupportedComponent(ComponentDto root, ComponentDto component) {
- i18n.put("qualifier." + component.qualifier(), "QualifierLabel");
- logInAsProjectAdmin(root);
-
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Setting 'foo' cannot be set on a QualifierLabel");
-
- executeRequestOnComponentSetting("foo", component);
- }
-
- private void executeRequestOnGlobalSetting(String key) {
- executeRequest(key, null);
- }
-
- private void executeRequestOnProjectSetting(String key) {
- executeRequest(key, project.getDbKey());
- }
-
- private void executeRequestOnComponentSetting(String key, ComponentDto componentDto) {
- executeRequest(key, componentDto.getDbKey());
- }
-
- private void executeRequest(String key, @Nullable String componentKey) {
- TestRequest request = ws.newRequest()
- .setMediaType(MediaTypes.PROTOBUF)
- .setParam("keys", key);
- if (componentKey != null) {
- request.setParam("component", componentKey);
- }
- request.execute();
- }
-
- private void logInAsSystemAdministrator() {
- userSession.logIn().setSystemAdministrator();
- }
-
- private void logInAsProjectAdmin() {
- userSession.logIn().addProjectPermission(ADMIN, project);
- }
-
- private void logInAsProjectAdmin(ComponentDto root) {
- userSession.logIn().addProjectPermission(ADMIN, root);
- }
-
- private void assertGlobalPropertyDoesNotExist(String key) {
- assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, key)).isNull();
- }
-
- private void assertGlobalPropertyExists(String key) {
- assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, key)).isNotNull();
- }
-
- private void assertProjectPropertyDoesNotExist(ComponentDto component, String key) {
- assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentUuid(component.uuid()).setKey(key).build(), dbSession)).isEmpty();
- }
-
- private void assertProjectPropertyDoesNotExist(String key) {
- assertProjectPropertyDoesNotExist(project, key);
- }
-
- private void assertProjectPropertyExists(String key) {
- assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentUuid(project.uuid()).setKey(key).build(), dbSession)).isNotEmpty();
- }
-
- private void assertUserPropertyExists(String key, UserDto user) {
- assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
- .setKey(key)
- .setUserUuid(user.getUuid())
- .build(),
- dbSession)).isNotEmpty();
- }
-
- private ComponentDto randomPublicOrPrivateProject() {
- return new Random().nextBoolean() ? db.components().insertPrivateProject() : db.components().insertPublicProject();
- }
-
- }
|