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.

CreateActionTest.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.qualitygate.ws;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Optional;
  25. import javax.annotation.Nullable;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.runner.RunWith;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.core.util.UuidFactoryFast;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.organization.OrganizationDbTester;
  37. import org.sonar.db.organization.OrganizationDto;
  38. import org.sonar.db.qualitygate.QGateWithOrgDto;
  39. import org.sonar.db.qualitygate.QualityGateDto;
  40. import org.sonar.server.exceptions.ForbiddenException;
  41. import org.sonar.server.exceptions.NotFoundException;
  42. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  43. import org.sonar.server.qualitygate.QualityGateUpdater;
  44. import org.sonar.server.tester.UserSessionRule;
  45. import org.sonar.server.ws.TestRequest;
  46. import org.sonar.server.ws.WsActionTester;
  47. import org.sonarqube.ws.Qualitygates.CreateResponse;
  48. import static org.assertj.core.api.Assertions.assertThat;
  49. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
  50. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
  51. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
  52. @RunWith(DataProviderRunner.class)
  53. public class CreateActionTest {
  54. @Rule
  55. public ExpectedException expectedException = ExpectedException.none();
  56. @Rule
  57. public UserSessionRule userSession = UserSessionRule.standalone();
  58. @Rule
  59. public DbTester db = DbTester.create(System2.INSTANCE);
  60. private OrganizationDbTester organizationDbTester = new OrganizationDbTester(db);
  61. private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  62. private DbClient dbClient = db.getDbClient();
  63. private DbSession dbSession = db.getSession();
  64. private CreateAction underTest = new CreateAction(dbClient, userSession, new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()),
  65. new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider));
  66. private WsActionTester ws = new WsActionTester(underTest);
  67. @Test
  68. public void default_organization_is_used_when_no_parameter() {
  69. logInAsQualityGateAdmin(db.getDefaultOrganization());
  70. String qgName = "Default";
  71. CreateResponse response = executeRequest(Optional.empty(), qgName);
  72. assertThat(response.getName()).isEqualTo(qgName);
  73. assertThat(response.getId()).isNotNull();
  74. dbSession.commit();
  75. QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, db.getDefaultOrganization(), qgName);
  76. assertThat(qualityGateDto).isNotNull();
  77. }
  78. @Test
  79. public void create_quality_gate_with_organization() {
  80. OrganizationDto organizationDto = organizationDbTester.insert();
  81. logInAsQualityGateAdmin(organizationDto);
  82. String qgName = "Default";
  83. CreateResponse response = executeRequest(Optional.of(organizationDto), qgName);
  84. assertThat(response.getName()).isEqualTo(qgName);
  85. assertThat(response.getId()).isNotNull();
  86. dbSession.commit();
  87. QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organizationDto, qgName);
  88. assertThat(qualityGateDto).isNotNull();
  89. }
  90. @Test
  91. public void creating_a_qg_with_a_name_used_in_another_organization_should_work() {
  92. OrganizationDto anOrganization = db.organizations().insert();
  93. QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(anOrganization);
  94. OrganizationDto anotherOrganization = db.organizations().insert();
  95. userSession.addPermission(ADMINISTER_QUALITY_GATES, anotherOrganization);
  96. CreateResponse response = ws.newRequest()
  97. .setParam(PARAM_NAME, qualityGate.getName())
  98. .setParam(PARAM_ORGANIZATION, anotherOrganization.getKey())
  99. .executeProtobuf(CreateResponse.class);
  100. assertThat(response.getName()).isEqualTo(qualityGate.getName());
  101. assertThat(response.getId()).isNotEqualTo(qualityGate.getId());
  102. }
  103. @Test
  104. public void test_ws_definition() {
  105. WebService.Action action = ws.getDef();
  106. assertThat(action).isNotNull();
  107. assertThat(action.isInternal()).isFalse();
  108. assertThat(action.isPost()).isTrue();
  109. assertThat(action.responseExampleAsString()).isNotEmpty();
  110. assertThat(action.params()).hasSize(2);
  111. }
  112. @Test
  113. public void throw_ForbiddenException_if_incorrect_organization() {
  114. logInAsQualityGateAdmin(db.getDefaultOrganization());
  115. OrganizationDto otherOrganization = organizationDbTester.insert();
  116. expectedException.expect(ForbiddenException.class);
  117. expectedException.expectMessage("Insufficient privileges");
  118. executeRequest(Optional.of(otherOrganization), "Default");
  119. }
  120. @Test
  121. public void throw_ForbiddenException_if_not_gate_administrator() {
  122. userSession.logIn();
  123. expectedException.expect(ForbiddenException.class);
  124. expectedException.expectMessage("Insufficient privileges");
  125. executeRequest(Optional.empty(), "Default");
  126. }
  127. @Test
  128. public void throw_ForbiddenException_if_not_gate_administrator_of_own_organization() {
  129. // as long as organizations don't support Quality gates, the global permission
  130. // is defined on the default organization
  131. OrganizationDto org = db.organizations().insert();
  132. userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, org);
  133. expectedException.expect(ForbiddenException.class);
  134. expectedException.expectMessage("Insufficient privileges");
  135. executeRequest(Optional.empty(), "Default");
  136. }
  137. @Test
  138. public void throw_ForbiddenException_if_unknown_organization() {
  139. OrganizationDto org = new OrganizationDto().setName("Unknown organization").setKey("unknown_key");
  140. userSession.logIn();
  141. expectedException.expect(NotFoundException.class);
  142. expectedException.expectMessage("No organization with key 'unknown_key'");
  143. executeRequest(Optional.of(org), "Default");
  144. }
  145. @Test
  146. public void throw_BadRequestException_if_name_is_already_used() {
  147. OrganizationDto org = db.organizations().insert();
  148. userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, org);
  149. executeRequest(Optional.of(org), "Default");
  150. expectedException.expect(IllegalArgumentException.class);
  151. expectedException.expectMessage("Name has already been taken");
  152. executeRequest(Optional.of(org), "Default");
  153. }
  154. @Test
  155. @UseDataProvider("nullOrEmpty")
  156. public void fail_when_name_parameter_is_empty(@Nullable String nameParameter) {
  157. OrganizationDto org = db.organizations().insert();
  158. userSession.addPermission(ADMINISTER_QUALITY_GATES, org);
  159. TestRequest request = ws.newRequest()
  160. .setParam(PARAM_ORGANIZATION, org.getKey());
  161. Optional.ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, ""));
  162. expectedException.expect(IllegalArgumentException.class);
  163. expectedException.expectMessage("The 'name' parameter is missing");
  164. request.execute();
  165. }
  166. @DataProvider
  167. public static Object[][] nullOrEmpty() {
  168. return new Object[][] {
  169. {null},
  170. {""},
  171. {" "}
  172. };
  173. }
  174. private CreateResponse executeRequest(Optional<OrganizationDto> organization, String qualitGateName) {
  175. if (organization.isPresent()) {
  176. return ws.newRequest()
  177. .setParam("name", qualitGateName)
  178. .setParam("organization", organization.get().getKey())
  179. .executeProtobuf(CreateResponse.class);
  180. } else {
  181. return ws.newRequest()
  182. .setParam("name", qualitGateName)
  183. .executeProtobuf(CreateResponse.class);
  184. }
  185. }
  186. private void logInAsQualityGateAdmin(OrganizationDto organizationDto) {
  187. userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, organizationDto);
  188. }
  189. }