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

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