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.

RenameActionTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.qualityprofile.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.server.ws.WebService;
  27. import org.sonar.api.server.ws.WebService.Param;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbTester;
  31. import org.sonar.db.organization.OrganizationDto;
  32. import org.sonar.db.qualityprofile.QProfileDto;
  33. import org.sonar.db.qualityprofile.QualityProfileTesting;
  34. import org.sonar.db.user.UserDto;
  35. import org.sonar.server.exceptions.BadRequestException;
  36. import org.sonar.server.exceptions.ForbiddenException;
  37. import org.sonar.server.exceptions.NotFoundException;
  38. import org.sonar.server.exceptions.UnauthorizedException;
  39. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.WsActionTester;
  43. import static java.util.Optional.ofNullable;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
  46. public class RenameActionTest {
  47. @Rule
  48. public DbTester db = DbTester.create(System2.INSTANCE);
  49. @Rule
  50. public UserSessionRule userSession = UserSessionRule.standalone();
  51. @Rule
  52. public ExpectedException expectedException = ExpectedException.none();
  53. private DbClient dbClient = db.getDbClient();
  54. private WsActionTester ws;
  55. private String xoo1Key = "xoo1";
  56. private String xoo2Key = "xoo2";
  57. private OrganizationDto organization;
  58. @Before
  59. public void setUp() {
  60. TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  61. QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, defaultOrganizationProvider);
  62. RenameAction underTest = new RenameAction(dbClient, userSession, wsSupport);
  63. ws = new WsActionTester(underTest);
  64. organization = db.organizations().insert();
  65. createProfiles();
  66. }
  67. @Test
  68. public void rename() {
  69. logInAsQProfileAdministrator();
  70. String qualityProfileKey = createNewValidQualityProfileKey();
  71. call(qualityProfileKey, "the new name");
  72. QProfileDto reloaded = db.getDbClient().qualityProfileDao().selectByUuid(db.getSession(), qualityProfileKey);
  73. assertThat(reloaded.getName()).isEqualTo("the new name");
  74. }
  75. @Test
  76. public void fail_renaming_if_name_already_exists() {
  77. logInAsQProfileAdministrator();
  78. QProfileDto qualityProfile1 = QualityProfileTesting.newQualityProfileDto()
  79. .setOrganizationUuid(organization.getUuid())
  80. .setLanguage("xoo")
  81. .setName("Old, valid name");
  82. db.qualityProfiles().insert(qualityProfile1);
  83. String qualityProfileKey1 = qualityProfile1.getKee();
  84. QProfileDto qualityProfile2 = QualityProfileTesting.newQualityProfileDto()
  85. .setOrganizationUuid(organization.getUuid())
  86. .setLanguage("xoo")
  87. .setName("Invalid, duplicated name");
  88. db.qualityProfiles().insert(qualityProfile2);
  89. String qualityProfileKey2 = qualityProfile2.getKee();
  90. expectedException.expect(BadRequestException.class);
  91. expectedException.expectMessage("Quality profile already exists: Invalid, duplicated name");
  92. call(qualityProfileKey1, "Invalid, duplicated name");
  93. }
  94. @Test
  95. public void allow_same_name_in_different_organizations() {
  96. OrganizationDto organizationX = db.organizations().insert();
  97. OrganizationDto organizationY = db.organizations().insert();
  98. userSession.logIn()
  99. .addPermission(ADMINISTER_QUALITY_PROFILES, organizationX);
  100. QProfileDto qualityProfile1 = QualityProfileTesting.newQualityProfileDto()
  101. .setOrganizationUuid(organizationX.getUuid())
  102. .setLanguage("xoo")
  103. .setName("Old, unique name");
  104. db.qualityProfiles().insert(qualityProfile1);
  105. String qualityProfileKey1 = qualityProfile1.getKee();
  106. QProfileDto qualityProfile2 = QualityProfileTesting.newQualityProfileDto()
  107. .setOrganizationUuid(organizationY.getUuid())
  108. .setLanguage("xoo")
  109. .setName("Duplicated name");
  110. db.qualityProfiles().insert(qualityProfile2);
  111. String qualityProfileKey2 = qualityProfile2.getKee();
  112. call(qualityProfileKey1, "Duplicated name");
  113. QProfileDto reloaded = db.getDbClient().qualityProfileDao().selectByUuid(db.getSession(), qualityProfileKey1);
  114. assertThat(reloaded.getName()).isEqualTo("Duplicated name");
  115. }
  116. @Test
  117. public void as_qprofile_editor() {
  118. QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
  119. UserDto user = db.users().insertUser();
  120. db.qualityProfiles().addUserPermission(qualityProfile, user);
  121. userSession.logIn(user);
  122. call(qualityProfile.getKee(), "the new name");
  123. QProfileDto reloaded = db.getDbClient().qualityProfileDao().selectByUuid(db.getSession(), qualityProfile.getKee());
  124. assertThat(reloaded.getName()).isEqualTo("the new name");
  125. }
  126. @Test
  127. public void fail_if_parameter_profile_is_missing() {
  128. logInAsQProfileAdministrator();
  129. expectedException.expect(IllegalArgumentException.class);
  130. expectedException.expectMessage("The 'key' parameter is missing");
  131. call(null, "Other Sonar Way");
  132. }
  133. @Test
  134. public void fail_if_parameter_name_is_missing() {
  135. logInAsQProfileAdministrator();
  136. expectedException.expect(IllegalArgumentException.class);
  137. expectedException.expectMessage("The 'name' parameter is missing");
  138. call("sonar-way-xoo1-13245", null);
  139. }
  140. @Test
  141. public void fail_if_not_profile_administrator() {
  142. OrganizationDto organizationX = db.organizations().insert();
  143. OrganizationDto organizationY = db.organizations().insert();
  144. userSession.logIn(db.users().insertUser())
  145. .addPermission(ADMINISTER_QUALITY_PROFILES, organizationX);
  146. QProfileDto qualityProfile = QualityProfileTesting.newQualityProfileDto()
  147. .setOrganizationUuid(organizationY.getUuid());
  148. db.qualityProfiles().insert(qualityProfile);
  149. String qualityProfileKey = qualityProfile.getKee();
  150. expectedException.expect(ForbiddenException.class);
  151. expectedException.expectMessage("Insufficient privileges");
  152. call(qualityProfileKey, "Hey look I am not quality profile admin!");
  153. }
  154. @Test
  155. public void fail_if_not_logged_in() {
  156. expectedException.expect(UnauthorizedException.class);
  157. expectedException.expectMessage("Authentication is required");
  158. call("sonar-way-xoo1-13245", "Not logged in");
  159. }
  160. @Test
  161. public void fail_if_profile_does_not_exist() {
  162. logInAsQProfileAdministrator();
  163. expectedException.expect(NotFoundException.class);
  164. expectedException.expectMessage("Quality Profile with key 'polop' does not exist");
  165. call("polop", "Uh oh, I don't know this profile");
  166. }
  167. @Test
  168. public void fail_if_profile_is_built_in() {
  169. logInAsQProfileAdministrator();
  170. String qualityProfileKey = db.qualityProfiles().insert(organization, p -> p.setIsBuiltIn(true)).getKee();
  171. expectedException.expect(BadRequestException.class);
  172. call(qualityProfileKey, "the new name");
  173. }
  174. @Test
  175. public void fail_if_blank_renaming() {
  176. String qualityProfileKey = createNewValidQualityProfileKey();
  177. expectedException.expect(IllegalArgumentException.class);
  178. expectedException.expectMessage("The 'name' parameter is missing");
  179. call(qualityProfileKey, " ");
  180. }
  181. @Test
  182. public void fail_renaming_if_profile_not_found() {
  183. logInAsQProfileAdministrator();
  184. expectedException.expect(NotFoundException.class);
  185. expectedException.expectMessage("Quality Profile with key 'unknown' does not exist");
  186. call("unknown", "the new name");
  187. }
  188. @Test
  189. public void definition() {
  190. WebService.Action definition = ws.getDef();
  191. assertThat(definition.key()).isEqualTo("rename");
  192. assertThat(definition.isPost()).isTrue();
  193. assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "name");
  194. Param profile = definition.param("key");
  195. assertThat(profile.deprecatedKey()).isNullOrEmpty();
  196. }
  197. private String createNewValidQualityProfileKey() {
  198. QProfileDto qualityProfile = QualityProfileTesting.newQualityProfileDto()
  199. .setOrganizationUuid(organization.getUuid());
  200. db.qualityProfiles().insert(qualityProfile);
  201. return qualityProfile.getKee();
  202. }
  203. private void createProfiles() {
  204. db.qualityProfiles().insert(organization, p -> p.setKee("sonar-way-xoo1-12345").setLanguage(xoo1Key).setName("Sonar way"));
  205. QProfileDto parentXoo2 = db.qualityProfiles().insert(organization, p -> p.setKee("sonar-way-xoo2-23456").setLanguage(xoo2Key).setName("Sonar way"));
  206. db.qualityProfiles().insert(organization, p -> p.setKee("my-sonar-way-xoo2-34567").setLanguage(xoo2Key).setName("My Sonar way").setParentKee(parentXoo2.getKee()));
  207. }
  208. private void logInAsQProfileAdministrator() {
  209. userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES, organization);
  210. }
  211. private void call(@Nullable String key, @Nullable String name) {
  212. TestRequest request = ws.newRequest()
  213. .setMethod("POST");
  214. ofNullable(key).ifPresent(k -> request.setParam("key", k));
  215. ofNullable(name).ifPresent(n -> request.setParam("name", n));
  216. request.execute();
  217. }
  218. }