]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10264 Add ITs for project badges page
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>
Wed, 17 Jan 2018 14:22:27 +0000 (15:22 +0100)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Thu, 25 Jan 2018 14:16:50 +0000 (15:16 +0100)
server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/Tester.java
server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/pageobjects/Navigation.java
server/sonar-web/src/main/js/apps/overview/badges/BadgesModal.tsx
server/sonar-web/src/main/js/apps/overview/badges/__tests__/__snapshots__/BadgesModal-test.tsx.snap
tests/src/test/java/org/sonarqube/tests/project/ProjectBadgesTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/project/SonarCloudProjectSuite.java [new file with mode: 0644]

index 0337ca80044629c4e9d30d1bc3f7a3d3013b07b6..9401ef2a9070bba83463adac7b90533c61eb5465 100644 (file)
@@ -141,6 +141,11 @@ public class Tester extends ExternalResource implements TesterSession {
     return Navigation.create(orchestrator);
   }
 
+  public Navigation openBrowser(String path) {
+    verifyStarted();
+    return Navigation.create(orchestrator, path);
+  }
+
   private void verifyNotStarted() {
     if (beforeCalled) {
       throw new IllegalStateException("Orchestrator should not be already started");
index 930c84317de8fc3ac8bbc1be750042d0291ef6f2..f4e37dce67f82835d2d9c54d911e022248c2bd7e 100644 (file)
@@ -47,12 +47,16 @@ public class Navigation {
   }
 
   public static Navigation create(Orchestrator orchestrator) {
+    return create(orchestrator, "/");
+  }
+
+  public static Navigation create(Orchestrator orchestrator, String path) {
     WebDriver driver = SelenideConfig.configure(orchestrator);
     driver.manage().deleteAllCookies();
     clearStorage(d -> d.getLocalStorage().clear());
     clearStorage(d -> d.getSessionStorage().clear());
     clearStorage(d -> Selenide.clearBrowserLocalStorage());
-    return Selenide.open("/", Navigation.class);
+    return Selenide.open(path, Navigation.class);
   }
 
   private static void clearStorage(Consumer<WebStorage> cleaner) {
index 4eea5f43a12bd235aab3fcd9ce08081d0b91f607..d8d23b2182c288276961a6cfc63dee574b458c23 100644 (file)
@@ -64,7 +64,9 @@ export default class BadgesModal extends React.PureComponent<Props, State> {
     const fullBadgeOptions = { branch, project, ...badgeOptions };
     return (
       <>
-        <button onClick={this.handleOpen}>{translate('overview.badges.get_badge')}</button>
+        <button className="js-project-badges" onClick={this.handleOpen}>
+          {translate('overview.badges.get_badge')}
+        </button>
         {this.state.open && (
           <Modal contentLabel={header} onRequestClose={this.handleClose}>
             <header className="modal-head">
index c6fd05219b0806a9f3cceec095ae91ca2a7637f4..00e92664a30989b93f1d6fb61c68b44036c56c8a 100644 (file)
@@ -3,6 +3,7 @@
 exports[`should display the modal after click 1`] = `
 <React.Fragment>
   <button
+    className="js-project-badges"
     onClick={[Function]}
   >
     overview.badges.get_badge
diff --git a/tests/src/test/java/org/sonarqube/tests/project/ProjectBadgesTest.java b/tests/src/test/java/org/sonarqube/tests/project/ProjectBadgesTest.java
new file mode 100644 (file)
index 0000000..7c1cb6a
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonarqube.tests.project;
+
+import com.codeborne.selenide.Condition;
+import com.codeborne.selenide.ElementsCollection;
+import com.codeborne.selenide.SelenideElement;
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.build.SonarScanner;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonarqube.qa.util.Tester;
+import org.sonarqube.ws.Organizations.Organization;
+import org.sonarqube.ws.Users.CreateWsResponse.User;
+import org.sonarqube.ws.client.projects.UpdateVisibilityRequest;
+
+import static com.codeborne.selenide.Selenide.$;
+import static util.ItUtils.projectDir;
+
+public class ProjectBadgesTest {
+
+  @ClassRule
+  public static Orchestrator orchestrator = SonarCloudProjectSuite.ORCHESTRATOR;
+
+  @Rule
+  public Tester tester = new Tester(orchestrator);
+
+  @Test
+  public void public_project_badges() {
+    orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample")));
+    tester.openBrowser("/projects").openProjectDashboard("sample");
+
+    SelenideElement badgesModal = openBadgesModal();
+    ElementsCollection badgeButtons = badgesModal.$$(".badge-button").shouldHaveSize(3);
+
+    // Check quality gate badge
+    shouldHaveUrl(badgesModal, "api/project_badges/measure?project=sample&metric=alert_status");
+
+    // Check bugs badge
+    selectOption("Bugs");
+    shouldHaveUrl(badgesModal, "api/project_badges/measure?project=sample&metric=bugs");
+
+    // Check marketing quality gate badge
+    badgeButtons.get(1).click();
+    shouldHaveUrl(badgesModal, "api/project_badges/quality_gate?project=sample");
+
+    // Check scanned on SonarCloud badge
+    badgeButtons.get(2).click();
+    selectOption("Orange");
+    shouldHaveUrl(badgesModal, "images/project_badges/sonarcloud-orange.svg");
+  }
+
+  @Test
+  public void private_project_do_not_have_badges() {
+    Organization org = tester.organizations().generate();
+    User user = tester.users().generateAdministrator(org);
+    orchestrator.executeBuild(
+      SonarScanner
+        .create(projectDir("shared/xoo-sample"))
+        .setProperties("sonar.organization", org.getKey(), "sonar.login", user.getLogin(), "sonar.password", user.getLogin())
+    );
+    tester.wsClient().projects().updateVisibility(new UpdateVisibilityRequest().setProject("sample").setVisibility("private"));
+    tester.openBrowser("/projects").logIn().submitCredentials(user.getLogin()).openProjectDashboard("sample");
+    shouldNotHaveBadges();
+  }
+
+  private void shouldNotHaveBadges() {
+    $(".js-project-badges").shouldNot(Condition.exist);
+  }
+
+  private SelenideElement openBadgesModal() {
+    $(".js-project-badges").shouldBe(Condition.visible).click();
+    return $(".modal-body").shouldBe(Condition.visible);
+  }
+
+  private void selectOption(String option) {
+    SelenideElement select = $(".Select").should(Condition.visible);
+    select.click();
+    select.$$(".Select-option").find(Condition.text(option)).should(Condition.exist).click();
+  }
+
+  private void shouldHaveUrl(SelenideElement badgesModal, String url) {
+    badgesModal.$(".badge-snippet pre")
+      .shouldBe(Condition.visible)
+      .shouldHave(Condition.text(url));
+  }
+}
diff --git a/tests/src/test/java/org/sonarqube/tests/project/SonarCloudProjectSuite.java b/tests/src/test/java/org/sonarqube/tests/project/SonarCloudProjectSuite.java
new file mode 100644 (file)
index 0000000..872029f
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonarqube.tests.project;
+
+import com.sonar.orchestrator.Orchestrator;
+import org.junit.ClassRule;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+import static util.ItUtils.xooPlugin;
+
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+  ProjectBadgesTest.class
+})
+public class SonarCloudProjectSuite {
+
+  @ClassRule
+  public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
+    .addPlugin(xooPlugin())
+    .setServerProperty("sonar.sonarcloud.enabled", "true")
+    .build();
+}