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.

AssignActionTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.hotspot.ws;
  21. import com.google.common.collect.Sets;
  22. import com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.EnumSet;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.Nullable;
  30. import org.assertj.core.api.Condition;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.ArgumentCaptor;
  35. import org.sonar.api.rules.RuleType;
  36. import org.sonar.api.server.ws.WebService;
  37. import org.sonar.api.utils.System2;
  38. import org.sonar.api.web.UserRole;
  39. import org.sonar.core.issue.DefaultIssue;
  40. import org.sonar.core.issue.IssueChangeContext;
  41. import org.sonar.db.DbClient;
  42. import org.sonar.db.DbSession;
  43. import org.sonar.db.DbTester;
  44. import org.sonar.db.component.ComponentDto;
  45. import org.sonar.db.issue.IssueDto;
  46. import org.sonar.db.rule.RuleDefinitionDto;
  47. import org.sonar.db.rule.RuleTesting;
  48. import org.sonar.db.user.UserDto;
  49. import org.sonar.server.exceptions.ForbiddenException;
  50. import org.sonar.server.exceptions.NotFoundException;
  51. import org.sonar.server.issue.IssueFieldsSetter;
  52. import org.sonar.server.issue.ws.IssueUpdater;
  53. import org.sonar.server.tester.UserSessionRule;
  54. import org.sonar.server.ws.TestRequest;
  55. import org.sonar.server.ws.WsActionTester;
  56. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  57. import static org.assertj.core.api.Assertions.assertThat;
  58. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  59. import static org.mockito.ArgumentMatchers.any;
  60. import static org.mockito.ArgumentMatchers.argThat;
  61. import static org.mockito.ArgumentMatchers.eq;
  62. import static org.mockito.Mockito.mock;
  63. import static org.mockito.Mockito.verify;
  64. import static org.mockito.Mockito.verifyNoMoreInteractions;
  65. import static org.mockito.Mockito.when;
  66. import static org.sonar.api.issue.Issue.STATUSES;
  67. import static org.sonar.api.issue.Issue.STATUS_CLOSED;
  68. import static org.sonar.api.issue.Issue.STATUS_TO_REVIEW;
  69. import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
  70. import static org.sonar.db.component.ComponentTesting.newFileDto;
  71. @RunWith(DataProviderRunner.class)
  72. public class AssignActionTest {
  73. @Rule
  74. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  75. @Rule
  76. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  77. private DbClient dbClient = dbTester.getDbClient();
  78. private IssueUpdater issueUpdater = mock(IssueUpdater.class);
  79. private System2 system2 = mock(System2.class);
  80. private IssueFieldsSetter issueFieldsSetter = mock(IssueFieldsSetter.class);
  81. private HotspotWsSupport hotspotWsSupport = new HotspotWsSupport(dbClient, userSessionRule, system2);
  82. private AssignAction underTest = new AssignAction(dbClient, hotspotWsSupport, issueFieldsSetter, issueUpdater);
  83. private WsActionTester actionTester = new WsActionTester(underTest);
  84. @Test
  85. public void ws_definition_check() {
  86. WebService.Action wsDefinition = actionTester.getDef();
  87. assertThat(wsDefinition.isPost()).isTrue();
  88. assertThat(wsDefinition.isInternal()).isTrue();
  89. assertThat(wsDefinition.params()).hasSize(3);
  90. assertThat(wsDefinition.param("hotspot").isRequired()).isTrue();
  91. assertThat(wsDefinition.param("assignee").isRequired()).isTrue();
  92. assertThat(wsDefinition.param("comment").isRequired()).isFalse();
  93. assertThat(wsDefinition.since()).isEqualTo("8.2");
  94. }
  95. @Test
  96. public void assign_hotspot_to_someone_for_public_project() {
  97. ComponentDto project = dbTester.components().insertPublicProject();
  98. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  99. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  100. UserDto userDto = insertUser(randomAlphanumeric(10));
  101. userSessionRule.logIn(userDto).registerComponents(project);
  102. UserDto assignee = insertUser(randomAlphanumeric(15));
  103. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
  104. executeRequest(hotspot, assignee.getLogin(), null);
  105. verifyFieldSetters(assignee, null);
  106. }
  107. @Test
  108. public void assign_hotspot_to_me_for_public_project() {
  109. ComponentDto project = dbTester.components().insertPublicProject();
  110. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  111. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  112. UserDto me = insertUser(randomAlphanumeric(10));
  113. userSessionRule.logIn(me).registerComponents(project);
  114. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(me), any(IssueChangeContext.class))).thenReturn(true);
  115. executeRequest(hotspot, me.getLogin(), null);
  116. verifyFieldSetters(me, null);
  117. }
  118. @Test
  119. public void assign_hotspot_to_someone_for_private_project() {
  120. ComponentDto project = dbTester.components().insertPrivateProject();
  121. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  122. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  123. insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), hotspot, project, UserRole.USER);
  124. UserDto assignee = insertUserWithProjectUserPermission(randomAlphanumeric(15), project);
  125. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
  126. executeRequest(hotspot, assignee.getLogin(), null);
  127. verifyFieldSetters(assignee, null);
  128. }
  129. @Test
  130. public void fail_if_assignee_does_not_have_access_for_private_project() {
  131. ComponentDto project = dbTester.components().insertPrivateProject();
  132. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  133. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  134. insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), hotspot, project, UserRole.USER);
  135. UserDto assignee = insertUser(randomAlphanumeric(15));
  136. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
  137. assertThatThrownBy(() -> executeRequest(hotspot, assignee.getLogin(), null))
  138. .isInstanceOf(IllegalArgumentException.class)
  139. .hasMessage("Provided user with login '%s' does not have access to project", assignee.getLogin());
  140. }
  141. @Test
  142. public void assign_hotspot_to_me_for_private_project() {
  143. ComponentDto project = dbTester.components().insertPrivateProject();
  144. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  145. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  146. UserDto me = insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), hotspot, project, UserRole.USER);
  147. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(me), any(IssueChangeContext.class))).thenReturn(true);
  148. executeRequest(hotspot, me.getLogin(), null);
  149. verifyFieldSetters(me, null);
  150. }
  151. @Test
  152. public void assign_hotspot_with_comment() {
  153. ComponentDto project = dbTester.components().insertPublicProject();
  154. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  155. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  156. UserDto userDto = insertUser(randomAlphanumeric(10));
  157. userSessionRule.logIn(userDto).registerComponents(project);
  158. UserDto assignee = insertUser(randomAlphanumeric(15));
  159. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
  160. String comment = "some comment";
  161. executeRequest(hotspot, assignee.getLogin(), comment);
  162. verifyFieldSetters(assignee, comment);
  163. }
  164. @Test
  165. public void assign_twice_same_user_to_hotspot_does_not_reload() {
  166. ComponentDto project = dbTester.components().insertPublicProject();
  167. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  168. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  169. UserDto userDto = insertUser(randomAlphanumeric(10));
  170. userSessionRule.logIn(userDto).registerComponents(project);
  171. UserDto assignee = insertUser(randomAlphanumeric(15));
  172. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(false);
  173. executeRequest(hotspot, assignee.getLogin(), "some comment");
  174. verify(issueFieldsSetter).assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class));
  175. verifyNoMoreInteractions(issueUpdater);
  176. }
  177. @Test
  178. public void fail_if_assigning_to_not_existing_user() {
  179. ComponentDto project = dbTester.components().insertPublicProject();
  180. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  181. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  182. UserDto userDto = insertUser(randomAlphanumeric(10));
  183. userSessionRule.logIn(userDto).registerComponents(project);
  184. String notExistingUserLogin = randomAlphanumeric(10);
  185. assertThatThrownBy(() -> executeRequest(hotspot, notExistingUserLogin, null))
  186. .isInstanceOf(NotFoundException.class)
  187. .hasMessage("Unknown user: " + notExistingUserLogin);
  188. }
  189. @Test
  190. @UseDataProvider("allIssueStatusesExceptToReviewAndClosed")
  191. public void fail_if_assign_user_to_hotspot_for_OTHER_STATUSES_for_public_project(String status) {
  192. ComponentDto project = dbTester.components().insertPublicProject();
  193. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  194. IssueDto hotspot = dbTester.issues().insertHotspot(project, file, h -> h.setStatus(status));
  195. UserDto userDto = insertUser(randomAlphanumeric(10));
  196. userSessionRule.logIn(userDto).registerComponents(project);
  197. assertThatThrownBy(() -> executeRequest(hotspot, userSessionRule.getLogin(), null))
  198. .isInstanceOf(IllegalArgumentException.class)
  199. .hasMessage("Assignee can only be changed on Security Hotspots with status 'TO_REVIEW'");
  200. }
  201. @Test
  202. @UseDataProvider("allIssueStatusesExceptToReviewAndClosed")
  203. public void fail_if_assign_user_to_hotspot_for_OTHER_STATUSES_for_private_project(String status) {
  204. ComponentDto project = dbTester.components().insertPrivateProject();
  205. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  206. IssueDto hotspot = dbTester.issues().insertHotspot(project, file, h -> h.setStatus(status));
  207. UserDto userDto = insertUser(randomAlphanumeric(10));
  208. userSessionRule.logIn(userDto).registerComponents(project);
  209. assertThatThrownBy(() -> executeRequest(hotspot, userSessionRule.getLogin(), null))
  210. .isInstanceOf(IllegalArgumentException.class)
  211. .hasMessage("Assignee can only be changed on Security Hotspots with status 'TO_REVIEW'");
  212. }
  213. @DataProvider
  214. public static Object[][] allIssueStatusesExceptToReviewAndClosed() {
  215. return STATUSES.stream()
  216. .filter(status -> !STATUS_TO_REVIEW.equals(status))
  217. .filter(status -> !STATUS_CLOSED.equals(status))
  218. .map(status -> new Object[] {status})
  219. .toArray(Object[][]::new);
  220. }
  221. @Test
  222. public void fail_if_not_authenticated() {
  223. ComponentDto project = dbTester.components().insertPublicProject();
  224. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  225. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  226. userSessionRule.anonymous();
  227. UserDto assignee = insertUser(randomAlphanumeric(15));
  228. assertThatThrownBy(() -> executeRequest(hotspot, assignee.getLogin(), null))
  229. .isInstanceOf(ForbiddenException.class)
  230. .hasMessage("Insufficient privileges");
  231. }
  232. @Test
  233. public void fail_if_missing_browse_permission() {
  234. ComponentDto project = dbTester.components().insertPrivateProject();
  235. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  236. IssueDto hotspot = dbTester.issues().insertHotspot(project, file);
  237. UserDto me = insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), hotspot, project, UserRole.CODEVIEWER);
  238. when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(me), any(IssueChangeContext.class))).thenReturn(true);
  239. assertThatThrownBy(() -> executeRequest(hotspot, me.getLogin(), null))
  240. .isInstanceOf(ForbiddenException.class)
  241. .hasMessage("Insufficient privileges");
  242. }
  243. @Test
  244. public void fail_if_hotspot_does_not_exist() {
  245. ComponentDto project = dbTester.components().insertPublicProject();
  246. UserDto me = insertUser(randomAlphanumeric(10));
  247. userSessionRule.logIn().registerComponents(project);
  248. String notExistingHotspotKey = randomAlphanumeric(10);
  249. assertThatThrownBy(() -> executeRequest(notExistingHotspotKey, me.getLogin(), null))
  250. .isInstanceOf(NotFoundException.class)
  251. .hasMessage("Hotspot '%s' does not exist", notExistingHotspotKey);
  252. }
  253. @Test
  254. @UseDataProvider("allRuleTypesWithStatusesExceptHotspot")
  255. public void fail_if_trying_to_assign_issue(RuleType ruleType, String status) {
  256. ComponentDto project = dbTester.components().insertPublicProject();
  257. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  258. RuleDefinitionDto rule = newRule(ruleType);
  259. IssueDto issue = dbTester.issues().insertIssue(rule, project, file, i -> i
  260. .setStatus(status)
  261. .setType(ruleType));
  262. UserDto me = insertUser(randomAlphanumeric(10));
  263. userSessionRule.logIn().registerComponents(project);
  264. assertThatThrownBy(() -> executeRequest(issue, me.getLogin(), null))
  265. .isInstanceOf(NotFoundException.class)
  266. .hasMessage("Hotspot '%s' does not exist", issue.getKey());
  267. }
  268. @DataProvider
  269. public static Object[][] allRuleTypesWithStatusesExceptHotspot() {
  270. Set<RuleType> ruleTypes = EnumSet.allOf(RuleType.class)
  271. .stream()
  272. .filter(ruleType -> SECURITY_HOTSPOT != ruleType)
  273. .collect(Collectors.toSet());
  274. Set<String> statuses = STATUSES
  275. .stream()
  276. .filter(status -> !STATUS_TO_REVIEW.equals(status))
  277. .collect(Collectors.toSet());
  278. return Sets.cartesianProduct(ruleTypes, statuses)
  279. .stream()
  280. .map(elements -> new Object[] {elements.get(0), elements.get(1)})
  281. .toArray(Object[][]::new);
  282. }
  283. @Test
  284. public void fail_with_NotFoundException_if_hotspot_is_closed() {
  285. ComponentDto project = dbTester.components().insertPublicProject();
  286. ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
  287. RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
  288. IssueDto issue = dbTester.issues().insertHotspot(rule, project, file, t -> t.setStatus(STATUS_CLOSED));
  289. UserDto me = insertUser(randomAlphanumeric(10));
  290. userSessionRule.logIn().registerComponents(project);
  291. assertThatThrownBy(() -> executeRequest(issue, me.getLogin(), null))
  292. .isInstanceOf(NotFoundException.class)
  293. .hasMessage("Hotspot '%s' does not exist", issue.getKey());
  294. }
  295. private void verifyFieldSetters(UserDto assignee, @Nullable String comment) {
  296. ArgumentCaptor<DefaultIssue> defaultIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
  297. short capturedArgsCount = 0;
  298. if (comment != null) {
  299. verify(issueFieldsSetter).addComment(defaultIssueCaptor.capture(), eq(comment), any(IssueChangeContext.class));
  300. capturedArgsCount++;
  301. }
  302. verify(issueFieldsSetter).assign(defaultIssueCaptor.capture(), userMatcher(assignee), any(IssueChangeContext.class));
  303. verify(issueUpdater).saveIssueAndPreloadSearchResponseData(
  304. any(DbSession.class),
  305. defaultIssueCaptor.capture(),
  306. any(IssueChangeContext.class),
  307. eq(false));
  308. capturedArgsCount += 2;
  309. // because it is mutated by FieldSetter and IssueUpdater, the same object must be passed to all methods
  310. List<DefaultIssue> capturedDefaultIssues = defaultIssueCaptor.getAllValues();
  311. assertThat(capturedDefaultIssues).hasSize(capturedArgsCount);
  312. assertThat(capturedDefaultIssues)
  313. .are(new Condition<DefaultIssue>() {
  314. @Override
  315. public boolean matches(DefaultIssue value) {
  316. return value == capturedDefaultIssues.get(0);
  317. }
  318. });
  319. }
  320. private void executeRequest(IssueDto hotspot, @Nullable String assignee, @Nullable String comment) {
  321. executeRequest(hotspot.getKey(), assignee, comment);
  322. }
  323. private void executeRequest(String hotspotKey, @Nullable String assignee, @Nullable String comment) {
  324. TestRequest request = actionTester.newRequest()
  325. .setParam("hotspot", hotspotKey);
  326. if (assignee != null) {
  327. request.setParam("assignee", assignee);
  328. }
  329. if (comment != null) {
  330. request.setParam("comment", comment);
  331. }
  332. request.execute().assertNoContent();
  333. }
  334. private RuleDefinitionDto newRule(RuleType ruleType) {
  335. RuleDefinitionDto ruleDefinition = RuleTesting.newRule()
  336. .setType(ruleType);
  337. dbTester.rules().insert(ruleDefinition);
  338. return ruleDefinition;
  339. }
  340. private UserDto insertUser(String login) {
  341. UserDto user = dbTester.users().insertUser(login);
  342. dbTester.organizations().addMember(dbTester.getDefaultOrganization(), user);
  343. return user;
  344. }
  345. private UserDto insertUserWithProjectPermission(String login, ComponentDto project, String permission) {
  346. UserDto user = dbTester.users().insertUser(login);
  347. dbTester.organizations().addMember(dbTester.getDefaultOrganization(), user);
  348. dbTester.users().insertProjectPermissionOnUser(user, permission, project);
  349. return user;
  350. }
  351. private UserDto insertUserWithProjectUserPermission(String login, ComponentDto project) {
  352. return insertUserWithProjectPermission(login, project, UserRole.USER);
  353. }
  354. private UserDto insertAndLoginAsUserWithProjectUserPermission(String login, IssueDto issue, ComponentDto project, String permission) {
  355. UserDto user = insertUserWithProjectUserPermission(login, project);
  356. userSessionRule.logIn(user)
  357. .addProjectPermission(permission,
  358. dbClient.componentDao().selectByUuid(dbTester.getSession(), issue.getProjectUuid()).get(),
  359. dbClient.componentDao().selectByUuid(dbTester.getSession(), issue.getComponentUuid()).get());
  360. return user;
  361. }
  362. private static UserDto userMatcher(UserDto user) {
  363. return argThat(argument -> argument.getLogin().equals(user.getLogin()) &&
  364. argument.getUuid().equals(user.getUuid()));
  365. }
  366. }