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.

DeleteCommentActionTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.mockito.ArgumentCaptor;
  26. import org.sonar.api.rules.RuleType;
  27. import org.sonar.api.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.issue.IssueChangeDto;
  33. import org.sonar.db.issue.IssueDbTester;
  34. import org.sonar.db.issue.IssueDto;
  35. import org.sonar.db.user.UserDto;
  36. import org.sonar.server.exceptions.ForbiddenException;
  37. import org.sonar.server.exceptions.NotFoundException;
  38. import org.sonar.server.exceptions.UnauthorizedException;
  39. import org.sonar.server.issue.IssueFinder;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.TestResponse;
  43. import org.sonar.server.ws.WsActionTester;
  44. import static java.util.Optional.ofNullable;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  47. import static org.mockito.ArgumentMatchers.any;
  48. import static org.mockito.ArgumentMatchers.eq;
  49. import static org.mockito.Mockito.mock;
  50. import static org.mockito.Mockito.verify;
  51. import static org.sonar.api.web.UserRole.CODEVIEWER;
  52. import static org.sonar.api.web.UserRole.USER;
  53. public class DeleteCommentActionTest {
  54. @Rule
  55. public ExpectedException expectedException = ExpectedException.none();
  56. @Rule
  57. public DbTester dbTester = DbTester.create();
  58. @Rule
  59. public UserSessionRule userSession = UserSessionRule.standalone();
  60. private DbClient dbClient = dbTester.getDbClient();
  61. private IssueDbTester issueDbTester = new IssueDbTester(dbTester);
  62. private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
  63. private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
  64. private WsActionTester tester = new WsActionTester(
  65. new DeleteCommentAction(userSession, dbClient, new IssueFinder(dbClient, userSession), responseWriter));
  66. @Test
  67. public void delete_comment() {
  68. IssueDto issueDto = issueDbTester.insertIssue();
  69. UserDto user = dbTester.users().insertUser();
  70. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  71. loginAndAddProjectPermission(user, issueDto, USER);
  72. call(commentDto.getKey());
  73. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  74. assertThat(dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey())).isNotPresent();
  75. verifyContentOfPreloadedSearchResponseData(issueDto);
  76. }
  77. @Test
  78. public void delete_comment_using_deprecated_key_parameter() {
  79. IssueDto issueDto = issueDbTester.insertIssue();
  80. UserDto user = dbTester.users().insertUser();
  81. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  82. loginAndAddProjectPermission(user, issueDto, USER);
  83. tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute();
  84. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  85. assertThat(dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey())).isNotPresent();
  86. verifyContentOfPreloadedSearchResponseData(issueDto);
  87. }
  88. @Test
  89. public void fail_when_comment_does_not_belong_to_current_user() {
  90. IssueDto issueDto = issueDbTester.insertIssue();
  91. UserDto user = dbTester.users().insertUser();
  92. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  93. UserDto another = dbTester.users().insertUser();
  94. loginAndAddProjectPermission(another, issueDto, USER);
  95. expectedException.expect(IllegalArgumentException.class);
  96. expectedException.expectMessage("You can only delete your own comments");
  97. call(commentDto.getKey());
  98. }
  99. @Test
  100. public void fail_when_comment_has_not_user() {
  101. IssueDto issueDto = issueDbTester.insertIssue();
  102. UserDto user = dbTester.users().insertUser();
  103. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it");
  104. loginAndAddProjectPermission(user, issueDto, USER);
  105. expectedException.expect(IllegalArgumentException.class);
  106. expectedException.expectMessage("You can only delete your own comments");
  107. call(commentDto.getKey());
  108. }
  109. @Test
  110. public void fail_when_missing_comment_key() {
  111. userSession.logIn("john");
  112. expectedException.expect(IllegalArgumentException.class);
  113. call(null);
  114. }
  115. @Test
  116. public void fail_when_comment_does_not_exist() {
  117. userSession.logIn("john");
  118. expectedException.expect(NotFoundException.class);
  119. call("ABCD");
  120. }
  121. @Test
  122. public void fail_when_not_authenticated() {
  123. expectedException.expect(UnauthorizedException.class);
  124. call("ABCD");
  125. }
  126. @Test
  127. public void fail_when_not_enough_permission() {
  128. IssueDto issueDto = issueDbTester.insertIssue();
  129. UserDto user = dbTester.users().insertUser();
  130. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  131. loginAndAddProjectPermission(user, issueDto, CODEVIEWER);
  132. expectedException.expect(ForbiddenException.class);
  133. call(commentDto.getKey());
  134. }
  135. @Test
  136. public void fail_when_hotspot() {
  137. IssueDto hotspot = issueDbTester.insertHotspot();
  138. UserDto user = dbTester.users().insertUser();
  139. IssueChangeDto commentDto = issueDbTester.insertComment(hotspot, user, "please fix it");
  140. loginAndAddProjectPermission(user, hotspot, USER);
  141. assertThatThrownBy(() -> call(commentDto.getKey()))
  142. .isInstanceOf(NotFoundException.class)
  143. .hasMessage("Issue with key '%s' does not exist", hotspot.getKey());
  144. }
  145. @Test
  146. public void test_definition() {
  147. WebService.Action action = tester.getDef();
  148. assertThat(action.key()).isEqualTo("delete_comment");
  149. assertThat(action.isPost()).isTrue();
  150. assertThat(action.isInternal()).isFalse();
  151. assertThat(action.params()).hasSize(1);
  152. assertThat(action.responseExample()).isNotNull();
  153. }
  154. private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
  155. SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
  156. assertThat(preloadedSearchResponseData.getIssues())
  157. .extracting(IssueDto::getKey)
  158. .containsOnly(issue.getKey());
  159. assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty();
  160. assertThat(preloadedSearchResponseData.getComponents()).isNullOrEmpty();
  161. }
  162. private TestResponse call(@Nullable String commentKey) {
  163. TestRequest request = tester.newRequest();
  164. ofNullable(commentKey).ifPresent(comment -> request.setParam("comment", comment));
  165. return request.execute();
  166. }
  167. private void loginAndAddProjectPermission(UserDto user, IssueDto issueDto, String permission) {
  168. userSession.logIn(user).addProjectPermission(permission, dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get());
  169. }
  170. }