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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.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.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.mockito.Mockito.when;
  52. import static org.sonar.api.web.UserRole.CODEVIEWER;
  53. import static org.sonar.api.web.UserRole.USER;
  54. public class EditCommentActionTest {
  55. private static final long NOW = 10_000_000_000L;
  56. @Rule
  57. public ExpectedException expectedException = ExpectedException.none();
  58. @Rule
  59. public DbTester dbTester = DbTester.create();
  60. @Rule
  61. public UserSessionRule userSession = UserSessionRule.standalone();
  62. private System2 system2 = mock(System2.class);
  63. private DbClient dbClient = dbTester.getDbClient();
  64. private IssueDbTester issueDbTester = new IssueDbTester(dbTester);
  65. private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
  66. private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
  67. private WsActionTester tester = new WsActionTester(
  68. new EditCommentAction(system2, userSession, dbClient, new IssueFinder(dbClient, userSession), responseWriter));
  69. @Before
  70. public void setUp() throws Exception {
  71. when(system2.now()).thenReturn(NOW);
  72. }
  73. @Test
  74. public void edit_comment() {
  75. IssueDto issueDto = issueDbTester.insertIssue();
  76. UserDto user = dbTester.users().insertUser();
  77. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  78. loginWithBrowsePermission(user, USER, issueDto);
  79. call(commentDto.getKey(), "please have a look");
  80. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  81. verifyContentOfPreloadedSearchResponseData(issueDto);
  82. IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get();
  83. assertThat(issueComment.getChangeData()).isEqualTo("please have a look");
  84. assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW);
  85. }
  86. @Test
  87. public void edit_comment_using_deprecated_key_parameter() {
  88. IssueDto issueDto = issueDbTester.insertIssue();
  89. UserDto user = dbTester.users().insertUser();
  90. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  91. loginWithBrowsePermission(user, USER, issueDto);
  92. tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute();
  93. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  94. verifyContentOfPreloadedSearchResponseData(issueDto);
  95. IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get();
  96. assertThat(issueComment.getChangeData()).isEqualTo("please have a look");
  97. assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW);
  98. }
  99. @Test
  100. public void fail_when_comment_does_not_belong_to_current_user() {
  101. IssueDto issueDto = issueDbTester.insertIssue();
  102. UserDto user = dbTester.users().insertUser();
  103. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  104. UserDto another = dbTester.users().insertUser();
  105. loginWithBrowsePermission(another, USER, issueDto);
  106. expectedException.expect(IllegalArgumentException.class);
  107. expectedException.expectMessage("You can only edit your own comments");
  108. call(commentDto.getKey(), "please have a look");
  109. }
  110. @Test
  111. public void fail_when_comment_has_not_user() {
  112. IssueDto issueDto = issueDbTester.insertIssue();
  113. UserDto user = dbTester.users().insertUser();
  114. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it");
  115. loginWithBrowsePermission(user, USER, issueDto);
  116. expectedException.expect(IllegalArgumentException.class);
  117. expectedException.expectMessage("You can only edit your own comments");
  118. call(commentDto.getKey(), "please have a look");
  119. }
  120. @Test
  121. public void fail_when_missing_comment_key() {
  122. userSession.logIn("john");
  123. expectedException.expect(IllegalArgumentException.class);
  124. call(null, "please fix it");
  125. }
  126. @Test
  127. public void fail_when_comment_does_not_exist() {
  128. userSession.logIn("john");
  129. expectedException.expect(NotFoundException.class);
  130. call("ABCD", "please fix it");
  131. }
  132. @Test
  133. public void fail_when_missing_comment_text() {
  134. userSession.logIn("john");
  135. expectedException.expect(IllegalArgumentException.class);
  136. call("ABCD", null);
  137. }
  138. @Test
  139. public void fail_when_empty_comment_text() {
  140. IssueDto issueDto = issueDbTester.insertIssue();
  141. UserDto user = dbTester.users().insertUser();
  142. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  143. loginWithBrowsePermission(user, USER, issueDto);
  144. expectedException.expect(IllegalArgumentException.class);
  145. call(commentDto.getKey(), "");
  146. }
  147. @Test
  148. public void fail_when_not_authenticated() {
  149. expectedException.expect(UnauthorizedException.class);
  150. call("ABCD", "please fix it");
  151. }
  152. @Test
  153. public void fail_when_not_enough_permission() {
  154. IssueDto issueDto = issueDbTester.insertIssue();
  155. UserDto user = dbTester.users().insertUser();
  156. IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it");
  157. loginWithBrowsePermission(user, CODEVIEWER, issueDto);
  158. expectedException.expect(ForbiddenException.class);
  159. call(commentDto.getKey(), "please have a look");
  160. }
  161. @Test
  162. public void test_definition() {
  163. WebService.Action action = tester.getDef();
  164. assertThat(action.key()).isEqualTo("edit_comment");
  165. assertThat(action.isPost()).isTrue();
  166. assertThat(action.isInternal()).isFalse();
  167. assertThat(action.params()).hasSize(2);
  168. assertThat(action.responseExample()).isNotNull();
  169. }
  170. private TestResponse call(@Nullable String commentKey, @Nullable String commentText) {
  171. TestRequest request = tester.newRequest();
  172. ofNullable(commentKey).ifPresent(comment1 -> request.setParam("comment", comment1));
  173. ofNullable(commentText).ifPresent(comment -> request.setParam("text", comment));
  174. return request.execute();
  175. }
  176. private void loginWithBrowsePermission(UserDto user, String permission, IssueDto issueDto) {
  177. userSession.logIn(user).addProjectPermission(permission,
  178. dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get(),
  179. dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getComponentUuid()).get());
  180. }
  181. private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
  182. SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
  183. assertThat(preloadedSearchResponseData.getIssues())
  184. .extracting(IssueDto::getKey)
  185. .containsOnly(issue.getKey());
  186. assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty();
  187. assertThat(preloadedSearchResponseData.getComponents()).isNullOrEmpty();
  188. }
  189. }