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.

SearchActionTestOnSonarCloud.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 com.google.gson.JsonElement;
  22. import com.google.gson.JsonParser;
  23. import java.time.Clock;
  24. import java.util.Arrays;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.config.internal.MapSettings;
  29. import org.sonar.api.resources.Languages;
  30. import org.sonar.api.rule.RuleKey;
  31. import org.sonar.api.utils.Durations;
  32. import org.sonar.api.utils.System2;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.component.ComponentDto;
  36. import org.sonar.db.organization.OrganizationDto;
  37. import org.sonar.db.rule.RuleDefinitionDto;
  38. import org.sonar.db.user.UserDto;
  39. import org.sonar.server.es.EsTester;
  40. import org.sonar.server.issue.IssueFieldsSetter;
  41. import org.sonar.server.issue.TransitionService;
  42. import org.sonar.server.issue.index.IssueIndex;
  43. import org.sonar.server.issue.index.IssueIndexer;
  44. import org.sonar.server.issue.index.IssueIteratorFactory;
  45. import org.sonar.server.issue.index.IssueQueryFactory;
  46. import org.sonar.server.issue.workflow.FunctionExecutor;
  47. import org.sonar.server.issue.workflow.IssueWorkflow;
  48. import org.sonar.server.permission.index.PermissionIndexerTester;
  49. import org.sonar.server.permission.index.WebAuthorizationTypeSupport;
  50. import org.sonar.server.tester.UserSessionRule;
  51. import org.sonar.server.ws.WsActionTester;
  52. import org.sonar.server.ws.WsResponseCommonFormat;
  53. import org.sonar.test.JsonAssert;
  54. import static org.assertj.core.api.Assertions.assertThat;
  55. import static org.sonar.api.server.ws.WebService.Param.FACETS;
  56. import static org.sonar.db.component.ComponentTesting.newFileDto;
  57. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_KEYS;
  58. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ORGANIZATION;
  59. public class SearchActionTestOnSonarCloud {
  60. @Rule
  61. public UserSessionRule userSession = UserSessionRule.standalone();
  62. @Rule
  63. public DbTester db = DbTester.create();
  64. @Rule
  65. public EsTester es = EsTester.create();
  66. private MapSettings mapSettings = new MapSettings().setProperty("sonar.sonarcloud.enabled", true);
  67. private DbClient dbClient = db.getDbClient();
  68. private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new WebAuthorizationTypeSupport(userSession));
  69. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
  70. private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, Clock.systemUTC(), userSession);
  71. private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter();
  72. private IssueWorkflow issueWorkflow = new IssueWorkflow(new FunctionExecutor(issueFieldsSetter), issueFieldsSetter);
  73. private SearchResponseLoader searchResponseLoader = new SearchResponseLoader(userSession, dbClient, new TransitionService(userSession, issueWorkflow));
  74. private Languages languages = new Languages();
  75. private SearchResponseFormat searchResponseFormat = new SearchResponseFormat(new Durations(), new WsResponseCommonFormat(languages), languages, new AvatarResolverImpl());
  76. private PermissionIndexerTester permissionIndexer = new PermissionIndexerTester(es, issueIndexer);
  77. private SearchAction underTest = new SearchAction(userSession, issueIndex, issueQueryFactory, searchResponseLoader, searchResponseFormat,
  78. mapSettings.asConfig(), System2.INSTANCE, dbClient);
  79. private WsActionTester ws = new WsActionTester(underTest);
  80. private OrganizationDto organization;
  81. private UserDto user;
  82. private ComponentDto project;
  83. @Before
  84. public void setup() {
  85. underTest.start();
  86. organization = db.organizations().insert(o -> o.setKey("org-1"));
  87. user = db.users().insertUser();
  88. project = db.components().insertPublicProject(organization, p -> p.setDbKey("PK1"));
  89. ComponentDto file = db.components().insertComponent(newFileDto(project, null, "F1").setDbKey("FK1"));
  90. RuleDefinitionDto rule = db.rules().insert(r -> r.setRuleKey(RuleKey.of("xoo", "x1")));
  91. db.issues().insert(rule, project, file, i -> i.setAuthorLogin("leia").setKee("2bd4eac2-b650-4037-80bc-7b112bd4eac2"));
  92. db.issues().insert(rule, project, file, i -> i.setAuthorLogin("luke@skywalker.name").setKee("82fd47d4-b650-4037-80bc-7b1182fd47d4"));
  93. db.commit();
  94. allowAnyoneOnProjects(project);
  95. indexIssues();
  96. }
  97. @Test
  98. public void authors_facet_is_hidden_if_organization_is_not_set() {
  99. db.organizations().addMember(organization, user);
  100. userSession
  101. .logIn(user)
  102. .addMembership(organization);
  103. String input = ws.newRequest()
  104. .setParam(PARAM_COMPONENT_KEYS, project.getKey())
  105. .setParam(FACETS, "authors")
  106. .execute()
  107. .getInput();
  108. JsonAssert.assertJson(input).isSimilarTo(this.getClass().getResource(this.getClass().getSimpleName() + "/no_authors_facet.json"));
  109. JsonElement gson = new JsonParser().parse(input);
  110. assertThat(gson.getAsJsonObject().get("facets").getAsJsonArray()).isEmpty();
  111. }
  112. @Test
  113. public void authors_facet_is_hidden_if_user_is_not_a_member_of_the_organization() {
  114. userSession
  115. .logIn(user);
  116. String input = ws.newRequest()
  117. .setParam(PARAM_COMPONENT_KEYS, project.getKey())
  118. .setParam(FACETS, "authors")
  119. .execute()
  120. .getInput();
  121. JsonAssert.assertJson(input).isSimilarTo(this.getClass().getResource(this.getClass().getSimpleName() + "/no_author_and_no_authors_facet.json"));
  122. JsonElement gson = new JsonParser().parse(input);
  123. assertThat(gson.getAsJsonObject().get("facets").getAsJsonArray()).isEmpty();
  124. }
  125. @Test
  126. public void authors_facet_is_shown_if_organization_is_set_and_user_is_member_of_the_organization() {
  127. db.organizations().addMember(organization, user);
  128. userSession
  129. .logIn(user)
  130. .addMembership(organization);
  131. ws.newRequest()
  132. .setParam(PARAM_COMPONENT_KEYS, project.getKey())
  133. .setParam(FACETS, "authors")
  134. .setParam(PARAM_ORGANIZATION, organization.getKey())
  135. .execute()
  136. .assertJson(this.getClass(), "with_authors_facet.json");
  137. }
  138. private void indexIssues() {
  139. issueIndexer.indexOnStartup(null);
  140. }
  141. private void allowAnyoneOnProjects(ComponentDto... projects) {
  142. userSession.registerComponents(projects);
  143. Arrays.stream(projects).forEach(p -> permissionIndexer.allowOnlyAnyone(p));
  144. }
  145. }