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.

AuthorsActionTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.server.ws.WebService.Param;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.component.ResourceTypesRule;
  30. import org.sonar.db.organization.OrganizationDto;
  31. import org.sonar.db.rule.RuleDefinitionDto;
  32. import org.sonar.db.user.UserDto;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.es.EsTester;
  35. import org.sonar.server.exceptions.ForbiddenException;
  36. import org.sonar.server.exceptions.NotFoundException;
  37. import org.sonar.server.exceptions.UnauthorizedException;
  38. import org.sonar.server.issue.index.IssueIndex;
  39. import org.sonar.server.issue.index.IssueIndexer;
  40. import org.sonar.server.issue.index.IssueIteratorFactory;
  41. import org.sonar.server.organization.DefaultOrganizationProvider;
  42. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  43. import org.sonar.server.permission.index.PermissionIndexerTester;
  44. import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
  45. import org.sonar.server.tester.UserSessionRule;
  46. import org.sonar.server.view.index.ViewIndexer;
  47. import org.sonar.server.ws.WsActionTester;
  48. import org.sonarqube.ws.Issues.AuthorsResponse;
  49. import static java.lang.String.format;
  50. import static java.util.Collections.emptySet;
  51. import static org.assertj.core.api.Assertions.assertThat;
  52. import static org.assertj.core.groups.Tuple.tuple;
  53. import static org.sonar.api.resources.Qualifiers.PROJECT;
  54. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  55. import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
  56. import static org.sonar.db.component.ComponentTesting.newFileDto;
  57. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  58. import static org.sonar.test.JsonAssert.assertJson;
  59. public class AuthorsActionTest {
  60. @Rule
  61. public DbTester db = DbTester.create();
  62. @Rule
  63. public EsTester es = EsTester.create();
  64. @Rule
  65. public UserSessionRule userSession = UserSessionRule.standalone();
  66. @Rule
  67. public ExpectedException expectedException = ExpectedException.none();
  68. private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new WebAuthorizationTypeSupport(userSession));
  69. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), db.getDbClient(), new IssueIteratorFactory(db.getDbClient()));
  70. private PermissionIndexerTester permissionIndexer = new PermissionIndexerTester(es, issueIndexer);
  71. private ViewIndexer viewIndexer = new ViewIndexer(db.getDbClient(), es.client());
  72. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  73. private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT);
  74. private WsActionTester ws = new WsActionTester(new AuthorsAction(userSession, db.getDbClient(), issueIndex,
  75. new ComponentFinder(db.getDbClient(), resourceTypes), defaultOrganizationProvider));
  76. @Test
  77. public void search_authors() {
  78. String leia = "leia.organa";
  79. String luke = "luke.skywalker";
  80. ComponentDto project = db.components().insertPrivateProject();
  81. permissionIndexer.allowOnlyAnyone(project);
  82. RuleDefinitionDto rule = db.rules().insertIssueRule();
  83. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  84. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
  85. issueIndexer.indexOnStartup(emptySet());
  86. userSession.logIn().addMembership(db.getDefaultOrganization());
  87. AuthorsResponse result = ws.newRequest().executeProtobuf(AuthorsResponse.class);
  88. assertThat(result.getAuthorsList()).containsExactlyInAnyOrder(leia, luke);
  89. }
  90. @Test
  91. public void search_authors_by_query() {
  92. String leia = "leia.organa";
  93. String luke = "luke.skywalker";
  94. ComponentDto project = db.components().insertPrivateProject();
  95. permissionIndexer.allowOnlyAnyone(project);
  96. RuleDefinitionDto rule = db.rules().insertIssueRule();
  97. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  98. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
  99. issueIndexer.indexOnStartup(emptySet());
  100. userSession.logIn().addMembership(db.getDefaultOrganization());
  101. AuthorsResponse result = ws.newRequest()
  102. .setParam(TEXT_QUERY, "leia")
  103. .executeProtobuf(AuthorsResponse.class);
  104. assertThat(result.getAuthorsList())
  105. .containsExactlyInAnyOrder(leia)
  106. .doesNotContain(luke);
  107. }
  108. @Test
  109. public void search_authors_by_organization() {
  110. String leia = "leia.organa";
  111. String luke = "luke.skywalker";
  112. OrganizationDto organization1 = db.organizations().insert();
  113. OrganizationDto organization2 = db.organizations().insert();
  114. ComponentDto project1 = db.components().insertPrivateProject(organization1);
  115. ComponentDto project2 = db.components().insertPrivateProject(organization2);
  116. permissionIndexer.allowOnlyAnyone(project1, project2);
  117. RuleDefinitionDto rule = db.rules().insertIssueRule();
  118. db.issues().insertIssue(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  119. db.issues().insertIssue(rule, project2, project2, issue -> issue.setAuthorLogin(luke));
  120. issueIndexer.indexOnStartup(emptySet());
  121. userSession.logIn().addMembership(organization1);
  122. assertThat(ws.newRequest()
  123. .setParam("organization", organization1.getKey())
  124. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  125. .containsExactlyInAnyOrder(leia);
  126. assertThat(ws.newRequest()
  127. .setParam("organization", organization1.getKey())
  128. .setParam(TEXT_QUERY, "eia")
  129. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  130. .containsExactlyInAnyOrder(leia);
  131. assertThat(ws.newRequest()
  132. .setParam("organization", organization1.getKey())
  133. .setParam(TEXT_QUERY, "luke")
  134. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  135. .isEmpty();
  136. }
  137. @Test
  138. public void search_authors_by_project() {
  139. String leia = "leia.organa";
  140. String luke = "luke.skywalker";
  141. OrganizationDto organization = db.organizations().insert();
  142. ComponentDto project1 = db.components().insertPrivateProject(organization);
  143. ComponentDto project2 = db.components().insertPrivateProject(organization);
  144. permissionIndexer.allowOnlyAnyone(project1, project2);
  145. RuleDefinitionDto rule = db.rules().insertIssueRule();
  146. db.issues().insertIssue(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  147. db.issues().insertIssue(rule, project2, project2, issue -> issue.setAuthorLogin(luke));
  148. issueIndexer.indexOnStartup(emptySet());
  149. userSession.logIn().addMembership(organization);
  150. assertThat(ws.newRequest()
  151. .setParam("organization", organization.getKey())
  152. .setParam("project", project1.getKey())
  153. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  154. .containsExactlyInAnyOrder(leia);
  155. assertThat(ws.newRequest()
  156. .setParam("organization", organization.getKey())
  157. .setParam("project", project1.getKey())
  158. .setParam(TEXT_QUERY, "eia")
  159. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  160. .containsExactlyInAnyOrder(leia);
  161. assertThat(ws.newRequest()
  162. .setParam("organization", organization.getKey())
  163. .setParam("project", project1.getKey())
  164. .setParam(TEXT_QUERY, "luke")
  165. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  166. .isEmpty();
  167. }
  168. @Test
  169. public void search_authors_by_portfolio() {
  170. String leia = "leia.organa";
  171. OrganizationDto organization = db.getDefaultOrganization();
  172. ComponentDto portfolio = db.components().insertPrivatePortfolio(organization);
  173. ComponentDto project = db.components().insertPrivateProject(organization);
  174. db.components().insertComponent(newProjectCopy(project, portfolio));
  175. permissionIndexer.allowOnlyAnyone(project);
  176. RuleDefinitionDto rule = db.rules().insertIssueRule();
  177. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  178. issueIndexer.indexOnStartup(emptySet());
  179. viewIndexer.indexOnStartup(emptySet());
  180. userSession.logIn().addMembership(organization);
  181. assertThat(ws.newRequest()
  182. .setParam("project", portfolio.getKey())
  183. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  184. .containsExactlyInAnyOrder(leia);
  185. }
  186. @Test
  187. public void search_authors_by_application() {
  188. String leia = "leia.organa";
  189. OrganizationDto defaultOrganization = db.getDefaultOrganization();
  190. ComponentDto application = db.components().insertPrivateApplication(defaultOrganization);
  191. ComponentDto project = db.components().insertPrivateProject(defaultOrganization);
  192. db.components().insertComponent(newProjectCopy(project, application));
  193. permissionIndexer.allowOnlyAnyone(project);
  194. RuleDefinitionDto rule = db.rules().insertIssueRule();
  195. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  196. issueIndexer.indexOnStartup(emptySet());
  197. viewIndexer.indexOnStartup(emptySet());
  198. userSession.logIn().addMembership(defaultOrganization);
  199. assertThat(ws.newRequest()
  200. .setParam("project", application.getKey())
  201. .executeProtobuf(AuthorsResponse.class).getAuthorsList())
  202. .containsExactlyInAnyOrder(leia);
  203. }
  204. @Test
  205. public void default_organization_is_used_when_no_organization_parameter_set() {
  206. String leia = "leia.organa";
  207. String luke = "luke.skywalker";
  208. ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization());
  209. OrganizationDto otherOrganization = db.organizations().insert();
  210. ComponentDto project2 = db.components().insertPrivateProject(otherOrganization);
  211. permissionIndexer.allowOnlyAnyone(project1, project2);
  212. RuleDefinitionDto rule = db.rules().insertIssueRule();
  213. db.issues().insertIssue(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  214. db.issues().insertIssue(rule, project2, project2, issue -> issue.setAuthorLogin(luke));
  215. issueIndexer.indexOnStartup(emptySet());
  216. userSession.logIn().addMembership(db.getDefaultOrganization());
  217. AuthorsResponse result = ws.newRequest().executeProtobuf(AuthorsResponse.class);
  218. assertThat(result.getAuthorsList())
  219. .containsExactlyInAnyOrder(leia)
  220. .doesNotContain(luke);
  221. }
  222. @Test
  223. public void set_page_size() {
  224. String han = "han.solo";
  225. String leia = "leia.organa";
  226. String luke = "luke.skywalker";
  227. ComponentDto project = db.components().insertPrivateProject();
  228. permissionIndexer.allowOnlyAnyone(project);
  229. RuleDefinitionDto rule = db.rules().insertIssueRule();
  230. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(han));
  231. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  232. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
  233. issueIndexer.indexOnStartup(emptySet());
  234. userSession.logIn().addMembership(db.getDefaultOrganization());
  235. AuthorsResponse result = ws.newRequest()
  236. .setParam(PAGE_SIZE, "2")
  237. .executeProtobuf(AuthorsResponse.class);
  238. assertThat(result.getAuthorsList())
  239. .containsExactlyInAnyOrder(han, leia)
  240. .doesNotContain(luke);
  241. }
  242. @Test
  243. public void return_only_authors_from_issues_visible_by_current_user() {
  244. String leia = "leia.organa";
  245. String luke = "luke.skywalker";
  246. RuleDefinitionDto rule = db.rules().insertIssueRule();
  247. ComponentDto project = db.components().insertPrivateProject();
  248. UserDto user = db.users().insertUser();
  249. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(leia));
  250. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin(luke));
  251. issueIndexer.indexOnStartup(emptySet());
  252. userSession.logIn(user).addMembership(db.getDefaultOrganization());
  253. // User has no permission on project
  254. assertThat(ws.newRequest().executeProtobuf(AuthorsResponse.class).getAuthorsList()).isEmpty();
  255. // User has no browse permission on project
  256. permissionIndexer.allowOnlyUser(project, user);
  257. assertThat(ws.newRequest().executeProtobuf(AuthorsResponse.class).getAuthorsList()).isNotEmpty();
  258. }
  259. @Test
  260. public void should_ignore_authors_of_hotspot() {
  261. String luke = "luke.skywalker";
  262. ComponentDto project = db.components().insertPrivateProject();
  263. permissionIndexer.allowOnlyAnyone(project);
  264. db.issues().insertHotspot(project, project, issue -> issue
  265. .setAuthorLogin(luke));
  266. issueIndexer.indexOnStartup(emptySet());
  267. userSession.logIn().addMembership(db.getDefaultOrganization());
  268. AuthorsResponse result = ws.newRequest().executeProtobuf(AuthorsResponse.class);
  269. assertThat(result.getAuthorsList()).isEmpty();
  270. }
  271. @Test
  272. public void fail_when_user_is_not_logged() {
  273. userSession.anonymous();
  274. expectedException.expect(UnauthorizedException.class);
  275. ws.newRequest().execute();
  276. }
  277. @Test
  278. public void fail_when_user_is_not_member_of_the_organization() {
  279. OrganizationDto organization = db.organizations().insert();
  280. OrganizationDto otherOrganization = db.organizations().insert();
  281. userSession.logIn().addMembership(otherOrganization);
  282. expectedException.expect(ForbiddenException.class);
  283. ws.newRequest()
  284. .setParam("organization", organization.getKey())
  285. .execute();
  286. }
  287. @Test
  288. public void fail_when_organization_does_not_exist() {
  289. userSession.logIn();
  290. expectedException.expect(NotFoundException.class);
  291. expectedException.expectMessage("No organization with key 'unknown'");
  292. ws.newRequest()
  293. .setParam("organization", "unknown")
  294. .execute();
  295. }
  296. @Test
  297. public void fail_when_project_does_not_belong_to_organization() {
  298. OrganizationDto organization = db.organizations().insert();
  299. OrganizationDto otherOrganization = db.organizations().insert();
  300. userSession.logIn()
  301. .addMembership(organization)
  302. .addMembership(otherOrganization);
  303. ComponentDto project = db.components().insertPrivateProject(otherOrganization);
  304. permissionIndexer.allowOnlyAnyone(project);
  305. expectedException.expect(IllegalArgumentException.class);
  306. expectedException.expectMessage(format("Project '%s' is not part of the organization '%s'", project.getKey(), organization.getKey()));
  307. ws.newRequest()
  308. .setParam("organization", organization.getKey())
  309. .setParam("project", project.getKey())
  310. .execute();
  311. }
  312. @Test
  313. public void fail_when_project_is_not_a_project() {
  314. OrganizationDto organization = db.organizations().insert();
  315. userSession.logIn().addMembership(organization);
  316. ComponentDto project = db.components().insertPrivateProject(organization);
  317. ComponentDto file = db.components().insertComponent(newFileDto(project));
  318. permissionIndexer.allowOnlyAnyone(project);
  319. expectedException.expect(IllegalArgumentException.class);
  320. expectedException.expectMessage(format("Component '%s' must be a project", file.getKey()));
  321. ws.newRequest()
  322. .setParam("organization", organization.getKey())
  323. .setParam("project", file.getKey())
  324. .execute();
  325. }
  326. @Test
  327. public void fail_when_project_does_not_exist() {
  328. OrganizationDto organization = db.organizations().insert();
  329. userSession.logIn().addMembership(organization);
  330. expectedException.expect(NotFoundException.class);
  331. expectedException.expectMessage("Component key 'unknown' not found");
  332. ws.newRequest()
  333. .setParam("organization", organization.getKey())
  334. .setParam("project", "unknown")
  335. .execute();
  336. }
  337. @Test
  338. public void json_example() {
  339. ComponentDto project = db.components().insertPrivateProject();
  340. permissionIndexer.allowOnlyAnyone(project);
  341. RuleDefinitionDto rule = db.rules().insertIssueRule();
  342. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("luke.skywalker"));
  343. db.issues().insertIssue(rule, project, project, issue -> issue.setAuthorLogin("leia.organa"));
  344. issueIndexer.indexOnStartup(emptySet());
  345. userSession.logIn().addMembership(db.getDefaultOrganization());
  346. String result = ws.newRequest().execute().getInput();
  347. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  348. }
  349. @Test
  350. public void definition() {
  351. WebService.Action definition = ws.getDef();
  352. assertThat(definition.key()).isEqualTo("authors");
  353. assertThat(definition.since()).isEqualTo("5.1");
  354. assertThat(definition.isPost()).isFalse();
  355. assertThat(definition.isInternal()).isFalse();
  356. assertThat(definition.responseExampleAsString()).isNotEmpty();
  357. assertThat(definition.params())
  358. .extracting(Param::key, Param::isRequired, Param::isInternal)
  359. .containsExactlyInAnyOrder(
  360. tuple("q", false, false),
  361. tuple("ps", false, false),
  362. tuple("organization", false, true),
  363. tuple("project", false, false));
  364. assertThat(definition.param("ps"))
  365. .extracting(Param::defaultValue, Param::maximumValue)
  366. .containsExactly("10", 100);
  367. }
  368. }