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.

AddGroupToTemplateActionTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.api.web.UserRole;
  28. import org.sonar.core.permission.GlobalPermissions;
  29. import org.sonar.db.component.ResourceTypesRule;
  30. import org.sonar.db.permission.PermissionQuery;
  31. import org.sonar.db.permission.template.PermissionTemplateDto;
  32. import org.sonar.db.user.GroupDto;
  33. import org.sonar.server.exceptions.BadRequestException;
  34. import org.sonar.server.exceptions.ForbiddenException;
  35. import org.sonar.server.exceptions.NotFoundException;
  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.ADMIN;
  44. import static org.sonar.api.web.UserRole.CODEVIEWER;
  45. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  46. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
  47. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
  48. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
  49. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
  50. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
  51. public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
  52. private PermissionTemplateDto template;
  53. private GroupDto group;
  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 AddGroupToTemplateAction buildWsAction() {
  59. return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
  60. }
  61. @Before
  62. public void setUp() {
  63. template = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
  64. group = db.users().insertGroup(db.getDefaultOrganization(), "group-name");
  65. }
  66. @Test
  67. public void add_group_to_template() {
  68. loginAsAdmin(db.getDefaultOrganization());
  69. newRequest(group.getName(), template.getUuid(), CODEVIEWER);
  70. assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
  71. }
  72. @Test
  73. public void add_group_to_template_by_name() {
  74. loginAsAdmin(db.getDefaultOrganization());
  75. newRequest()
  76. .setParam(PARAM_GROUP_NAME, group.getName())
  77. .setParam(PARAM_PERMISSION, CODEVIEWER)
  78. .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
  79. .execute();
  80. assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
  81. }
  82. @Test
  83. public void add_with_group_id() {
  84. loginAsAdmin(db.getDefaultOrganization());
  85. newRequest()
  86. .setParam(PARAM_TEMPLATE_ID, template.getUuid())
  87. .setParam(PARAM_PERMISSION, CODEVIEWER)
  88. .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
  89. .execute();
  90. assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
  91. }
  92. @Test
  93. public void does_not_add_a_group_twice() {
  94. loginAsAdmin(db.getDefaultOrganization());
  95. newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
  96. newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
  97. assertThat(getGroupNamesInTemplateAndPermission(template, ISSUE_ADMIN)).containsExactly(group.getName());
  98. }
  99. @Test
  100. public void add_anyone_group_to_template() {
  101. loginAsAdmin(db.getDefaultOrganization());
  102. newRequest(ANYONE, template.getUuid(), CODEVIEWER);
  103. assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(ANYONE);
  104. }
  105. @Test
  106. public void fail_if_add_anyone_group_to_admin_permission() {
  107. loginAsAdmin(db.getDefaultOrganization());
  108. expectedException.expect(BadRequestException.class);
  109. expectedException.expectMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'", UserRole.ADMIN));
  110. newRequest(ANYONE, template.getUuid(), ADMIN);
  111. }
  112. @Test
  113. public void fail_if_not_a_project_permission() {
  114. loginAsAdmin(db.getDefaultOrganization());
  115. expectedException.expect(IllegalArgumentException.class);
  116. newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
  117. }
  118. @Test
  119. public void fail_if_not_admin_of_default_organization() {
  120. userSession.logIn();
  121. expectedException.expect(ForbiddenException.class);
  122. newRequest(group.getName(), template.getUuid(), CODEVIEWER);
  123. }
  124. @Test
  125. public void fail_if_group_params_missing() {
  126. loginAsAdmin(db.getDefaultOrganization());
  127. expectedException.expect(BadRequestException.class);
  128. newRequest(null, template.getUuid(), CODEVIEWER);
  129. }
  130. @Test
  131. public void fail_if_permission_missing() {
  132. loginAsAdmin(db.getDefaultOrganization());
  133. expectedException.expect(IllegalArgumentException.class);
  134. newRequest(group.getName(), template.getUuid(), null);
  135. }
  136. @Test
  137. public void fail_if_template_uuid_and_name_missing() {
  138. loginAsAdmin(db.getDefaultOrganization());
  139. expectedException.expect(BadRequestException.class);
  140. newRequest(group.getName(), null, CODEVIEWER);
  141. }
  142. @Test
  143. public void fail_if_group_does_not_exist() {
  144. loginAsAdmin(db.getDefaultOrganization());
  145. expectedException.expect(NotFoundException.class);
  146. expectedException.expectMessage("No group with name 'unknown-group-name'");
  147. newRequest("unknown-group-name", template.getUuid(), CODEVIEWER);
  148. }
  149. @Test
  150. public void fail_if_template_key_does_not_exist() {
  151. loginAsAdmin(db.getDefaultOrganization());
  152. expectedException.expect(NotFoundException.class);
  153. expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
  154. newRequest(group.getName(), "unknown-key", CODEVIEWER);
  155. }
  156. private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
  157. TestRequest request = newRequest();
  158. if (groupName != null) {
  159. request.setParam(PARAM_GROUP_NAME, groupName);
  160. }
  161. if (templateKey != null) {
  162. request.setParam(PARAM_TEMPLATE_ID, templateKey);
  163. }
  164. if (permission != null) {
  165. request.setParam(PARAM_PERMISSION, permission);
  166. }
  167. request.execute();
  168. }
  169. private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
  170. PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(template.getOrganizationUuid()).setPermission(permission).build();
  171. return db.getDbClient().permissionTemplateDao()
  172. .selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getId());
  173. }
  174. }