You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResetActionTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.setting.ws;
  21. import java.util.Random;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.config.PropertyDefinition;
  28. import org.sonar.api.config.PropertyDefinitions;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.server.ws.WebService.Param;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.api.web.UserRole;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.component.ComponentDbTester;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.component.ComponentTesting;
  39. import org.sonar.db.property.PropertyDbTester;
  40. import org.sonar.db.property.PropertyQuery;
  41. import org.sonar.db.user.UserDto;
  42. import org.sonar.db.user.UserTesting;
  43. import org.sonar.process.ProcessProperties;
  44. import org.sonar.server.component.ComponentFinder;
  45. import org.sonar.server.component.TestComponentFinder;
  46. import org.sonar.server.exceptions.BadRequestException;
  47. import org.sonar.server.exceptions.ForbiddenException;
  48. import org.sonar.server.exceptions.NotFoundException;
  49. import org.sonar.server.l18n.I18nRule;
  50. import org.sonar.server.tester.UserSessionRule;
  51. import org.sonar.server.ws.TestRequest;
  52. import org.sonar.server.ws.TestResponse;
  53. import org.sonar.server.ws.WsActionTester;
  54. import org.sonarqube.ws.MediaTypes;
  55. import static java.lang.String.format;
  56. import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
  57. import static org.assertj.core.api.Assertions.assertThat;
  58. import static org.sonar.api.resources.Qualifiers.PROJECT;
  59. import static org.sonar.api.resources.Qualifiers.VIEW;
  60. import static org.sonar.api.web.UserRole.ADMIN;
  61. import static org.sonar.api.web.UserRole.USER;
  62. import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
  63. import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
  64. import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
  65. public class ResetActionTest {
  66. @Rule
  67. public ExpectedException expectedException = ExpectedException.none();
  68. @Rule
  69. public UserSessionRule userSession = UserSessionRule.standalone();
  70. @Rule
  71. public DbTester db = DbTester.create(System2.INSTANCE);
  72. private I18nRule i18n = new I18nRule();
  73. private PropertyDbTester propertyDb = new PropertyDbTester(db);
  74. private ComponentDbTester componentDb = new ComponentDbTester(db);
  75. private DbClient dbClient = db.getDbClient();
  76. private DbSession dbSession = db.getSession();
  77. private ComponentFinder componentFinder = TestComponentFinder.from(db);
  78. private PropertyDefinitions definitions = new PropertyDefinitions(System2.INSTANCE);
  79. private SettingsUpdater settingsUpdater = new SettingsUpdater(dbClient, definitions);
  80. private SettingValidations settingValidations = new SettingValidations(definitions, dbClient, i18n);
  81. private ComponentDto project;
  82. private ResetAction underTest = new ResetAction(dbClient, componentFinder, settingsUpdater, userSession, definitions, settingValidations);
  83. private WsActionTester ws = new WsActionTester(underTest);
  84. @Before
  85. public void setUp() {
  86. project = componentDb.insertComponent(ComponentTesting.newPrivateProjectDto());
  87. }
  88. @Test
  89. public void remove_global_setting() {
  90. logInAsSystemAdministrator();
  91. definitions.addComponent(PropertyDefinition.builder("foo").build());
  92. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  93. executeRequestOnGlobalSetting("foo");
  94. assertGlobalPropertyDoesNotExist("foo");
  95. }
  96. @Test
  97. public void remove_global_setting_even_if_not_defined() {
  98. logInAsSystemAdministrator();
  99. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  100. executeRequestOnGlobalSetting("foo");
  101. assertGlobalPropertyDoesNotExist("foo");
  102. }
  103. @Test
  104. public void remove_component_setting() {
  105. logInAsProjectAdmin();
  106. definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
  107. propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
  108. executeRequestOnProjectSetting("foo");
  109. assertProjectPropertyDoesNotExist("foo");
  110. }
  111. @Test
  112. public void remove_component_setting_even_if_not_defined() {
  113. logInAsProjectAdmin();
  114. propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
  115. executeRequestOnProjectSetting("foo");
  116. assertProjectPropertyDoesNotExist("foo");
  117. }
  118. @Test
  119. public void remove_hidden_setting() {
  120. logInAsSystemAdministrator();
  121. definitions.addComponent(PropertyDefinition.builder("foo").hidden().build());
  122. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  123. executeRequestOnGlobalSetting("foo");
  124. assertGlobalPropertyDoesNotExist("foo");
  125. }
  126. @Test
  127. public void ignore_project_setting_when_removing_global_setting() {
  128. logInAsSystemAdministrator();
  129. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  130. propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
  131. executeRequestOnGlobalSetting("foo");
  132. assertGlobalPropertyDoesNotExist("foo");
  133. assertProjectPropertyExists("foo");
  134. }
  135. @Test
  136. public void ignore_global_setting_when_removing_project_setting() {
  137. logInAsProjectAdmin();
  138. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  139. propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value"));
  140. executeRequestOnProjectSetting("foo");
  141. assertGlobalPropertyExists("foo");
  142. assertProjectPropertyDoesNotExist("foo");
  143. }
  144. @Test
  145. public void ignore_user_setting_when_removing_global_setting() {
  146. logInAsSystemAdministrator();
  147. UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto());
  148. propertyDb.insertProperties(newUserPropertyDto("foo", "one", user));
  149. executeRequestOnGlobalSetting("foo");
  150. assertUserPropertyExists("foo", user);
  151. }
  152. @Test
  153. public void ignore_user_setting_when_removing_project_setting() {
  154. logInAsProjectAdmin();
  155. UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto());
  156. propertyDb.insertProperties(newUserPropertyDto("foo", "one", user));
  157. executeRequestOnProjectSetting("foo");
  158. assertUserPropertyExists("foo", user);
  159. }
  160. @Test
  161. public void ignore_unknown_setting_key() {
  162. logInAsSystemAdministrator();
  163. executeRequestOnGlobalSetting("unknown");
  164. }
  165. @Test
  166. public void remove_setting_by_deprecated_key() {
  167. logInAsSystemAdministrator();
  168. definitions.addComponent(PropertyDefinition.builder("foo").deprecatedKey("old").build());
  169. propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
  170. executeRequestOnGlobalSetting("old");
  171. assertGlobalPropertyDoesNotExist("foo");
  172. }
  173. @Test
  174. public void remove_setting_on_branch() {
  175. ComponentDto project = db.components().insertPublicProject();
  176. ComponentDto branch = db.components().insertProjectBranch(project);
  177. definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
  178. propertyDb.insertProperties(newComponentPropertyDto(branch).setKey("foo").setValue("value"));
  179. userSession.logIn().addProjectPermission(ADMIN, project);
  180. ws.newRequest()
  181. .setMediaType(MediaTypes.PROTOBUF)
  182. .setParam("keys", "foo")
  183. .setParam("component", branch.getKey())
  184. .setParam("branch", branch.getBranch())
  185. .execute();
  186. assertProjectPropertyDoesNotExist(branch, "foo");
  187. }
  188. @Test
  189. public void empty_204_response() {
  190. logInAsSystemAdministrator();
  191. TestResponse result = ws.newRequest()
  192. .setParam("keys", "my.key")
  193. .execute();
  194. assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT);
  195. assertThat(result.getInput()).isEmpty();
  196. }
  197. @Test
  198. public void test_ws_definition() {
  199. WebService.Action action = ws.getDef();
  200. assertThat(action).isNotNull();
  201. assertThat(action.isInternal()).isFalse();
  202. assertThat(action.isPost()).isTrue();
  203. assertThat(action.responseExampleAsString()).isNull();
  204. assertThat(action.params()).extracting(Param::key).containsExactlyInAnyOrder("keys", "component", "branch", "pullRequest");
  205. }
  206. @Test
  207. public void throw_ForbiddenException_if_global_setting_and_not_system_administrator() {
  208. userSession.logIn().setNonSystemAdministrator();
  209. definitions.addComponent(PropertyDefinition.builder("foo").build());
  210. expectedException.expect(ForbiddenException.class);
  211. expectedException.expectMessage("Insufficient privileges");
  212. executeRequestOnGlobalSetting("foo");
  213. }
  214. @Test
  215. public void throw_ForbiddenException_if_project_setting_and_not_project_administrator() {
  216. userSession.logIn().addProjectPermission(USER, project);
  217. definitions.addComponent(PropertyDefinition.builder("foo").build());
  218. expectedException.expect(ForbiddenException.class);
  219. expectedException.expectMessage("Insufficient privileges");
  220. executeRequestOnComponentSetting("foo", project);
  221. }
  222. @Test
  223. public void throw_ForbiddenException_if_project_setting_and_system_administrator() {
  224. logInAsSystemAdministrator();
  225. definitions.addComponent(PropertyDefinition.builder("foo").build());
  226. expectedException.expect(ForbiddenException.class);
  227. expectedException.expectMessage("Insufficient privileges");
  228. executeRequestOnComponentSetting("foo", project);
  229. }
  230. @Test
  231. public void fail_when_not_global_and_no_component() {
  232. logInAsSystemAdministrator();
  233. definitions.addComponent(PropertyDefinition.builder("foo")
  234. .onlyOnQualifiers(VIEW)
  235. .build());
  236. expectedException.expect(BadRequestException.class);
  237. expectedException.expectMessage("Setting 'foo' cannot be global");
  238. executeRequestOnGlobalSetting("foo");
  239. }
  240. @Test
  241. public void fail_when_qualifier_not_included() {
  242. userSession.logIn().setRoot();
  243. definitions.addComponent(PropertyDefinition.builder("foo")
  244. .onQualifiers(VIEW)
  245. .build());
  246. i18n.put("qualifier." + PROJECT, "project");
  247. expectedException.expect(BadRequestException.class);
  248. expectedException.expectMessage("Setting 'foo' cannot be set on a project");
  249. executeRequestOnComponentSetting("foo", project);
  250. }
  251. @Test
  252. public void fail_to_reset_setting_component_when_setting_is_global() {
  253. userSession.logIn().setRoot();
  254. definitions.addComponent(PropertyDefinition.builder("foo").build());
  255. i18n.put("qualifier." + PROJECT, "project");
  256. expectedException.expect(BadRequestException.class);
  257. expectedException.expectMessage("Setting 'foo' cannot be set on a project");
  258. executeRequestOnComponentSetting("foo", project);
  259. }
  260. @Test
  261. public void succeed_for_property_without_definition_when_set_on_project_component() {
  262. ComponentDto project = randomPublicOrPrivateProject();
  263. succeedForPropertyWithoutDefinitionAndValidComponent(project, project);
  264. }
  265. @Test
  266. public void succeed_for_property_without_definition_when_set_on_module_component() {
  267. ComponentDto project = randomPublicOrPrivateProject();
  268. ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  269. succeedForPropertyWithoutDefinitionAndValidComponent(project, module);
  270. }
  271. @Test
  272. public void fail_for_property_without_definition_when_set_on_directory_component() {
  273. ComponentDto project = randomPublicOrPrivateProject();
  274. ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(project, "A/B"));
  275. failForPropertyWithoutDefinitionOnUnsupportedComponent(project, directory);
  276. }
  277. @Test
  278. public void fail_for_property_without_definition_when_set_on_file_component() {
  279. ComponentDto project = randomPublicOrPrivateProject();
  280. ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
  281. failForPropertyWithoutDefinitionOnUnsupportedComponent(project, file);
  282. }
  283. @Test
  284. public void succeed_for_property_without_definition_when_set_on_view_component() {
  285. ComponentDto view = db.components().insertPublicPortfolio();
  286. succeedForPropertyWithoutDefinitionAndValidComponent(view, view);
  287. }
  288. @Test
  289. public void succeed_for_property_without_definition_when_set_on_subview_component() {
  290. ComponentDto view = db.components().insertPublicPortfolio();
  291. ComponentDto subview = db.components().insertComponent(ComponentTesting.newSubView(view));
  292. succeedForPropertyWithoutDefinitionAndValidComponent(view, subview);
  293. }
  294. @Test
  295. public void fail_for_property_without_definition_when_set_on_projectCopy_component() {
  296. ComponentDto view = db.components().insertPublicPortfolio();
  297. ComponentDto projectCopy = db.components().insertComponent(ComponentTesting.newProjectCopy("a", db.components().insertPrivateProject(), view));
  298. failForPropertyWithoutDefinitionOnUnsupportedComponent(view, projectCopy);
  299. }
  300. @Test
  301. public void fail_when_using_branch_db_key() {
  302. ComponentDto project = db.components().insertPublicProject();
  303. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  304. ComponentDto branch = db.components().insertProjectBranch(project);
  305. definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
  306. expectedException.expect(NotFoundException.class);
  307. expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
  308. ws.newRequest()
  309. .setParam("keys", "foo")
  310. .setParam("component", branch.getDbKey())
  311. .execute();
  312. }
  313. @Test
  314. public void fail_when_component_not_found() {
  315. expectedException.expect(NotFoundException.class);
  316. expectedException.expectMessage("Component key 'unknown' not found");
  317. ws.newRequest()
  318. .setParam("keys", "foo")
  319. .setParam("component", "unknown")
  320. .execute();
  321. }
  322. @Test
  323. public void fail_when_branch_not_found() {
  324. ComponentDto project = db.components().insertPublicProject();
  325. logInAsProjectAdmin(project);
  326. ComponentDto branch = db.components().insertProjectBranch(project);
  327. String settingKey = "not_allowed_on_branch";
  328. expectedException.expect(NotFoundException.class);
  329. expectedException.expectMessage(format("Component '%s' on branch 'unknown' not found", branch.getKey()));
  330. ws.newRequest()
  331. .setParam("keys", settingKey)
  332. .setParam("component", branch.getKey())
  333. .setParam("branch", "unknown")
  334. .execute();
  335. }
  336. @Test
  337. public void fail_when_setting_key_is_defined_in_sonar_properties() {
  338. ComponentDto project = db.components().insertPrivateProject();
  339. logInAsProjectAdmin(project);
  340. String settingKey = ProcessProperties.Property.JDBC_URL.getKey();
  341. expectedException.expect(IllegalArgumentException.class);
  342. expectedException.expectMessage(format("Setting '%s' can only be used in sonar.properties", settingKey));
  343. ws.newRequest()
  344. .setParam("keys", settingKey)
  345. .setParam("component", project.getKey())
  346. .execute();
  347. }
  348. private void succeedForPropertyWithoutDefinitionAndValidComponent(ComponentDto root, ComponentDto module) {
  349. logInAsProjectAdmin(root);
  350. executeRequestOnComponentSetting("foo", module);
  351. }
  352. private void failForPropertyWithoutDefinitionOnUnsupportedComponent(ComponentDto root, ComponentDto component) {
  353. i18n.put("qualifier." + component.qualifier(), "QualifierLabel");
  354. logInAsProjectAdmin(root);
  355. expectedException.expect(BadRequestException.class);
  356. expectedException.expectMessage("Setting 'foo' cannot be set on a QualifierLabel");
  357. executeRequestOnComponentSetting("foo", component);
  358. }
  359. private void executeRequestOnGlobalSetting(String key) {
  360. executeRequest(key, null);
  361. }
  362. private void executeRequestOnProjectSetting(String key) {
  363. executeRequest(key, project.getDbKey());
  364. }
  365. private void executeRequestOnComponentSetting(String key, ComponentDto componentDto) {
  366. executeRequest(key, componentDto.getDbKey());
  367. }
  368. private void executeRequest(String key, @Nullable String componentKey) {
  369. TestRequest request = ws.newRequest()
  370. .setMediaType(MediaTypes.PROTOBUF)
  371. .setParam("keys", key);
  372. if (componentKey != null) {
  373. request.setParam("component", componentKey);
  374. }
  375. request.execute();
  376. }
  377. private void logInAsSystemAdministrator() {
  378. userSession.logIn().setSystemAdministrator();
  379. }
  380. private void logInAsProjectAdmin() {
  381. userSession.logIn().addProjectPermission(ADMIN, project);
  382. }
  383. private void logInAsProjectAdmin(ComponentDto root) {
  384. userSession.logIn().addProjectPermission(ADMIN, root);
  385. }
  386. private void assertGlobalPropertyDoesNotExist(String key) {
  387. assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, key)).isNull();
  388. }
  389. private void assertGlobalPropertyExists(String key) {
  390. assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, key)).isNotNull();
  391. }
  392. private void assertProjectPropertyDoesNotExist(ComponentDto component, String key) {
  393. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentUuid(component.uuid()).setKey(key).build(), dbSession)).isEmpty();
  394. }
  395. private void assertProjectPropertyDoesNotExist(String key) {
  396. assertProjectPropertyDoesNotExist(project, key);
  397. }
  398. private void assertProjectPropertyExists(String key) {
  399. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentUuid(project.uuid()).setKey(key).build(), dbSession)).isNotEmpty();
  400. }
  401. private void assertUserPropertyExists(String key, UserDto user) {
  402. assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
  403. .setKey(key)
  404. .setUserUuid(user.getUuid())
  405. .build(),
  406. dbSession)).isNotEmpty();
  407. }
  408. private ComponentDto randomPublicOrPrivateProject() {
  409. return new Random().nextBoolean() ? db.components().insertPrivateProject() : db.components().insertPublicProject();
  410. }
  411. }