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.

CreateTemplateActionTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 javax.annotation.Nullable;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.api.utils.internal.TestSystem2;
  25. import org.sonar.db.permission.template.PermissionTemplateDto;
  26. import org.sonar.server.exceptions.BadRequestException;
  27. import org.sonar.server.exceptions.ForbiddenException;
  28. import org.sonar.server.permission.ws.BasePermissionWsTest;
  29. import org.sonar.server.ws.TestRequest;
  30. import org.sonar.server.ws.TestResponse;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
  33. import static org.sonar.test.JsonAssert.assertJson;
  34. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
  35. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
  36. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
  37. public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplateAction> {
  38. private static final long NOW = 1_440_512_328_743L;
  39. private System2 system = new TestSystem2().setNow(NOW);
  40. @Override
  41. protected CreateTemplateAction buildWsAction() {
  42. return new CreateTemplateAction(db.getDbClient(), userSession, system, newPermissionWsSupport());
  43. }
  44. @Test
  45. public void create_full_permission_template() {
  46. loginAsAdmin(db.getDefaultOrganization());
  47. TestResponse result = newRequest("Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
  48. assertJson(result.getInput())
  49. .ignoreFields("id")
  50. .isSimilarTo(getClass().getResource("create_template-example.json"));
  51. PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
  52. assertThat(finance.getName()).isEqualTo("Finance");
  53. assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
  54. assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
  55. assertThat(finance.getUuid()).isNotEmpty();
  56. assertThat(finance.getCreatedAt().getTime()).isEqualTo(NOW);
  57. assertThat(finance.getUpdatedAt().getTime()).isEqualTo(NOW);
  58. }
  59. @Test
  60. public void create_minimalist_permission_template() {
  61. loginAsAdmin(db.getDefaultOrganization());
  62. newRequest("Finance", null, null);
  63. PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
  64. assertThat(finance.getName()).isEqualTo("Finance");
  65. assertThat(finance.getDescription()).isNullOrEmpty();
  66. assertThat(finance.getKeyPattern()).isNullOrEmpty();
  67. assertThat(finance.getUuid()).isNotEmpty();
  68. assertThat(finance.getCreatedAt().getTime()).isEqualTo(NOW);
  69. assertThat(finance.getUpdatedAt().getTime()).isEqualTo(NOW);
  70. }
  71. @Test
  72. public void fail_if_name_not_provided() {
  73. loginAsAdmin(db.getDefaultOrganization());
  74. expectedException.expect(IllegalArgumentException.class);
  75. newRequest(null, null, null);
  76. }
  77. @Test
  78. public void fail_if_name_empty() {
  79. loginAsAdmin(db.getDefaultOrganization());
  80. expectedException.expect(IllegalArgumentException.class);
  81. expectedException.expectMessage("The 'name' parameter is missing");
  82. newRequest("", null, null);
  83. }
  84. @Test
  85. public void fail_if_regexp_if_not_valid() {
  86. loginAsAdmin(db.getDefaultOrganization());
  87. expectedException.expect(BadRequestException.class);
  88. expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
  89. newRequest("Finance", null, "[azerty");
  90. }
  91. @Test
  92. public void fail_if_name_already_exists_in_database_case_insensitive() {
  93. loginAsAdmin(db.getDefaultOrganization());
  94. PermissionTemplateDto template = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
  95. expectedException.expect(BadRequestException.class);
  96. expectedException.expectMessage("A template with the name '" + template.getName() + "' already exists (case insensitive).");
  97. newRequest(template.getName(), null, null);
  98. }
  99. @Test
  100. public void fail_if_not_admin() {
  101. userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization());
  102. expectedException.expect(ForbiddenException.class);
  103. newRequest("Finance", null, null);
  104. }
  105. private TestResponse newRequest(@Nullable String name, @Nullable String description, @Nullable String projectPattern) {
  106. TestRequest request = newRequest();
  107. if (name != null) {
  108. request.setParam(PARAM_NAME, name);
  109. }
  110. if (description != null) {
  111. request.setParam(PARAM_DESCRIPTION, description);
  112. }
  113. if (projectPattern != null) {
  114. request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
  115. }
  116. return request.execute();
  117. }
  118. }