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.

EditCommentActionTest.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbTester;
  33. import org.sonar.db.issue.IssueChangeDto;
  34. import org.sonar.db.issue.IssueDbTester;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.db.user.UserDto;
  37. import org.sonar.server.exceptions.ForbiddenException;
  38. import org.sonar.server.exceptions.NotFoundException;
  39. import org.sonar.server.exceptions.UnauthorizedException;
  40. import org.sonar.server.issue.IssueFinder;
  41. import org.sonar.server.tester.UserSessionRule;
  42. import org.sonar.server.ws.TestRequest;
  43. import org.sonar.server.ws.TestResponse;
  44. import org.sonar.server.ws.WsActionTester;
  45. import static java.util.Optional.ofNullable;
  46. import static org.assertj.core.api.Assertions.assertThat;
  47. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  48. import static org.mockito.ArgumentMatchers.any;
  49. import static org.mockito.ArgumentMatchers.eq;
  50. import static org.mockito.Mockito.mock;
  51. import static org.mockito.Mockito.verify;
  52. import static org.mockito.Mockito.when;
  53. import static org.sonar.api.web.UserRole.CODEVIEWER;
  54. import static org.sonar.api.web.UserRole.USER;
  55. public class EditCommentActionTest {
  56. private static final long NOW = 10_000_000_000L;
  57. @Rule
  58. public ExpectedException expectedException = ExpectedException.none();
  59. @Rule
  60. public DbTester dbTester = DbTester.create();
  61. @Rule
  62. public UserSessionRule userSession = UserSessionRule.standalone();
  63. private System2 system2 = mock(System2.class);
  64. private DbClient dbClient = dbTester.getDbClient();
  65. private IssueDbTester issueDbTester = new IssueDbTester(dbTester);
  66. private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
  67. private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
  68. private WsActionTester tester = new WsActionTester(
  69. new EditCommentAction(system2, userSession, dbClient, new IssueFinder(dbClient, userSession), responseWriter));
  70. @Before
  71. public void setUp() {
  72. when(system2.now()).thenReturn(NOW);
  73. }
  74. @Test
  75. public void edit_comment() {
  76. IssueDto issueDto = newIssue();
  77. UserDto user = dbTester.users().insertUser();
  78. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  79. loginWithBrowsePermission(user, USER, issueDto);
  80. call(commentDto.getKey(), "please have a look");
  81. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  82. verifyContentOfPreloadedSearchResponseData(issueDto);
  83. IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get();
  84. assertThat(issueComment.getChangeData()).isEqualTo("please have a look");
  85. assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW);
  86. }
  87. @Test
  88. public void edit_comment_using_deprecated_key_parameter() {
  89. IssueDto issueDto = newIssue();
  90. UserDto user = dbTester.users().insertUser();
  91. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  92. loginWithBrowsePermission(user, USER, issueDto);
  93. tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute();
  94. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  95. verifyContentOfPreloadedSearchResponseData(issueDto);
  96. IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get();
  97. assertThat(issueComment.getChangeData()).isEqualTo("please have a look");
  98. assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW);
  99. }
  100. @Test
  101. public void fail_when_comment_is_for_hotspot() {
  102. IssueDto hotspot = issueDbTester.insertHotspot();
  103. UserDto user = dbTester.users().insertUser();
  104. IssueChangeDto commentDto = issueDbTester.insertComment(hotspot, user, "please fix it");
  105. UserDto another = dbTester.users().insertUser();
  106. loginWithBrowsePermission(another, USER, hotspot);
  107. assertThatThrownBy(() -> call(commentDto.getKey(), "please have a look"))
  108. .isInstanceOf(NotFoundException.class)
  109. .hasMessage("Issue with key '%s' does not exist", hotspot.getKey());
  110. }
  111. @Test
  112. public void fail_when_comment_does_not_belong_to_current_user() {
  113. IssueDto issueDto = newIssue();
  114. UserDto user = dbTester.users().insertUser();
  115. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  116. UserDto another = dbTester.users().insertUser();
  117. loginWithBrowsePermission(another, USER, issueDto);
  118. expectedException.expect(IllegalArgumentException.class);
  119. expectedException.expectMessage("You can only edit your own comments");
  120. call(commentDto.getKey(), "please have a look");
  121. }
  122. @Test
  123. public void fail_when_comment_has_not_user() {
  124. IssueDto issueDto = newIssue();
  125. UserDto user = dbTester.users().insertUser();
  126. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it");
  127. loginWithBrowsePermission(user, USER, issueDto);
  128. expectedException.expect(IllegalArgumentException.class);
  129. expectedException.expectMessage("You can only edit your own comments");
  130. call(commentDto.getKey(), "please have a look");
  131. }
  132. @Test
  133. public void fail_when_missing_comment_key() {
  134. userSession.logIn("john");
  135. expectedException.expect(IllegalArgumentException.class);
  136. call(null, "please fix it");
  137. }
  138. @Test
  139. public void fail_when_comment_does_not_exist() {
  140. userSession.logIn("john");
  141. expectedException.expect(NotFoundException.class);
  142. call("ABCD", "please fix it");
  143. }
  144. @Test
  145. public void fail_when_missing_comment_text() {
  146. userSession.logIn("john");
  147. expectedException.expect(IllegalArgumentException.class);
  148. call("ABCD", null);
  149. }
  150. @Test
  151. public void fail_when_empty_comment_text() {
  152. IssueDto issueDto = newIssue();
  153. UserDto user = dbTester.users().insertUser();
  154. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  155. loginWithBrowsePermission(user, USER, issueDto);
  156. expectedException.expect(IllegalArgumentException.class);
  157. call(commentDto.getKey(), "");
  158. }
  159. @Test
  160. public void fail_when_not_authenticated() {
  161. expectedException.expect(UnauthorizedException.class);
  162. call("ABCD", "please fix it");
  163. }
  164. @Test
  165. public void fail_when_not_enough_permission() {
  166. IssueDto issueDto = newIssue();
  167. UserDto user = dbTester.users().insertUser();
  168. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  169. loginWithBrowsePermission(user, CODEVIEWER, issueDto);
  170. expectedException.expect(ForbiddenException.class);
  171. call(commentDto.getKey(), "please have a look");
  172. }
  173. @Test
  174. public void fail_NFE_if_security_hotspots() {
  175. IssueDto issueDto = issueDbTester.insertHotspot();
  176. UserDto user = dbTester.users().insertUser();
  177. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  178. loginWithBrowsePermission(user, CODEVIEWER, issueDto);
  179. expectedException.expect(NotFoundException.class);
  180. expectedException.expectMessage(String.format("Issue with key '%s' does not exist", issueDto.getKey()));
  181. call(commentDto.getKey(), "please have a look");
  182. }
  183. @Test
  184. public void test_definition() {
  185. WebService.Action action = tester.getDef();
  186. assertThat(action.key()).isEqualTo("edit_comment");
  187. assertThat(action.isPost()).isTrue();
  188. assertThat(action.isInternal()).isFalse();
  189. assertThat(action.params()).hasSize(2);
  190. assertThat(action.responseExample()).isNotNull();
  191. }
  192. private TestResponse call(@Nullable String commentKey, @Nullable String commentText) {
  193. TestRequest request = tester.newRequest();
  194. ofNullable(commentKey).ifPresent(comment1 -> request.setParam("comment", comment1));
  195. ofNullable(commentText).ifPresent(comment -> request.setParam("text", comment));
  196. return request.execute();
  197. }
  198. private IssueDto newIssue() {
  199. return issueDbTester.insertIssue();
  200. }
  201. private void loginWithBrowsePermission(UserDto user, String permission, IssueDto issueDto) {
  202. userSession.logIn(user).addProjectPermission(permission,
  203. dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get(),
  204. dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getComponentUuid()).get());
  205. }
  206. private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
  207. SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
  208. assertThat(preloadedSearchResponseData.getIssues())
  209. .extracting(IssueDto::getKey)
  210. .containsOnly(issue.getKey());
  211. assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty();
  212. assertThat(preloadedSearchResponseData.getComponents()).isNullOrEmpty();
  213. }
  214. }