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.

UpdateTemplateActionTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.Date;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.db.permission.template.PermissionTemplateDto;
  27. import org.sonar.server.exceptions.BadRequestException;
  28. import org.sonar.server.exceptions.ForbiddenException;
  29. import org.sonar.server.exceptions.NotFoundException;
  30. import org.sonar.server.exceptions.UnauthorizedException;
  31. import org.sonar.server.permission.ws.BasePermissionWsTest;
  32. import org.sonar.server.ws.TestRequest;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.mockito.Mockito.spy;
  35. import static org.mockito.Mockito.when;
  36. import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
  37. import static org.sonar.db.permission.OrganizationPermission.SCAN;
  38. import static org.sonar.test.JsonAssert.assertJson;
  39. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
  40. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID;
  41. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
  42. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
  43. public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplateAction> {
  44. private System2 system = spy(System2.INSTANCE);
  45. private PermissionTemplateDto template;
  46. @Override
  47. protected UpdateTemplateAction buildWsAction() {
  48. return new UpdateTemplateAction(db.getDbClient(), userSession, system, newPermissionWsSupport());
  49. }
  50. @Before
  51. public void setUp() {
  52. when(system.now()).thenReturn(1_440_512_328_743L);
  53. template = db.getDbClient().permissionTemplateDao().insert(db.getSession(), newPermissionTemplateDto()
  54. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  55. .setName("Permission Template Name")
  56. .setDescription("Permission Template Description")
  57. .setKeyPattern(".*\\.pattern\\..*")
  58. .setCreatedAt(new Date(1_000_000_000_000L))
  59. .setUpdatedAt(new Date(1_000_000_000_000L)));
  60. db.commit();
  61. }
  62. @Test
  63. public void update_all_permission_template_fields() {
  64. loginAsAdmin(db.getDefaultOrganization());
  65. String result = call(template.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
  66. assertJson(result)
  67. .ignoreFields("id")
  68. .isSimilarTo(getClass().getResource("update_template-example.json"));
  69. PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
  70. assertThat(finance.getName()).isEqualTo("Finance");
  71. assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
  72. assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
  73. assertThat(finance.getUuid()).isEqualTo(template.getUuid());
  74. assertThat(finance.getCreatedAt()).isEqualTo(template.getCreatedAt());
  75. assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
  76. }
  77. @Test
  78. public void update_with_the_same_values() {
  79. loginAsAdmin(db.getDefaultOrganization());
  80. call(template.getUuid(), template.getName(), template.getDescription(), template.getKeyPattern());
  81. PermissionTemplateDto reloaded = db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid());
  82. assertThat(reloaded.getName()).isEqualTo(template.getName());
  83. assertThat(reloaded.getDescription()).isEqualTo(template.getDescription());
  84. assertThat(reloaded.getKeyPattern()).isEqualTo(template.getKeyPattern());
  85. }
  86. @Test
  87. public void update_name_only() {
  88. loginAsAdmin(db.getDefaultOrganization());
  89. call(template.getUuid(), "Finance", null, null);
  90. PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
  91. assertThat(finance.getName()).isEqualTo("Finance");
  92. assertThat(finance.getDescription()).isEqualTo(template.getDescription());
  93. assertThat(finance.getKeyPattern()).isEqualTo(template.getKeyPattern());
  94. }
  95. @Test
  96. public void fail_if_key_is_not_found() {
  97. loginAsAdmin(db.getDefaultOrganization());
  98. expectedException.expect(NotFoundException.class);
  99. expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
  100. call("unknown-key", null, null, null);
  101. }
  102. @Test
  103. public void fail_if_name_already_exists_in_another_template() {
  104. loginAsAdmin(db.getDefaultOrganization());
  105. PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
  106. expectedException.expect(BadRequestException.class);
  107. expectedException.expectMessage("A template with the name '" + anotherTemplate.getName() + "' already exists (case insensitive).");
  108. call(this.template.getUuid(), anotherTemplate.getName(), null, null);
  109. }
  110. @Test
  111. public void fail_if_key_is_not_provided() {
  112. loginAsAdmin(db.getDefaultOrganization());
  113. expectedException.expect(IllegalArgumentException.class);
  114. call(null, "Finance", null, null);
  115. }
  116. @Test
  117. public void fail_if_name_empty() {
  118. loginAsAdmin(db.getDefaultOrganization());
  119. expectedException.expect(BadRequestException.class);
  120. expectedException.expectMessage("The template name must not be blank");
  121. call(template.getUuid(), "", null, null);
  122. }
  123. @Test
  124. public void fail_if_name_has_just_whitespaces() {
  125. loginAsAdmin(db.getDefaultOrganization());
  126. expectedException.expect(BadRequestException.class);
  127. expectedException.expectMessage("The template name must not be blank");
  128. call(template.getUuid(), " \r\n", null, null);
  129. }
  130. @Test
  131. public void fail_if_regexp_if_not_valid() {
  132. loginAsAdmin(db.getDefaultOrganization());
  133. expectedException.expect(BadRequestException.class);
  134. expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
  135. call(template.getUuid(), "Finance", null, "[azerty");
  136. }
  137. @Test
  138. public void fail_if_name_already_exists_in_database_case_insensitive() {
  139. loginAsAdmin(db.getDefaultOrganization());
  140. PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
  141. String nameCaseInsensitive = anotherTemplate.getName().toUpperCase();
  142. expectedException.expect(BadRequestException.class);
  143. expectedException.expectMessage("A template with the name '" + nameCaseInsensitive + "' already exists (case insensitive).");
  144. call(this.template.getUuid(), nameCaseInsensitive, null, null);
  145. }
  146. @Test
  147. public void fail_if_not_logged_in() {
  148. expectedException.expect(UnauthorizedException.class);
  149. userSession.anonymous();
  150. call(template.getUuid(), "Finance", null, null);
  151. }
  152. @Test
  153. public void fail_if_not_admin() {
  154. userSession.logIn().addPermission(SCAN, db.getDefaultOrganization());
  155. expectedException.expect(ForbiddenException.class);
  156. call(template.getUuid(), "Finance", null, null);
  157. }
  158. private String call(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
  159. TestRequest request = newRequest();
  160. if (key != null) {
  161. request.setParam(PARAM_ID, key);
  162. }
  163. if (name != null) {
  164. request.setParam(PARAM_NAME, name);
  165. }
  166. if (description != null) {
  167. request.setParam(PARAM_DESCRIPTION, description);
  168. }
  169. if (projectPattern != null) {
  170. request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
  171. }
  172. return request.execute().getInput();
  173. }
  174. }