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.

RemoveGroupFromTemplateActionTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.permission.ws.template;
  21. import java.util.List;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.sonar.api.resources.Qualifiers;
  26. import org.sonar.api.resources.ResourceTypes;
  27. import org.sonar.core.permission.GlobalPermissions;
  28. import org.sonar.db.component.ResourceTypesRule;
  29. import org.sonar.db.permission.PermissionQuery;
  30. import org.sonar.db.permission.template.PermissionTemplateDto;
  31. import org.sonar.db.user.GroupDto;
  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.exceptions.UnauthorizedException;
  36. import org.sonar.server.permission.PermissionService;
  37. import org.sonar.server.permission.PermissionServiceImpl;
  38. import org.sonar.server.permission.ws.BasePermissionWsTest;
  39. import org.sonar.server.permission.ws.WsParameters;
  40. import org.sonar.server.ws.TestRequest;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.sonar.api.security.DefaultGroups.ANYONE;
  43. import static org.sonar.api.web.UserRole.CODEVIEWER;
  44. import static org.sonar.db.permission.OrganizationPermission.SCAN;
  45. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
  46. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
  47. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
  48. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
  49. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
  50. public class RemoveGroupFromTemplateActionTest extends BasePermissionWsTest<RemoveGroupFromTemplateAction> {
  51. private static final String PERMISSION = CODEVIEWER;
  52. private GroupDto group;
  53. private PermissionTemplateDto template;
  54. private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
  55. private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
  56. private WsParameters wsParameters = new WsParameters(permissionService);
  57. @Override
  58. protected RemoveGroupFromTemplateAction buildWsAction() {
  59. return new RemoveGroupFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
  60. }
  61. @Before
  62. public void setUp() {
  63. loginAsAdmin(db.getDefaultOrganization());
  64. group = db.users().insertGroup(db.getDefaultOrganization(), "group-name");
  65. template = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
  66. addGroupToTemplate(template, group.getId(), PERMISSION);
  67. }
  68. @Test
  69. public void remove_group_from_template() {
  70. newRequest(group.getName(), template.getUuid(), PERMISSION);
  71. assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
  72. }
  73. @Test
  74. public void remove_group_from_template_by_name_case_insensitive() {
  75. newRequest()
  76. .setParam(PARAM_GROUP_NAME, group.getName())
  77. .setParam(PARAM_PERMISSION, PERMISSION)
  78. .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
  79. .execute();
  80. assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
  81. }
  82. @Test
  83. public void remove_group_with_group_id() {
  84. newRequest()
  85. .setParam(PARAM_TEMPLATE_ID, template.getUuid())
  86. .setParam(PARAM_PERMISSION, PERMISSION)
  87. .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
  88. .execute();
  89. assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
  90. }
  91. @Test
  92. public void remove_group_twice_without_error() {
  93. newRequest(group.getName(), template.getUuid(), PERMISSION);
  94. newRequest(group.getName(), template.getUuid(), PERMISSION);
  95. assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
  96. }
  97. @Test
  98. public void remove_anyone_group_from_template() {
  99. addGroupToTemplate(template, null, PERMISSION);
  100. newRequest(ANYONE, template.getUuid(), PERMISSION);
  101. assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).containsExactly(group.getName());
  102. }
  103. @Test
  104. public void fail_if_not_a_project_permission() {
  105. expectedException.expect(IllegalArgumentException.class);
  106. newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
  107. }
  108. @Test
  109. public void fail_if_insufficient_privileges() {
  110. userSession.logIn().addPermission(SCAN, db.getDefaultOrganization());
  111. expectedException.expect(ForbiddenException.class);
  112. newRequest(group.getName(), template.getUuid(), PERMISSION);
  113. }
  114. @Test
  115. public void fail_if_not_logged_in() {
  116. expectedException.expect(UnauthorizedException.class);
  117. userSession.anonymous();
  118. newRequest(group.getName(), template.getUuid(), PERMISSION);
  119. }
  120. @Test
  121. public void fail_if_group_params_missing() {
  122. expectedException.expect(BadRequestException.class);
  123. newRequest(null, template.getUuid(), PERMISSION);
  124. }
  125. @Test
  126. public void fail_if_permission_missing() {
  127. expectedException.expect(IllegalArgumentException.class);
  128. newRequest(group.getName(), template.getUuid(), null);
  129. }
  130. @Test
  131. public void fail_if_template_missing() {
  132. expectedException.expect(BadRequestException.class);
  133. newRequest(group.getName(), null, PERMISSION);
  134. }
  135. @Test
  136. public void fail_if_group_does_not_exist() {
  137. expectedException.expect(NotFoundException.class);
  138. expectedException.expectMessage("No group with name 'unknown-group-name'");
  139. newRequest("unknown-group-name", template.getUuid(), PERMISSION);
  140. }
  141. @Test
  142. public void fail_if_template_key_does_not_exist() {
  143. expectedException.expect(NotFoundException.class);
  144. expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
  145. newRequest(group.getName(), "unknown-key", PERMISSION);
  146. }
  147. private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
  148. TestRequest request = newRequest();
  149. if (groupName != null) {
  150. request.setParam(PARAM_GROUP_NAME, groupName);
  151. }
  152. if (templateKey != null) {
  153. request.setParam(PARAM_TEMPLATE_ID, templateKey);
  154. }
  155. if (permission != null) {
  156. request.setParam(PARAM_PERMISSION, permission);
  157. }
  158. request.execute();
  159. }
  160. private void addGroupToTemplate(PermissionTemplateDto template, @Nullable Integer groupId, String permission) {
  161. db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), template.getId(), groupId, permission);
  162. db.commit();
  163. }
  164. private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
  165. PermissionQuery permissionQuery = PermissionQuery.builder().setOrganizationUuid(template.getOrganizationUuid()).setPermission(permission).build();
  166. return db.getDbClient().permissionTemplateDao()
  167. .selectGroupNamesByQueryAndTemplate(db.getSession(), permissionQuery, template.getId());
  168. }
  169. }