Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

7.8 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { ORDERS_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";

const { PEOPLE_ID } = SAMPLE_DATABASE;

describe("scenarios > alert > email_alert", { tags: "@external" }, () => {
  beforeEach(() => {
    cy.intercept("POST", "/api/notification").as("saveAlert");
    cy.intercept("POST", "/api/card").as("saveCard");

    H.restore();
    cy.signInAsAdmin();

    H.setupSMTP();
  });

  it("should set up an email alert", () => {
    openAlertForQuestion(ORDERS_QUESTION_ID);

    cy.log(
      "Should not display slack channel if it is not configured metabase#48407",
    );
    cy.findByTestId("alert-configured-channel").within(() => {
      cy.findByTestId("loading-indicator").should("not.exist");
      cy.findByText("Email").should("be.visible");
      cy.findByText("Slack").should("not.exist");
    });
    H.modal()
      .findByRole("link", { name: "Slack" })
      .should("have.attr", "href", "/admin/settings/slack");

    H.selectScheduleTime();
    H.modal().within(() => {
      cy.findByText("New alert").should("be.visible");

      cy.findByTestId("alert-goal-select")
        .should("not.be.enabled")
        .should("have.text", "When this question has results");
    });
    cy.button("Done").click();

    cy.wait("@saveAlert").then(({ response: { body } }) => {
      expect(body.handlers).to.have.length(1);
      expect(body.handlers[0].channel_type).to.eq("channel/email");
      expect(body.payload?.send_condition).to.equal("has_result");
    });
  });

  it("should respect email alerts toggled off and save the Slack channel_id (metabase#12349)", () => {
    //For this test, we need to pretend that slack is set up
    H.mockSlackConfigured();
    H.setupNotificationChannel({ name: "Webhook" });

    openAlertForQuestion(ORDERS_QUESTION_ID);

    H.removeNotificationHandlerChannel("Email");

    H.addNotificationHandlerChannel("Slack", { hasNoChannelsAdded: true });

    H.modal()
      .findByPlaceholderText(/Pick a user or channel/)
      .click();

    H.popover().findByText("#work").click();

    H.addNotificationHandlerChannel("Email");

    H.removeNotificationHandlerChannel("Email");

    H.selectScheduleTime();
    H.modal().within(() => {
      cy.button("Done").click();
    });

    cy.wait("@saveAlert").then(({ response: { body } }) => {
      expect(body.handlers).to.have.length(1);
      expect(body.handlers[0].channel_type).to.eq("channel/slack");

      // The mocked channel `#work` has id `C001` in e2e-slack-helpers.js.
      // Storing the immutable channel_id at save time is what makes the
      // subscription survive future channel renames in Slack.
      const slackDetails = body.handlers[0].recipients[0].details;
      expect(slackDetails.value).to.eq("#work");
      expect(slackDetails.channel_id).to.eq("C001");
    });

    cy.log(
      "ensure that when the alert is deleted, the delete modal is correct metabase#48402",
    );
    cy.findByLabelText("Move, trash, and more…").click();
    H.popover().findByText("Edit alerts").click();
    H.modal().within(() => {
      cy.findByText("Edit alerts").should("be.visible");
      cy.findByText(/Created by you/).realHover();
    });

    cy.findByRole("button", { name: "Delete this alert" }).click();

    cy.findByTestId("alert-delete").within(() => {
      cy.findByText("Delete this alert?").should("be.visible");
      cy.findByText("This can't be undone.").should("be.visible");
      cy.findByText(/email/i).should("not.exist");
      cy.button("Cancel").should("be.visible");
      cy.button("Delete it").should("be.visible");
    });
  });

  it("should set up an email alert for a new question and update it without updating the question (metabase#36866)", () => {
    cy.intercept("PUT", "/api/notification/*").as("updatedAlert");
    cy.intercept("PUT", "/api/card/*").as("updateCard");

    H.openTable({
      table: PEOPLE_ID,
    });

    saveAlert();

    cy.log("Check that /api/card has been called once");
    cy.get("@saveCard.all").should("have.length", 1);

    cy.findByTestId("toast-undo")
      .findByText("Your alert is all set up.")
      .should("be.visible");

    cy.wait("@saveAlert").then(({ response: { body } }) => {
      expect(body.handlers[0].channel_type).to.eq("channel/email");
    });

    cy.findByLabelText("Move, duplicate, and more…").click();
    H.popover().findByText("Edit alerts").click();

    H.modal().within(() => {
      cy.findByText("Edit alerts").should("be.visible");
      cy.findByText(/Created by you/).click();
    });

    cy.log("Change the frequency of the alert to weekly");

    H.modal().findByTestId("select-frequency").click();

    cy.findByRole("option", { name: /weekly/i })
      .should("have.attr", "value", "weekly")
      .should("have.attr", "aria-selected", "false")
      .click();
    H.selectScheduleTime();
    cy.button("Save changes").click();

    cy.wait("@updatedAlert").then(({ response: { body } }) => {
      expect(body.subscriptions[0].cron_schedule).to.equal("0 0 8 ? * 2 *");
    });

    cy.log("Check that /api/card has still only been called once");
    cy.get("@saveCard.all").should("have.length", 1);
    cy.get("@updateCard.all").should("have.length", 0);
  });

  describe("branding", () => {
    const questionName = "Fourty Two";

    it("should include branding for OSS instances", { tags: "@OSS" }, () => {
      sendTestAlertForQuestion(questionName);
      cy.findAllByRole("link")
        .filter(":contains(Made with)")
        .should("contain", "Metabase")
        .and(
          "have.attr",
          "href",
          "https://www.metabase.com?utm_source=product&utm_medium=export&utm_campaign=exports_branding&utm_content=alert",
        );
    });

    it("should include branding for Starter instances and not claim a schedule for a one-off alert", () => {
      H.activateToken("starter");
      createQuestionAndOpenAlert(questionName);
      H.sendAlertAndVisitIt();
      cy.findAllByRole("link")
        .filter(`:contains(${questionName})`)
        .should("be.visible");

      cy.get("table.header")
        .first()
        .within(() => {
          cy.findByText(questionName).should("be.visible");
          cy.findByText(/Run daily at/).should("not.exist");
        });

      cy.findAllByRole("link")
        .filter(":contains(Made with)")
        .should("contain", "Metabase")
        .and(
          "have.attr",
          "href",
          "https://www.metabase.com?utm_source=product&utm_medium=export&utm_campaign=exports_branding&utm_content=alert",
        );
    });

    it("should not include branding on Pro/Enterprise instances", () => {
      H.activateToken("pro-self-hosted");
      sendTestAlertForQuestion(questionName);
      cy.findAllByRole("link")
        .filter(":contains(Made with)")
        .should("not.exist");
    });
  });
});

function openAlertForQuestion(id) {
  H.visitQuestion(id);
  cy.findByLabelText("Move, trash, and more…").click();
  H.popover().findByText("Create an alert").click();
}

function saveAlert() {
  H.openSharingMenu();

  H.modal().within(() => {
    cy.findByLabelText("Name").type(" alert");
    cy.button("Save").click();
  });
  cy.wait("@saveCard");

  cy.findByLabelText("Move, duplicate, and more…").click();
  H.popover().findByText("Create an alert").click();
  H.selectScheduleTime();
  H.modal().button("Done").click();
}

function createQuestionAndOpenAlert(name) {
  H.createNativeQuestion(
    {
      name,
      native: { query: "select 42" },
    },
    { visitQuestion: true },
  );

  cy.findByLabelText("Move, trash, and more…").click();
  H.popover().findByText("Create an alert").click();
}

function sendTestAlertForQuestion(name) {
  createQuestionAndOpenAlert(name);
  H.selectScheduleTime();
  H.sendAlertAndVisitIt();
  cy.findAllByRole("link").filter(`:contains(${name})`).should("be.visible");
}