From dfd30e5af9aed794886bdb7babf07e097084fa13 Mon Sep 17 00:00:00 2001
From: Nemanja Glumac <31325167+nemanjaglumac@users.noreply.github.com>
Date: Fri, 22 Oct 2021 14:52:12 +0200
Subject: [PATCH] Register mocked Slack configuration (global e2e helper)
 (#18618)

* Refactor and register global Slack configuration e2e helper

* Use slack helper in tests
---
 frontend/test/__support__/e2e/cypress.js      |  1 +
 .../e2e/helpers/e2e-slack-helpers.js          | 35 +++++++++++++++++++
 .../sharing/subscriptions.cy.spec.js          | 34 +-----------------
 3 files changed, 37 insertions(+), 33 deletions(-)
 create mode 100644 frontend/test/__support__/e2e/helpers/e2e-slack-helpers.js

diff --git a/frontend/test/__support__/e2e/cypress.js b/frontend/test/__support__/e2e/cypress.js
index 1e294df3312..5213166a94a 100644
--- a/frontend/test/__support__/e2e/cypress.js
+++ b/frontend/test/__support__/e2e/cypress.js
@@ -24,6 +24,7 @@ export * from "./helpers/e2e-data-model-helpers";
 export * from "./helpers/e2e-misc-helpers";
 export * from "./helpers/e2e-deprecated-helpers";
 export * from "./helpers/e2e-email-helpers";
+export * from "./helpers/e2e-slack-helpers";
 export * from "./helpers/e2e-custom-column-helpers";
 
 Cypress.on("uncaught:exception", (err, runnable) => false);
diff --git a/frontend/test/__support__/e2e/helpers/e2e-slack-helpers.js b/frontend/test/__support__/e2e/helpers/e2e-slack-helpers.js
new file mode 100644
index 00000000000..ad22e176092
--- /dev/null
+++ b/frontend/test/__support__/e2e/helpers/e2e-slack-helpers.js
@@ -0,0 +1,35 @@
+// Since there is no testing API token for Slack, it's easier to mock its configuration
+const mockedSlack = {
+  type: "slack",
+  name: "Slack",
+  allows_recipients: true,
+  schedules: ["hourly", "daily", "weekly", "monthly"],
+  fields: [
+    {
+      name: "channel",
+      type: "select",
+      displayName: "Post to",
+      options: ["#work", "#play"],
+      required: true,
+    },
+  ],
+  configured: true,
+};
+
+export function mockSlackConfigured() {
+  // First, let's get the actual config (email might have already been configured and we want to preserve that)
+  cy.request("GET", "/api/pulse/form_input").then(({ body }) => {
+    const originalEmailConfig = body.channels.email;
+
+    const mockedConfig = Object.assign({}, body, {
+      channels: {
+        email: originalEmailConfig,
+        slack: mockedSlack,
+      },
+    });
+
+    // Stubbing the response in advance
+    // (Cypress will intercept it when we navigate to "Dashboard subscriptions" or a question alert)
+    cy.intercept("GET", "/api/pulse/form_input", mockedConfig);
+  });
+}
diff --git a/frontend/test/metabase/scenarios/sharing/subscriptions.cy.spec.js b/frontend/test/metabase/scenarios/sharing/subscriptions.cy.spec.js
index c5c1cc87ae1..3870ef8171f 100644
--- a/frontend/test/metabase/scenarios/sharing/subscriptions.cy.spec.js
+++ b/frontend/test/metabase/scenarios/sharing/subscriptions.cy.spec.js
@@ -5,6 +5,7 @@ import {
   popover,
   mockSessionProperty,
   sidebar,
+  mockSlackConfigured,
 } from "__support__/e2e/cypress";
 import { USERS } from "__support__/e2e/cypress_data";
 const { admin } = USERS;
@@ -435,36 +436,3 @@ function addParametersToDashboard() {
   // wait for dashboard to save
   cy.contains("You're editing this dashboard.").should("not.exist");
 }
-
-function mockSlackConfigured() {
-  // Stubbing the response in advance (Cypress will intercept it when we navigate to "Dashboard subscriptions")
-  cy.server();
-  cy.route("GET", "/api/pulse/form_input", {
-    channels: {
-      email: {
-        type: "email",
-        name: "Email",
-        allows_recipients: false,
-        recipients: ["user", "email"],
-        schedules: ["hourly", "daily", "weekly", "monthly"],
-        configured: false,
-      },
-      slack: {
-        type: "slack",
-        name: "Slack",
-        allows_recipients: true,
-        schedules: ["hourly", "daily", "weekly", "monthly"],
-        fields: [
-          {
-            name: "channel",
-            type: "select",
-            displayName: "Post to",
-            options: ["#work", "#play"],
-            required: true,
-          },
-        ],
-        configured: true,
-      },
-    },
-  });
-}
-- 
GitLab