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.

ProjectBadgesTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.sonarqube.tests.project;
  21. import com.codeborne.selenide.Condition;
  22. import com.codeborne.selenide.ElementsCollection;
  23. import com.codeborne.selenide.SelenideElement;
  24. import com.sonar.orchestrator.Orchestrator;
  25. import com.sonar.orchestrator.build.SonarScanner;
  26. import org.junit.ClassRule;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.sonarqube.qa.util.Tester;
  30. import org.sonarqube.ws.Organizations.Organization;
  31. import org.sonarqube.ws.Users.CreateWsResponse.User;
  32. import org.sonarqube.ws.client.GetRequest;
  33. import org.sonarqube.ws.client.projects.UpdateVisibilityRequest;
  34. import static com.codeborne.selenide.Selenide.$;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static util.ItUtils.projectDir;
  37. public class ProjectBadgesTest {
  38. private static final String PROJECT_KEY = "sample";
  39. private static final String WS_MEASURE_BADGES_ON_QUALITY_GATE = "api/project_badges/measure?project=" + PROJECT_KEY + "&metric=alert_status";
  40. private static final String WS_MEASURE_BADGES_ON_BUGS = "api/project_badges/measure?project=" + PROJECT_KEY + "&metric=bugs";
  41. private static final String WS_QUALITY_GATE_BADGE = "api/project_badges/quality_gate?project=" + PROJECT_KEY;
  42. @ClassRule
  43. public static Orchestrator orchestrator = ProjectSuite.ORCHESTRATOR;
  44. @Rule
  45. public Tester tester = new Tester(orchestrator);
  46. @Test
  47. public void public_project_badges() {
  48. orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample")));
  49. tester.openBrowser("/projects").openProjectDashboard(PROJECT_KEY);
  50. SelenideElement badgesModal = openBadgesModal();
  51. ElementsCollection badgeButtons = badgesModal.$$(".badge-button").shouldHaveSize(2);
  52. // Check quality gate badge
  53. shouldHaveUrl(badgesModal, WS_MEASURE_BADGES_ON_QUALITY_GATE);
  54. // Check bugs badge
  55. selectOption("Bugs");
  56. shouldHaveUrl(badgesModal, WS_MEASURE_BADGES_ON_BUGS);
  57. // Check marketing quality gate badge
  58. badgeButtons.get(1).click();
  59. shouldHaveUrl(badgesModal, WS_QUALITY_GATE_BADGE);
  60. }
  61. @Test
  62. public void private_project_do_not_have_badges() {
  63. Organization org = tester.organizations().generate();
  64. User user = tester.users().generateAdministrator(org);
  65. orchestrator.executeBuild(
  66. SonarScanner
  67. .create(projectDir("shared/xoo-sample"))
  68. .setProperties("sonar.organization", org.getKey(), "sonar.login", user.getLogin(), "sonar.password", user.getLogin())
  69. );
  70. tester.wsClient().projects().updateVisibility(new UpdateVisibilityRequest().setProject("sample").setVisibility("private"));
  71. tester.openBrowser("/projects").logIn().submitCredentials(user.getLogin()).openProjectDashboard(PROJECT_KEY);
  72. shouldNotHaveBadges();
  73. }
  74. @Test
  75. public void project_badges_ws() {
  76. orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample")));
  77. assertThat(tester.wsClient().wsConnector().call(new GetRequest(WS_MEASURE_BADGES_ON_QUALITY_GATE)).failIfNotSuccessful().contentType()).isEqualTo("image/svg+xml");
  78. assertThat(tester.wsClient().wsConnector().call(new GetRequest(WS_MEASURE_BADGES_ON_BUGS)).failIfNotSuccessful().contentType()).isEqualTo("image/svg+xml");
  79. assertThat(tester.wsClient().wsConnector().call(new GetRequest(WS_QUALITY_GATE_BADGE)).failIfNotSuccessful().contentType()).isEqualTo("image/svg+xml");
  80. }
  81. private void shouldNotHaveBadges() {
  82. $(".js-project-badges").shouldNot(Condition.exist);
  83. }
  84. private SelenideElement openBadgesModal() {
  85. $(".js-project-badges").shouldBe(Condition.visible).click();
  86. return $(".modal-body").shouldBe(Condition.visible);
  87. }
  88. private void selectOption(String option) {
  89. SelenideElement select = $(".Select").should(Condition.visible);
  90. select.click();
  91. select.$$(".Select-option").find(Condition.text(option)).should(Condition.exist).click();
  92. }
  93. private void shouldHaveUrl(SelenideElement badgesModal, String url) {
  94. badgesModal.$(".code-snippet pre")
  95. .shouldBe(Condition.visible)
  96. .shouldHave(Condition.text(url));
  97. }
  98. }