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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 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().insert();
  83. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin(leia));
  84. db.issues().insert(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().insert();
  97. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin(leia));
  98. db.issues().insert(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().insert();
  118. db.issues().insert(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  119. db.issues().insert(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().insert();
  146. db.issues().insert(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  147. db.issues().insert(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().insert();
  177. db.issues().insert(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().insert();
  195. db.issues().insert(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().insert();
  213. db.issues().insert(rule, project1, project1, issue -> issue.setAuthorLogin(leia));
  214. db.issues().insert(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().insert();
  230. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin(han));
  231. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin(leia));
  232. db.issues().insert(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().insert();
  247. ComponentDto project = db.components().insertPrivateProject();
  248. UserDto user = db.users().insertUser();
  249. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin(leia));
  250. db.issues().insert(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 fail_when_user_is_not_logged() {
  261. userSession.anonymous();
  262. expectedException.expect(UnauthorizedException.class);
  263. ws.newRequest().execute();
  264. }
  265. @Test
  266. public void fail_when_user_is_not_member_of_the_organization() {
  267. OrganizationDto organization = db.organizations().insert();
  268. OrganizationDto otherOrganization = db.organizations().insert();
  269. userSession.logIn().addMembership(otherOrganization);
  270. expectedException.expect(ForbiddenException.class);
  271. ws.newRequest()
  272. .setParam("organization", organization.getKey())
  273. .execute();
  274. }
  275. @Test
  276. public void fail_when_organization_does_not_exist() {
  277. userSession.logIn();
  278. expectedException.expect(NotFoundException.class);
  279. expectedException.expectMessage("No organization with key 'unknown'");
  280. ws.newRequest()
  281. .setParam("organization", "unknown")
  282. .execute();
  283. }
  284. @Test
  285. public void fail_when_project_does_not_belong_to_organization() {
  286. OrganizationDto organization = db.organizations().insert();
  287. OrganizationDto otherOrganization = db.organizations().insert();
  288. userSession.logIn()
  289. .addMembership(organization)
  290. .addMembership(otherOrganization);
  291. ComponentDto project = db.components().insertPrivateProject(otherOrganization);
  292. permissionIndexer.allowOnlyAnyone(project);
  293. expectedException.expect(IllegalArgumentException.class);
  294. expectedException.expectMessage(format("Project '%s' is not part of the organization '%s'", project.getKey(), organization.getKey()));
  295. ws.newRequest()
  296. .setParam("organization", organization.getKey())
  297. .setParam("project", project.getKey())
  298. .execute();
  299. }
  300. @Test
  301. public void fail_when_project_is_not_a_project() {
  302. OrganizationDto organization = db.organizations().insert();
  303. userSession.logIn().addMembership(organization);
  304. ComponentDto project = db.components().insertPrivateProject(organization);
  305. ComponentDto file = db.components().insertComponent(newFileDto(project));
  306. permissionIndexer.allowOnlyAnyone(project);
  307. expectedException.expect(IllegalArgumentException.class);
  308. expectedException.expectMessage(format("Component '%s' must be a project", file.getKey()));
  309. ws.newRequest()
  310. .setParam("organization", organization.getKey())
  311. .setParam("project", file.getKey())
  312. .execute();
  313. }
  314. @Test
  315. public void fail_when_project_does_not_exist() {
  316. OrganizationDto organization = db.organizations().insert();
  317. userSession.logIn().addMembership(organization);
  318. expectedException.expect(NotFoundException.class);
  319. expectedException.expectMessage("Component key 'unknown' not found");
  320. ws.newRequest()
  321. .setParam("organization", organization.getKey())
  322. .setParam("project", "unknown")
  323. .execute();
  324. }
  325. @Test
  326. public void json_example() {
  327. ComponentDto project = db.components().insertPrivateProject();
  328. permissionIndexer.allowOnlyAnyone(project);
  329. RuleDefinitionDto rule = db.rules().insert();
  330. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin("luke.skywalker"));
  331. db.issues().insert(rule, project, project, issue -> issue.setAuthorLogin("leia.organa"));
  332. issueIndexer.indexOnStartup(emptySet());
  333. userSession.logIn().addMembership(db.getDefaultOrganization());
  334. String result = ws.newRequest().execute().getInput();
  335. assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
  336. }
  337. @Test
  338. public void definition() {
  339. WebService.Action definition = ws.getDef();
  340. assertThat(definition.key()).isEqualTo("authors");
  341. assertThat(definition.since()).isEqualTo("5.1");
  342. assertThat(definition.isPost()).isFalse();
  343. assertThat(definition.isInternal()).isFalse();
  344. assertThat(definition.responseExampleAsString()).isNotEmpty();
  345. assertThat(definition.params())
  346. .extracting(Param::key, Param::isRequired, Param::isInternal)
  347. .containsExactlyInAnyOrder(
  348. tuple("q", false, false),
  349. tuple("ps", false, false),
  350. tuple("organization", false, true),
  351. tuple("project", false, false));
  352. assertThat(definition.param("ps"))
  353. .extracting(Param::defaultValue, Param::maximumValue)
  354. .containsExactly("10", 100);
  355. }
  356. }