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.

ListDefinitionsActionTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 javax.annotation.Nullable;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.PropertyType;
  26. import org.sonar.api.config.PropertyDefinition;
  27. import org.sonar.api.config.PropertyDefinitions;
  28. import org.sonar.api.config.PropertyFieldDefinition;
  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.db.DbClient;
  33. import org.sonar.db.DbTester;
  34. import org.sonar.db.component.ComponentDbTester;
  35. import org.sonar.db.component.ComponentDto;
  36. import org.sonar.db.component.ComponentTesting;
  37. import org.sonar.server.component.TestComponentFinder;
  38. import org.sonar.server.exceptions.ForbiddenException;
  39. import org.sonar.server.exceptions.NotFoundException;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.WsActionTester;
  43. import org.sonar.test.JsonAssert;
  44. import org.sonarqube.ws.Settings;
  45. import org.sonarqube.ws.Settings.Definition;
  46. import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
  47. import static java.util.Arrays.asList;
  48. import static org.assertj.core.api.Assertions.assertThat;
  49. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  50. import static org.assertj.core.groups.Tuple.tuple;
  51. import static org.sonar.api.resources.Qualifiers.PROJECT;
  52. import static org.sonar.api.web.UserRole.ADMIN;
  53. import static org.sonar.api.web.UserRole.CODEVIEWER;
  54. import static org.sonar.api.web.UserRole.USER;
  55. import static org.sonar.db.permission.GlobalPermission.ADMINISTER;
  56. import static org.sonar.db.permission.GlobalPermission.SCAN;
  57. import static org.sonarqube.ws.MediaTypes.JSON;
  58. import static org.sonarqube.ws.Settings.Definition.CategoryOneOfCase.CATEGORYONEOF_NOT_SET;
  59. import static org.sonarqube.ws.Settings.Definition.DefaultValueOneOfCase.DEFAULTVALUEONEOF_NOT_SET;
  60. import static org.sonarqube.ws.Settings.Definition.DeprecatedKeyOneOfCase.DEPRECATEDKEYONEOF_NOT_SET;
  61. import static org.sonarqube.ws.Settings.Definition.NameOneOfCase.NAMEONEOF_NOT_SET;
  62. import static org.sonarqube.ws.Settings.Definition.SubCategoryOneOfCase.SUBCATEGORYONEOF_NOT_SET;
  63. import static org.sonarqube.ws.Settings.Type.BOOLEAN;
  64. import static org.sonarqube.ws.Settings.Type.LICENSE;
  65. import static org.sonarqube.ws.Settings.Type.PROPERTY_SET;
  66. import static org.sonarqube.ws.Settings.Type.SINGLE_SELECT_LIST;
  67. import static org.sonarqube.ws.Settings.Type.STRING;
  68. import static org.sonarqube.ws.Settings.Type.TEXT;
  69. public class ListDefinitionsActionTest {
  70. @Rule
  71. public UserSessionRule userSession = UserSessionRule.standalone();
  72. @Rule
  73. public DbTester db = DbTester.create(System2.INSTANCE);
  74. private DbClient dbClient = db.getDbClient();
  75. private ComponentDbTester componentDb = new ComponentDbTester(db);
  76. private ComponentDto project;
  77. private PropertyDefinitions propertyDefinitions = new PropertyDefinitions(System2.INSTANCE);
  78. private SettingsWsSupport support = new SettingsWsSupport(userSession);
  79. private WsActionTester ws = new WsActionTester(
  80. new ListDefinitionsAction(dbClient, TestComponentFinder.from(db), userSession, propertyDefinitions, support));
  81. @Before
  82. public void setUp() {
  83. project = componentDb.insertComponent(ComponentTesting.newPrivateProjectDto());
  84. }
  85. @Test
  86. public void return_settings_definitions() {
  87. logIn();
  88. propertyDefinitions.addComponent(PropertyDefinition
  89. .builder("foo")
  90. .name("Foo")
  91. .description("desc")
  92. .category("cat")
  93. .subCategory("subCat")
  94. .type(PropertyType.TEXT)
  95. .defaultValue("default")
  96. .multiValues(true)
  97. .build());
  98. ListDefinitionsWsResponse result = executeRequest();
  99. assertThat(result.getDefinitionsList()).hasSize(1);
  100. Definition definition = result.getDefinitions(0);
  101. assertThat(definition.getKey()).isEqualTo("foo");
  102. assertThat(definition.getName()).isEqualTo("Foo");
  103. assertThat(definition.getDescription()).isEqualTo("desc");
  104. assertThat(definition.getCategory()).isEqualTo("cat");
  105. assertThat(definition.getSubCategory()).isEqualTo("subCat");
  106. assertThat(definition.getType()).isEqualTo(TEXT);
  107. assertThat(definition.getDefaultValue()).isEqualTo("default");
  108. assertThat(definition.getMultiValues()).isTrue();
  109. }
  110. @Test
  111. public void return_settings_definitions_with_minimum_fields() {
  112. logIn();
  113. propertyDefinitions.addComponent(PropertyDefinition
  114. .builder("foo")
  115. .build());
  116. ListDefinitionsWsResponse result = executeRequest();
  117. assertThat(result.getDefinitionsList()).hasSize(1);
  118. Definition definition = result.getDefinitions(0);
  119. assertThat(definition.getKey()).isEqualTo("foo");
  120. assertThat(definition.getType()).isEqualTo(STRING);
  121. assertThat(definition.getNameOneOfCase()).isEqualTo(NAMEONEOF_NOT_SET);
  122. assertThat(definition.getCategoryOneOfCase()).isEqualTo(CATEGORYONEOF_NOT_SET);
  123. assertThat(definition.getSubCategoryOneOfCase()).isEqualTo(SUBCATEGORYONEOF_NOT_SET);
  124. assertThat(definition.getDefaultValueOneOfCase()).isEqualTo(DEFAULTVALUEONEOF_NOT_SET);
  125. assertThat(definition.getMultiValues()).isFalse();
  126. assertThat(definition.getOptionsCount()).isZero();
  127. assertThat(definition.getFieldsCount()).isZero();
  128. assertThat(definition.getDeprecatedKeyOneOfCase()).isEqualTo(DEPRECATEDKEYONEOF_NOT_SET);
  129. }
  130. @Test
  131. public void return_settings_definitions_with_deprecated_key() {
  132. logIn();
  133. propertyDefinitions.addComponent(PropertyDefinition
  134. .builder("foo")
  135. .name("Foo")
  136. .deprecatedKey("deprecated")
  137. .build());
  138. ListDefinitionsWsResponse result = executeRequest();
  139. assertThat(result.getDefinitionsList()).hasSize(1);
  140. Definition definition = result.getDefinitions(0);
  141. assertThat(definition.getKey()).isEqualTo("foo");
  142. assertThat(definition.getName()).isEqualTo("Foo");
  143. assertThat(definition.getDeprecatedKey()).isEqualTo("deprecated");
  144. }
  145. @Test
  146. public void return_default_category() {
  147. logIn();
  148. propertyDefinitions.addComponent(PropertyDefinition.builder("foo").build(), "default");
  149. propertyDefinitions.addComponent(PropertyDefinition.builder("foo").category("").build(), "default");
  150. ListDefinitionsWsResponse result = executeRequest();
  151. assertThat(result.getDefinitionsList()).hasSize(1);
  152. assertThat(result.getDefinitions(0).getCategory()).isEqualTo("default");
  153. assertThat(result.getDefinitions(0).getSubCategory()).isEqualTo("default");
  154. }
  155. @Test
  156. public void return_single_select_list_property() {
  157. logIn();
  158. propertyDefinitions.addComponent(PropertyDefinition
  159. .builder("foo")
  160. .type(PropertyType.SINGLE_SELECT_LIST)
  161. .options("one", "two")
  162. .build());
  163. ListDefinitionsWsResponse result = executeRequest();
  164. assertThat(result.getDefinitionsList()).hasSize(1);
  165. Definition definition = result.getDefinitions(0);
  166. assertThat(definition.getType()).isEqualTo(SINGLE_SELECT_LIST);
  167. assertThat(definition.getOptionsList()).containsExactly("one", "two");
  168. }
  169. @Test
  170. public void return_JSON_property() {
  171. logIn();
  172. propertyDefinitions.addComponent(PropertyDefinition
  173. .builder("foo")
  174. .type(PropertyType.JSON)
  175. .build());
  176. ListDefinitionsWsResponse result = executeRequest();
  177. assertThat(result.getDefinitionsList()).hasSize(1);
  178. Definition definition = result.getDefinitions(0);
  179. assertThat(definition.getType()).isEqualTo(Settings.Type.JSON);
  180. }
  181. @Test
  182. public void return_property_set() {
  183. logIn();
  184. propertyDefinitions.addComponent(PropertyDefinition
  185. .builder("foo")
  186. .type(PropertyType.PROPERTY_SET)
  187. .fields(
  188. PropertyFieldDefinition.build("boolean").name("Boolean").description("boolean desc").type(PropertyType.BOOLEAN).build(),
  189. PropertyFieldDefinition.build("list").name("List").description("list desc").type(PropertyType.SINGLE_SELECT_LIST).options("one", "two").build())
  190. .build());
  191. ListDefinitionsWsResponse result = executeRequest();
  192. assertThat(result.getDefinitionsList()).hasSize(1);
  193. Definition definition = result.getDefinitions(0);
  194. assertThat(definition.getType()).isEqualTo(PROPERTY_SET);
  195. assertThat(definition.getFieldsList()).hasSize(2);
  196. assertThat(definition.getFields(0).getKey()).isEqualTo("boolean");
  197. assertThat(definition.getFields(0).getName()).isEqualTo("Boolean");
  198. assertThat(definition.getFields(0).getDescription()).isEqualTo("boolean desc");
  199. assertThat(definition.getFields(0).getType()).isEqualTo(BOOLEAN);
  200. assertThat(definition.getFields(0).getOptionsCount()).isZero();
  201. assertThat(definition.getFields(1).getKey()).isEqualTo("list");
  202. assertThat(definition.getFields(1).getName()).isEqualTo("List");
  203. assertThat(definition.getFields(1).getDescription()).isEqualTo("list desc");
  204. assertThat(definition.getFields(1).getType()).isEqualTo(SINGLE_SELECT_LIST);
  205. assertThat(definition.getFields(1).getOptionsList()).containsExactly("one", "two");
  206. }
  207. @Test
  208. public void return_license_type_in_property_set() {
  209. logIn();
  210. propertyDefinitions.addComponent(PropertyDefinition
  211. .builder("foo")
  212. .type(PropertyType.PROPERTY_SET)
  213. .fields(PropertyFieldDefinition.build("license").name("License").type(PropertyType.LICENSE).build())
  214. .build());
  215. ListDefinitionsWsResponse result = executeRequest();
  216. assertThat(result.getDefinitionsList()).hasSize(1);
  217. assertThat(result.getDefinitions(0).getFieldsList()).extracting(Settings.Field::getKey, Settings.Field::getType).containsOnly(tuple("license", LICENSE));
  218. }
  219. @Test
  220. public void return_global_settings_definitions() {
  221. logIn();
  222. propertyDefinitions.addComponent(PropertyDefinition.builder("foo").build());
  223. ListDefinitionsWsResponse result = executeRequest();
  224. assertThat(result.getDefinitionsList()).hasSize(1);
  225. }
  226. @Test
  227. public void definitions_are_ordered_by_category_then_index_then_name_case_insensitive() {
  228. logIn();
  229. propertyDefinitions.addComponent(PropertyDefinition.builder("sonar.prop.11").category("cat-1").index(1).name("prop 1").build());
  230. propertyDefinitions.addComponent(PropertyDefinition.builder("sonar.prop.12").category("cat-1").index(2).name("prop 2").build());
  231. propertyDefinitions.addComponent(PropertyDefinition.builder("sonar.prop.13").category("CAT-1").index(1).name("prop 3").build());
  232. propertyDefinitions.addComponent(PropertyDefinition.builder("sonar.prop.41").category("cat-0").index(25).name("prop 1").build());
  233. ListDefinitionsWsResponse result = executeRequest();
  234. assertThat(result.getDefinitionsList()).extracting(Definition::getKey)
  235. .containsExactly("sonar.prop.41", "sonar.prop.11", "sonar.prop.13", "sonar.prop.12");
  236. }
  237. @Test
  238. public void return_project_settings_def_by_project_key() {
  239. logInAsProjectUser();
  240. propertyDefinitions.addComponent(PropertyDefinition
  241. .builder("foo")
  242. .onQualifiers(PROJECT)
  243. .build());
  244. ListDefinitionsWsResponse result = executeRequest(project.getKey());
  245. assertThat(result.getDefinitionsList()).hasSize(1);
  246. }
  247. @Test
  248. public void return_only_global_properties_when_no_component_parameter() {
  249. logInAsProjectUser();
  250. propertyDefinitions.addComponents(asList(
  251. PropertyDefinition.builder("global").build(),
  252. PropertyDefinition.builder("global-and-project").onQualifiers(PROJECT).build(),
  253. PropertyDefinition.builder("only-on-project").onlyOnQualifiers(PROJECT).build()));
  254. ListDefinitionsWsResponse result = executeRequest();
  255. assertThat(result.getDefinitionsList()).extracting("key").containsOnly("global", "global-and-project");
  256. }
  257. @Test
  258. public void return_only_properties_available_for_component_qualifier() {
  259. logInAsProjectUser();
  260. propertyDefinitions.addComponents(asList(
  261. PropertyDefinition.builder("global").build(),
  262. PropertyDefinition.builder("global-and-project").onQualifiers(PROJECT).build(),
  263. PropertyDefinition.builder("only-on-project").onlyOnQualifiers(PROJECT).build()));
  264. ListDefinitionsWsResponse result = executeRequest(project.getKey());
  265. assertThat(result.getDefinitionsList()).extracting("key").containsOnly("global-and-project", "only-on-project");
  266. }
  267. @Test
  268. public void does_not_return_hidden_properties() {
  269. logInAsAdmin();
  270. propertyDefinitions.addComponent(PropertyDefinition.builder("foo").hidden().build());
  271. ListDefinitionsWsResponse result = executeRequest();
  272. assertThat(result.getDefinitionsList()).isEmpty();
  273. }
  274. @Test
  275. public void return_license_type() {
  276. logInAsAdmin();
  277. propertyDefinitions.addComponents(asList(
  278. PropertyDefinition.builder("plugin.license.secured").type(PropertyType.LICENSE).build(),
  279. PropertyDefinition.builder("commercial.plugin").type(PropertyType.LICENSE).build()));
  280. ListDefinitionsWsResponse result = executeRequest();
  281. assertThat(result.getDefinitionsList()).extracting(Definition::getKey, Definition::getType)
  282. .containsOnly(tuple("plugin.license.secured", LICENSE), tuple("commercial.plugin", LICENSE));
  283. }
  284. @Test
  285. public void does_not_returned_secured_and_license_settings_when_not_authenticated() {
  286. propertyDefinitions.addComponents(asList(
  287. PropertyDefinition.builder("foo").build(),
  288. PropertyDefinition.builder("secret.secured").build()));
  289. ListDefinitionsWsResponse result = executeRequest();
  290. assertThat(result.getDefinitionsList()).extracting(Definition::getKey).containsOnly("foo");
  291. }
  292. @Test
  293. public void return_secured_settings_when_not_authenticated_but_with_scan_permission() {
  294. userSession.anonymous().addPermission(SCAN);
  295. propertyDefinitions.addComponents(asList(
  296. PropertyDefinition.builder("foo").build(),
  297. PropertyDefinition.builder("secret.secured").build()));
  298. ListDefinitionsWsResponse result = executeRequest();
  299. assertThat(result.getDefinitionsList()).extracting(Definition::getKey).containsOnly("foo", "secret.secured");
  300. }
  301. @Test
  302. public void return_secured_settings_when_system_admin() {
  303. logInAsAdmin();
  304. propertyDefinitions.addComponents(asList(
  305. PropertyDefinition.builder("foo").build(),
  306. PropertyDefinition.builder("secret.secured").build()));
  307. ListDefinitionsWsResponse result = executeRequest();
  308. assertThat(result.getDefinitionsList()).extracting(Definition::getKey).containsOnly("foo", "secret.secured");
  309. }
  310. @Test
  311. public void return_secured_settings_when_project_admin() {
  312. logInAsProjectAdmin();
  313. propertyDefinitions.addComponents(asList(
  314. PropertyDefinition.builder("foo").onQualifiers(PROJECT).build(),
  315. PropertyDefinition.builder("secret.secured").onQualifiers(PROJECT).build()));
  316. ListDefinitionsWsResponse result = executeRequest(project.getKey());
  317. assertThat(result.getDefinitionsList()).extracting(Definition::getKey).containsOnly("foo", "secret.secured");
  318. }
  319. @Test
  320. public void fail_when_user_has_not_project_browse_permission() {
  321. userSession.logIn("project-admin").addProjectPermission(CODEVIEWER, project);
  322. propertyDefinitions.addComponent(PropertyDefinition.builder("foo").build());
  323. assertThatThrownBy(() -> executeRequest(project.getKey()))
  324. .isInstanceOf(ForbiddenException.class);
  325. }
  326. @Test
  327. public void fail_when_component_not_found() {
  328. assertThatThrownBy(() -> {
  329. ws.newRequest()
  330. .setParam("component", "unknown")
  331. .execute();
  332. })
  333. .isInstanceOf(NotFoundException.class)
  334. .hasMessage("Component key 'unknown' not found");
  335. }
  336. @Test
  337. public void test_ws_definition() {
  338. WebService.Action action = ws.getDef();
  339. assertThat(action).isNotNull();
  340. assertThat(action.isInternal()).isFalse();
  341. assertThat(action.isPost()).isFalse();
  342. assertThat(action.responseExampleAsString()).isNotEmpty();
  343. assertThat(action.params()).extracting(Param::key).containsExactlyInAnyOrder("component");
  344. }
  345. @Test
  346. public void test_example_json_response() {
  347. logInAsProjectAdmin();
  348. propertyDefinitions.addComponents(asList(
  349. PropertyDefinition.builder("sonar.string")
  350. .name("String")
  351. .description("String property")
  352. .type(PropertyType.STRING)
  353. .category("general")
  354. .subCategory("test")
  355. .defaultValue("123")
  356. .build(),
  357. PropertyDefinition.builder("sonar.list")
  358. .name("List")
  359. .description("List property")
  360. .type(PropertyType.SINGLE_SELECT_LIST)
  361. .category("general")
  362. .options("a", "b")
  363. .build(),
  364. PropertyDefinition.builder("sonar.multiValues")
  365. .name("Multi values")
  366. .description("Multi values property")
  367. .type(PropertyType.STRING)
  368. .category("general")
  369. .multiValues(true)
  370. .build(),
  371. PropertyDefinition.builder("sonar.propertySet")
  372. .name("Property Set")
  373. .description("Property Set property")
  374. .type(PropertyType.PROPERTY_SET)
  375. .category("property")
  376. .subCategory("set")
  377. .fields(
  378. PropertyFieldDefinition.build("text")
  379. .name("Text")
  380. .description("Text field description")
  381. .type(PropertyType.TEXT)
  382. .build(),
  383. PropertyFieldDefinition.build("list")
  384. .name("List")
  385. .description("List field description")
  386. .type(PropertyType.SINGLE_SELECT_LIST)
  387. .options("value1", "value2")
  388. .build())
  389. .build()));
  390. String result = ws.newRequest().setMediaType(JSON).execute().getInput();
  391. JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
  392. }
  393. private ListDefinitionsWsResponse executeRequest() {
  394. return executeRequest(null);
  395. }
  396. private ListDefinitionsWsResponse executeRequest(@Nullable String key) {
  397. TestRequest request = ws.newRequest();
  398. if (key != null) {
  399. request.setParam("component", key);
  400. }
  401. return request.executeProtobuf(ListDefinitionsWsResponse.class);
  402. }
  403. private void logIn() {
  404. userSession.logIn();
  405. }
  406. private void logInAsProjectUser() {
  407. userSession.logIn().addProjectPermission(USER, project);
  408. }
  409. private void logInAsAdmin() {
  410. userSession.logIn().addPermission(ADMINISTER);
  411. }
  412. private void logInAsProjectAdmin() {
  413. userSession.logIn()
  414. .addProjectPermission(ADMIN, project)
  415. .addProjectPermission(USER, project);
  416. }
  417. }