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.

RemoveGroupActionTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.resources.Languages;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.organization.OrganizationDto;
  28. import org.sonar.db.permission.OrganizationPermission;
  29. import org.sonar.db.qualityprofile.QProfileDto;
  30. import org.sonar.db.user.GroupDto;
  31. import org.sonar.db.user.UserDto;
  32. import org.sonar.server.exceptions.BadRequestException;
  33. import org.sonar.server.exceptions.ForbiddenException;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.language.LanguageTesting;
  36. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  37. import org.sonar.server.tester.UserSessionRule;
  38. import org.sonar.server.ws.TestResponse;
  39. import org.sonar.server.ws.WsActionTester;
  40. import static java.lang.String.format;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_GROUP;
  43. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
  44. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
  45. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
  46. public class RemoveGroupActionTest {
  47. private static final String XOO = "xoo";
  48. private static final Languages LANGUAGES = LanguageTesting.newLanguages(XOO);
  49. @Rule
  50. public ExpectedException expectedException = ExpectedException.none();
  51. @Rule
  52. public UserSessionRule userSession = UserSessionRule.standalone();
  53. @Rule
  54. public DbTester db = DbTester.create();
  55. private QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db));
  56. private WsActionTester ws = new WsActionTester(new RemoveGroupAction(db.getDbClient(), wsSupport, LANGUAGES));
  57. @Test
  58. public void test_definition() {
  59. WebService.Action def = ws.getDef();
  60. assertThat(def.key()).isEqualTo("remove_group");
  61. assertThat(def.isPost()).isTrue();
  62. assertThat(def.isInternal()).isTrue();
  63. assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("organization", "qualityProfile", "language", "group");
  64. }
  65. @Test
  66. public void remove_group() {
  67. OrganizationDto organization = db.organizations().insert();
  68. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  69. GroupDto group = db.users().insertGroup(organization);
  70. db.qualityProfiles().addGroupPermission(profile, group);
  71. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  72. TestResponse response = ws.newRequest()
  73. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  74. .setParam(PARAM_LANGUAGE, XOO)
  75. .setParam(PARAM_GROUP, group.getName())
  76. .setParam(PARAM_ORGANIZATION, organization.getKey())
  77. .execute();
  78. assertThat(response.getStatus()).isEqualTo(204);
  79. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  80. }
  81. @Test
  82. public void does_nothing_when_group_cannot_edit_profile() {
  83. OrganizationDto organization = db.organizations().insert();
  84. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  85. GroupDto group = db.users().insertGroup(organization);
  86. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  87. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  88. ws.newRequest()
  89. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  90. .setParam(PARAM_LANGUAGE, XOO)
  91. .setParam(PARAM_GROUP, group.getName())
  92. .setParam(PARAM_ORGANIZATION, organization.getKey())
  93. .execute();
  94. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  95. }
  96. @Test
  97. public void qp_administers_can_remove_group() {
  98. OrganizationDto organization = db.organizations().insert();
  99. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  100. GroupDto group = db.users().insertGroup(organization);
  101. db.qualityProfiles().addGroupPermission(profile, group);
  102. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  103. ws.newRequest()
  104. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  105. .setParam(PARAM_LANGUAGE, XOO)
  106. .setParam(PARAM_GROUP, group.getName())
  107. .setParam(PARAM_ORGANIZATION, organization.getKey())
  108. .execute();
  109. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  110. }
  111. @Test
  112. public void qp_editors_can_remove_group() {
  113. OrganizationDto organization = db.organizations().insert();
  114. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  115. GroupDto group = db.users().insertGroup(organization);
  116. db.qualityProfiles().addGroupPermission(profile, group);
  117. UserDto userAllowedToEditProfile = db.users().insertUser();
  118. db.qualityProfiles().addUserPermission(profile, userAllowedToEditProfile);
  119. userSession.logIn(userAllowedToEditProfile);
  120. ws.newRequest()
  121. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  122. .setParam(PARAM_LANGUAGE, XOO)
  123. .setParam(PARAM_GROUP, group.getName())
  124. .setParam(PARAM_ORGANIZATION, organization.getKey())
  125. .execute();
  126. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  127. }
  128. @Test
  129. public void uses_default_organization_when_no_organization() {
  130. OrganizationDto organization = db.getDefaultOrganization();
  131. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  132. GroupDto group = db.users().insertGroup(organization);
  133. db.qualityProfiles().addGroupPermission(profile, group);
  134. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  135. ws.newRequest()
  136. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  137. .setParam(PARAM_LANGUAGE, XOO)
  138. .setParam(PARAM_GROUP, group.getName())
  139. .execute();
  140. assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
  141. }
  142. @Test
  143. public void fail_when_group_does_not_exist() {
  144. OrganizationDto organization = db.organizations().insert();
  145. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  146. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  147. expectedException.expect(NotFoundException.class);
  148. expectedException.expectMessage(format("No group with name 'unknown' in organization '%s'", organization.getKey()));
  149. ws.newRequest()
  150. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  151. .setParam(PARAM_LANGUAGE, XOO)
  152. .setParam(PARAM_GROUP, "unknown")
  153. .setParam(PARAM_ORGANIZATION, organization.getKey())
  154. .execute();
  155. }
  156. @Test
  157. public void fail_when_qprofile_does_not_exist() {
  158. OrganizationDto organization = db.organizations().insert();
  159. GroupDto group = db.users().insertGroup(organization);
  160. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  161. expectedException.expect(NotFoundException.class);
  162. expectedException.expectMessage(format("Quality Profile for language 'xoo' and name 'unknown' does not exist in organization '%s'", organization.getKey()));
  163. ws.newRequest()
  164. .setParam(PARAM_QUALITY_PROFILE, "unknown")
  165. .setParam(PARAM_LANGUAGE, XOO)
  166. .setParam(PARAM_GROUP, group.getName())
  167. .setParam(PARAM_ORGANIZATION, organization.getKey())
  168. .execute();
  169. }
  170. @Test
  171. public void fail_when_qprofile_does_not_belong_to_organization() {
  172. OrganizationDto organization = db.organizations().insert();
  173. GroupDto group = db.users().insertGroup(organization);
  174. OrganizationDto anotherOrganization = db.organizations().insert();
  175. QProfileDto profile = db.qualityProfiles().insert(anotherOrganization, p -> p.setLanguage(XOO));
  176. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  177. expectedException.expect(NotFoundException.class);
  178. expectedException.expectMessage(format("Quality Profile for language 'xoo' and name '%s' does not exist in organization '%s'", profile.getName(), organization.getKey()));
  179. ws.newRequest()
  180. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  181. .setParam(PARAM_LANGUAGE, XOO)
  182. .setParam(PARAM_GROUP, group.getName())
  183. .setParam(PARAM_ORGANIZATION, organization.getKey())
  184. .execute();
  185. }
  186. @Test
  187. public void fail_when_wrong_language() {
  188. OrganizationDto organization = db.organizations().insert();
  189. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage("unknown"));
  190. GroupDto group = db.users().insertGroup(organization);
  191. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  192. expectedException.expect(NotFoundException.class);
  193. expectedException.expectMessage(format("Quality Profile for language 'xoo' and name '%s' does not exist in organization '%s'", profile.getName(), organization.getKey()));
  194. ws.newRequest()
  195. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  196. .setParam(PARAM_LANGUAGE, XOO)
  197. .setParam(PARAM_GROUP, group.getName())
  198. .setParam(PARAM_ORGANIZATION, organization.getKey())
  199. .execute();
  200. }
  201. @Test
  202. public void fail_when_group_does_not_belong_to_organization() {
  203. OrganizationDto organization = db.organizations().insert();
  204. OrganizationDto anotherOrganization = db.organizations().insert();
  205. GroupDto group = db.users().insertGroup(anotherOrganization);
  206. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  207. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  208. expectedException.expect(NotFoundException.class);
  209. expectedException.expectMessage(format("No group with name '%s' in organization '%s'", group.getName(), organization.getKey()));
  210. ws.newRequest()
  211. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  212. .setParam(PARAM_LANGUAGE, XOO)
  213. .setParam(PARAM_GROUP, group.getName())
  214. .setParam(PARAM_ORGANIZATION, organization.getKey())
  215. .execute();
  216. }
  217. @Test
  218. public void fail_when_qp_is_built_in() {
  219. OrganizationDto organization = db.organizations().insert();
  220. GroupDto group = db.users().insertGroup(organization);
  221. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO).setIsBuiltIn(true));
  222. userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
  223. expectedException.expect(BadRequestException.class);
  224. expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s' with language 'xoo'", profile.getName()));
  225. ws.newRequest()
  226. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  227. .setParam(PARAM_LANGUAGE, XOO)
  228. .setParam(PARAM_GROUP, group.getName())
  229. .setParam(PARAM_ORGANIZATION, organization.getKey())
  230. .execute();
  231. }
  232. @Test
  233. public void fail_when_not_enough_permission() {
  234. OrganizationDto organization = db.organizations().insert();
  235. QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO));
  236. GroupDto group = db.users().insertGroup(organization);
  237. userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organization);
  238. expectedException.expect(ForbiddenException.class);
  239. ws.newRequest()
  240. .setParam(PARAM_QUALITY_PROFILE, profile.getName())
  241. .setParam(PARAM_LANGUAGE, XOO)
  242. .setParam(PARAM_GROUP, group.getName())
  243. .setParam(PARAM_ORGANIZATION, organization.getKey())
  244. .execute();
  245. }
  246. }