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.

DoTransitionActionTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.issue.ws;
  21. import javax.annotation.Nullable;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.mockito.ArgumentCaptor;
  27. import org.sonar.api.impl.utils.TestSystem2;
  28. import org.sonar.api.rules.RuleType;
  29. import org.sonar.api.server.ws.Request;
  30. import org.sonar.api.server.ws.Response;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbTester;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.db.rule.RuleDefinitionDto;
  37. import org.sonar.server.es.EsTester;
  38. import org.sonar.server.exceptions.ForbiddenException;
  39. import org.sonar.server.exceptions.NotFoundException;
  40. import org.sonar.server.exceptions.UnauthorizedException;
  41. import org.sonar.server.issue.IssueFieldsSetter;
  42. import org.sonar.server.issue.IssueFinder;
  43. import org.sonar.server.issue.TestIssueChangePostProcessor;
  44. import org.sonar.server.issue.TransitionService;
  45. import org.sonar.server.issue.WebIssueStorage;
  46. import org.sonar.server.issue.index.IssueIndexer;
  47. import org.sonar.server.issue.index.IssueIteratorFactory;
  48. import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer;
  49. import org.sonar.server.issue.workflow.FunctionExecutor;
  50. import org.sonar.server.issue.workflow.IssueWorkflow;
  51. import org.sonar.server.notification.NotificationManager;
  52. import org.sonar.server.organization.DefaultOrganizationProvider;
  53. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  54. import org.sonar.server.rule.DefaultRuleFinder;
  55. import org.sonar.server.tester.UserSessionRule;
  56. import org.sonar.server.ws.TestRequest;
  57. import org.sonar.server.ws.TestResponse;
  58. import org.sonar.server.ws.WsAction;
  59. import org.sonar.server.ws.WsActionTester;
  60. import static org.assertj.core.api.Assertions.assertThat;
  61. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  62. import static org.mockito.ArgumentMatchers.any;
  63. import static org.mockito.ArgumentMatchers.eq;
  64. import static org.mockito.Mockito.mock;
  65. import static org.mockito.Mockito.verify;
  66. import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
  67. import static org.sonar.api.issue.Issue.STATUS_OPEN;
  68. import static org.sonar.api.rules.RuleType.CODE_SMELL;
  69. import static org.sonar.api.web.UserRole.CODEVIEWER;
  70. import static org.sonar.api.web.UserRole.USER;
  71. import static org.sonar.db.component.ComponentTesting.newFileDto;
  72. public class DoTransitionActionTest {
  73. private static final long NOW = 999_776_888L;
  74. private System2 system2 = new TestSystem2().setNow(NOW);
  75. @Rule
  76. public ExpectedException expectedException = ExpectedException.none();
  77. @Rule
  78. public DbTester db = DbTester.create(system2);
  79. @Rule
  80. public EsTester es = EsTester.create();
  81. @Rule
  82. public UserSessionRule userSession = UserSessionRule.standalone();
  83. private DbClient dbClient = db.getDbClient();
  84. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  85. private IssueFieldsSetter updater = new IssueFieldsSetter();
  86. private IssueWorkflow workflow = new IssueWorkflow(new FunctionExecutor(updater), updater);
  87. private TransitionService transitionService = new TransitionService(userSession, workflow);
  88. private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
  89. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
  90. private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor();
  91. private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer();
  92. private IssueUpdater issueUpdater = new IssueUpdater(dbClient,
  93. new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, defaultOrganizationProvider), issueIndexer), mock(NotificationManager.class),
  94. issueChangePostProcessor, issuesChangesSerializer);
  95. private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
  96. private WsAction underTest = new DoTransitionAction(dbClient, userSession, new IssueFinder(dbClient, userSession), issueUpdater, transitionService, responseWriter, system2);
  97. private WsActionTester tester = new WsActionTester(underTest);
  98. @Before
  99. public void setUp() {
  100. workflow.start();
  101. }
  102. @Test
  103. public void do_transition() {
  104. ComponentDto project = db.components().insertMainBranch();
  105. ComponentDto file = db.components().insertComponent(newFileDto(project));
  106. RuleDefinitionDto rule = db.rules().insertIssueRule();
  107. IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  108. userSession.logIn(db.users().insertUser()).addProjectPermission(USER, project, file);
  109. call(issue.getKey(), "confirm");
  110. verify(responseWriter).write(eq(issue.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  111. verifyContentOfPreloadedSearchResponseData(issue);
  112. IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get();
  113. assertThat(issueReloaded.getStatus()).isEqualTo(STATUS_CONFIRMED);
  114. assertThat(issueChangePostProcessor.calledComponents()).containsExactlyInAnyOrder(file);
  115. }
  116. @Test
  117. public void fail_if_external_issue() {
  118. ComponentDto project = db.components().insertPrivateProject();
  119. ComponentDto file = db.components().insertComponent(newFileDto(project));
  120. RuleDefinitionDto externalRule = db.rules().insertIssueRule(r -> r.setIsExternal(true));
  121. IssueDto externalIssue = db.issues().insertIssue(externalRule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  122. userSession.logIn().addProjectPermission(USER, project, file);
  123. expectedException.expect(IllegalArgumentException.class);
  124. expectedException.expectMessage("Transition is not allowed on issues imported from external rule engines");
  125. call(externalIssue.getKey(), "confirm");
  126. }
  127. @Test
  128. public void fail_if_hotspot() {
  129. ComponentDto project = db.components().insertPrivateProject();
  130. ComponentDto file = db.components().insertComponent(newFileDto(project));
  131. RuleDefinitionDto rule = db.rules().insertHotspotRule();
  132. IssueDto hotspot = db.issues().insertHotspot(rule, project, file, i -> i.setType(RuleType.SECURITY_HOTSPOT));
  133. userSession.logIn().addProjectPermission(USER, project, file);
  134. assertThatThrownBy(() -> call(hotspot.getKey(), "confirm"))
  135. .isInstanceOf(NotFoundException.class)
  136. .hasMessage("Issue with key '%s' does not exist", hotspot.getKey());
  137. }
  138. @Test
  139. public void fail_if_issue_does_not_exist() {
  140. userSession.logIn();
  141. expectedException.expect(NotFoundException.class);
  142. call("UNKNOWN", "confirm");
  143. }
  144. @Test
  145. public void fail_if_no_issue_param() {
  146. userSession.logIn();
  147. expectedException.expect(IllegalArgumentException.class);
  148. call(null, "confirm");
  149. }
  150. @Test
  151. public void fail_if_no_transition_param() {
  152. ComponentDto project = db.components().insertPrivateProject();
  153. ComponentDto file = db.components().insertComponent(newFileDto(project));
  154. RuleDefinitionDto rule = db.rules().insertIssueRule();
  155. IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  156. userSession.logIn().addProjectPermission(USER, project, file);
  157. expectedException.expect(IllegalArgumentException.class);
  158. call(issue.getKey(), null);
  159. }
  160. @Test
  161. public void fail_if_not_enough_permission_to_access_issue() {
  162. ComponentDto project = db.components().insertPrivateProject();
  163. ComponentDto file = db.components().insertComponent(newFileDto(project));
  164. RuleDefinitionDto rule = db.rules().insertIssueRule();
  165. IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  166. userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
  167. expectedException.expect(ForbiddenException.class);
  168. call(issue.getKey(), "confirm");
  169. }
  170. @Test
  171. public void fail_if_not_enough_permission_to_apply_transition() {
  172. ComponentDto project = db.components().insertPrivateProject();
  173. ComponentDto file = db.components().insertComponent(newFileDto(project));
  174. RuleDefinitionDto rule = db.rules().insertIssueRule();
  175. IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
  176. userSession.logIn().addProjectPermission(USER, project, file);
  177. // False-positive transition is requiring issue admin permission
  178. expectedException.expect(ForbiddenException.class);
  179. call(issue.getKey(), "falsepositive");
  180. }
  181. @Test
  182. public void fail_if_not_authenticated() {
  183. expectedException.expect(UnauthorizedException.class);
  184. call("ISSUE_KEY", "confirm");
  185. }
  186. private TestResponse call(@Nullable String issueKey, @Nullable String transition) {
  187. TestRequest request = tester.newRequest();
  188. if (issueKey != null) {
  189. request.setParam("issue", issueKey);
  190. }
  191. if (transition != null) {
  192. request.setParam("transition", transition);
  193. }
  194. return request.execute();
  195. }
  196. private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
  197. SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
  198. assertThat(preloadedSearchResponseData.getIssues())
  199. .extracting(IssueDto::getKey)
  200. .containsOnly(issue.getKey());
  201. assertThat(preloadedSearchResponseData.getRules())
  202. .extracting(RuleDefinitionDto::getKey)
  203. .containsOnly(issue.getRuleKey());
  204. assertThat(preloadedSearchResponseData.getComponents())
  205. .extracting(ComponentDto::uuid)
  206. .containsOnly(issue.getComponentUuid(), issue.getProjectUuid());
  207. }
  208. }