Skip to content
Snippets Groups Projects
Unverified Commit c6760ff1 authored by Nemanja Glumac's avatar Nemanja Glumac Committed by GitHub
Browse files

Refactor alert e2 tests (#18624)

parent 4eefe9a4
No related branches found
No related tags found
No related merge requests found
// TODO: does this really need to be a global helper function?
export function createBasicAlert({ firstAlert, includeNormal } = {}) {
cy.get(".Icon-bell").click();
if (firstAlert) {
cy.findByText("Set up an alert").click();
}
cy.findByText("Let's set up your alert");
if (includeNormal) {
cy.findByText("Email alerts to:")
.parent()
.children()
.last()
.click();
cy.findByText("Robert Tableton").click();
}
cy.findByText("Done").click();
cy.findByText("Let's set up your alert").should("not.exist");
}
export function setupLocalHostEmail() {
// Email info
cy.findByPlaceholderText("smtp.yourservice.com").type("localhost");
......
import {
restore,
setupLocalHostEmail,
createBasicAlert,
} from "__support__/e2e/cypress";
// Port from alert.e2e.spec.js
// [quarantine]: cannot run tests that rely on email setup in CI (yet)
// TODO: unskip once we have update CI with this functionality
// NOTE: freely unskip if running these tests locally
describe.skip("scenarios > alert > auth for alerts", () => {
import { restore, setupSMTP } from "__support__/e2e/cypress";
describe("scenarios > alert > alert permissions", () => {
// Intentional use of before (not beforeEach) hook because the setup is quite long.
// Make sure that all tests are always able to run independently!
before(() => {
restore();
cy.signInAsAdmin();
// Setup email
// NOTE: Must run `python -m smtpd -n -c DebuggingServer localhost:1025` before these tests
cy.signInAsAdmin();
cy.visit("/admin/settings/email");
setupLocalHostEmail();
setupSMTP();
// Create alert as admin
cy.visit("/question/1");
......@@ -43,36 +33,32 @@ describe.skip("scenarios > alert > auth for alerts", () => {
});
it("should let you edit an alert", () => {
cy.server();
cy.route("PUT", "/api/alert/1").as("savedQuestion");
cy.intercept("PUT", "/api/alert/1").as("updatedAlert");
// Change alert
cy.visit(`/question/1`);
cy.icon("bell").click();
cy.findByText("Edit").click();
cy.findByText("Daily").click();
cy.findByText("Weekly").click();
cy.findByText("Save changes").click();
cy.button("Save changes").click();
// Check that changes stuck
cy.wait("@savedQuestion");
cy.request("api/alert").then(response => {
expect(response.body[0].channels[0].schedule_type).to.equal("weekly");
cy.wait("@updatedAlert").then(({ response: { body } }) => {
expect(body.channels[0].schedule_type).to.equal("weekly");
});
// Change alert back
cy.icon("bell").click();
cy.findByText("Edit").click();
cy.findByText("Weekly").click();
cy.findByText("Daily").click();
});
});
describe("as a non-admin / normal user", () => {
beforeEach(cy.signInAsNormalUser);
it("should not let you see other people's alerts", () => {
cy.visit("/question/1");
cy.icon("bell").click();
cy.findByText("Unsubscribe").should("not.exist");
cy.findByText("Set up an alert");
});
......@@ -80,6 +66,7 @@ describe.skip("scenarios > alert > auth for alerts", () => {
it("should let you see other alerts where you are a recipient", () => {
cy.visit("/question/2");
cy.icon("bell").click();
cy.findByText("You're receiving Bobby's alerts");
cy.findByText("Set up your own alert");
});
......@@ -87,6 +74,7 @@ describe.skip("scenarios > alert > auth for alerts", () => {
it("should let you see your own alerts", () => {
cy.visit("/question/3");
cy.icon("bell").click();
cy.findByText("You set up an alert");
});
......@@ -107,3 +95,22 @@ describe.skip("scenarios > alert > auth for alerts", () => {
});
});
});
function createBasicAlert({ firstAlert, includeNormal } = {}) {
cy.get(".Icon-bell").click();
if (firstAlert) {
cy.findByText("Set up an alert").click();
}
if (includeNormal) {
cy.findByText("Email alerts to:")
.parent()
.children()
.last()
.click();
cy.findByText("Robert Tableton").click();
}
cy.findByText("Done").click();
cy.findByText("Let's set up your alert").should("not.exist");
}
import { restore, setupSMTP } from "__support__/e2e/cypress";
import { SAMPLE_DATASET } from "__support__/e2e/cypress_sample_dataset";
const { PEOPLE, PEOPLE_ID } = SAMPLE_DATASET;
const multiSeriesQuestionWithGoal = {
name: "multi",
query: {
"source-table": PEOPLE_ID,
aggregation: [["count"]],
breakout: [
["field", PEOPLE.SOURCE, null],
[
"field",
PEOPLE.CREATED_AT,
{
"temporal-unit": "month",
},
],
],
},
display: "line",
};
const timeSeriesQuestionId = 3;
const rawTestCases = [
{
questionType: "raw data question",
questionId: 1,
},
{
questionType: "timeseries question without a goal",
questionId: timeSeriesQuestionId,
},
];
describe("scenarios > alert > types", () => {
beforeEach(() => {
cy.intercept("POST", "/api/alert").as("savedAlert");
restore();
cy.signInAsAdmin();
setupSMTP();
});
describe("rows based alerts", () => {
rawTestCases.forEach(({ questionType, questionId }) => {
it(`should be supported for ${questionType}`, () => {
cy.visit(`/question/${questionId}`);
openAlertModal();
cy.findByText("Done").click();
cy.wait("@savedAlert").then(({ response: { body } }) => {
expect(body.alert_condition).to.equal("rows");
});
});
});
});
describe("goal based alerts", () => {
it("should work for timeseries questions with a set goal", () => {
// Set goal on timeseries question
cy.visit(`/question/${timeSeriesQuestionId}`);
cy.findByText("Settings").click();
cy.findByText("Line options");
setGoal("7000");
// Save question
cy.findByText("Save").click();
cy.get(".Modal")
.button("Save")
.click();
cy.findByText("Save question").should("not.exist");
openAlertModal();
cy.findByText("Goes above the goal line").click();
cy.findByText("The first time").click();
cy.button("Done").click();
// Check the API response
cy.wait("@savedAlert").then(({ response: { body } }) => {
expect(body.alert_condition).to.equal("goal");
expect(body.alert_above_goal).to.equal(true);
expect(body.alert_first_only).to.equal(true);
});
});
it("should not be possible to create goal based alert for a multi-series question", () => {
cy.createQuestion(multiSeriesQuestionWithGoal, { visitQuestion: true });
openAlertModal();
// *** The warning below is not showing when we try to make an alert (Issue #???)
// cy.contains(
// "Goal-based alerts aren't yet supported for charts with more than one line",
// );
cy.findByText("Done").click();
// The alert condition should fall back to rows
cy.wait("@savedAlert").then(({ response: { body } }) => {
expect(body.alert_condition).to.equal("rows");
expect(body.alert_above_goal).to.equal(null);
});
});
});
});
function openAlertModal() {
cy.icon("bell").click();
cy.findByText("Set up an alert").click();
}
function setGoal(goal) {
// Enable the toggle
cy.findByText("Goal line")
.next()
.click();
cy.findByDisplayValue("0")
.clear()
.type(goal);
cy.button("Done").click();
}
import {
restore,
setupLocalHostEmail,
createBasicAlert,
popover,
openPeopleTable,
visualize,
setupSMTP,
mockSlackConfigured,
} from "__support__/e2e/cypress";
// Ported from alert.e2e.spec.js
// *** We should also check that alerts can be set up through slack
const raw_q_id = 1;
const timeseries_q_id = 3;
export function setGoal(number) {
cy.findByText("Settings").click();
cy.findByText("Line options");
cy.findByText("Goal line")
.next()
.click();
cy.get("input[value='0']")
.clear()
.type(number);
cy.findByText("Done").click();
}
const channels = { slack: mockSlackConfigured, email: setupSMTP };
describe("scenarios > alert", () => {
beforeEach(() => {
restore();
cy.signInAsAdmin();
});
describe("with nothing set", () => {
beforeEach(() => {
restore();
cy.signInAsAdmin();
});
it("should prompt you to add email/slack credentials", () => {
cy.visit("/question/1");
cy.icon("bell").click();
cy.findByText(
"To send alerts, you'll need to set up email or Slack integration.",
);
......@@ -43,169 +23,48 @@ describe("scenarios > alert", () => {
it("should say to non-admins that admin must add email credentials", () => {
cy.signInAsNormalUser();
cy.visit("/question/1");
cy.icon("bell").click();
cy.findByText(
"To send alerts, an admin needs to set up email integration.",
);
});
});
// [quarantine]: cannot run tests that rely on email setup in CI (yet)
describe.skip("educational screen", () => {
before(() => {
// NOTE: Must run `python -m smtpd -n -c DebuggingServer localhost:1025` before these tests
cy.signInAsAdmin();
cy.visit("/admin/settings/email");
setupLocalHostEmail();
cy.server();
});
it("should show for the first alert, but not the second", () => {
// Create first alert
cy.visit("/question/1");
cy.icon("bell").click();
cy.findByText("The wide world of alerts");
cy.contains("When a raw data question returns any results");
cy.findByText("Set up an alert").click();
cy.findByText("Done").click();
// Create second alert
cy.visit("/question/1");
cy.icon("bell").click();
cy.findByText("The wide world of alerts").should("not.exist");
});
});
// [quarantine]: cannot run tests that rely on email setup in CI (yet)
describe.skip("types of alerts", () => {
before(() => {
restore();
cy.signInAsAdmin();
cy.visit("/admin/settings/email");
setupLocalHostEmail();
});
describe("'rows present' alert", () => {
it("should be supported for raw data questions ", () => {
cy.visit(`/question/${raw_q_id}`);
cy.icon("table");
createBasicAlert({ firstAlert: true });
cy.request("/api/alert/").then(response => {
expect(response.body[0].alert_condition).to.equal("rows");
});
});
Object.entries(channels).forEach(([channel, setup]) => {
describe(`with ${channel} set up`, () => {
beforeEach(setup);
it("should be supported for timeseries questions without a goal", () => {
cy.visit(`/question/${timeseries_q_id}`);
cy.icon("line");
it("educational screen should show for the first alert, but not for the second", () => {
cy.intercept("POST", "/api/alert").as("savedAlert");
cy.intercept("POST", "/api/card/2/query").as("questionLoaded");
createBasicAlert({ firstAlert: true });
cy.request("api/alert").then(response => {
expect(response.body[1].alert_condition).to.equal("rows");
});
});
it("should work for timeseries questions with a set goal", () => {
cy.server();
cy.route("PUT", "/api/alert/2").as("savedAlert");
// Open the first alert screen and create an alert
cy.visit("/question/1");
cy.icon("bell").click();
// Set goal on timeseries
cy.visit(`/question/${timeseries_q_id}`);
setGoal("7000");
cy.findByText("The wide world of alerts");
cy.findByText("There are a few different kinds of alerts you can get");
// Save question
cy.findByText("Save").click();
cy.findAllByText("Save")
.last()
.click();
cy.findByText("Save question").should("not.exist");
cy.contains("When a raw data question returns any results");
cy.contains("When a line or bar crosses a goal line");
cy.contains("When a progress bar reaches its goal");
// Create alert
cy.icon("bell").click();
cy.findByText("Edit").click();
cy.findByText("Goes above the goal line").click();
cy.findByText("The first time").click();
cy.findByText("Save changes").click();
cy.findByText("Set up an alert").click();
cy.findByText("Done").click();
// Check api call
cy.wait("@savedAlert");
cy.request("/api/alert/").then(response => {
expect(response.body[1].alert_above_goal).to.equal(true);
expect(response.body[1].alert_first_only).to.equal(true);
});
});
});
});
// [quarantine]: cannot run tests that rely on email setup in CI (yet)
describe.skip("time-multiseries questions with a set goal", () => {
before(() => {
restore();
cy.signInAsAdmin();
cy.visit("/admin/settings/email");
setupLocalHostEmail();
});
it.skip("should fall back to raw data alert and show a warning", () => {
// Create a time-multiseries q
openPeopleTable();
cy.findByText("Summarize").click();
cy.icon("notebook").click();
cy.findByText("Summarize").click();
cy.findByText("Count of rows").click();
cy.findByText("Pick a column to group by").click();
popover()
.within(() => {
cy.findByPlaceholderText("Find...").type("S");
cy.findByText("Source").click();
})
.then(() => {
cy.findAllByText("Source")
.parent()
.parent()
.find(".Icon-add")
.click();
});
popover().within(() => {
cy.findByPlaceholderText("Find...").type("Cr");
cy.findByText("Created At").click();
});
visualize();
// Open the second alert screen
cy.visit("/question/2");
cy.wait("@questionLoaded");
// Set a goal
setGoal("35");
cy.findByText("Save").click();
cy.findAllByText("Save")
.last()
.click();
cy.findByText("Not now").click();
cy.icon("bell").click();
// Create Alert
cy.icon("bell").click();
cy.findByText("Set up an alert").click();
// *** This below warning is not showing when we try to make an alert (Issue #???)
cy.contains(
"Goal-based alerts aren't yet supported for charts with more than one line",
);
cy.findByText("Goes above the goal line").click();
cy.findByText("Every time").click();
cy.findByText("Done").click();
cy.findByText("Your alert is all set up.")
.parent()
.find(".Icon-close")
.click();
// Check that alert has changed to raw data/ is not 'goal'
cy.request("/api/alert").then(response => {
expect(response.body[0].alert_condition).to.equal("rows");
cy.findByText("Let's set up your alert");
cy.findByText("The wide world of alerts").should("not.exist");
});
});
});
......
import { restore, setupSMTP } from "__support__/e2e/cypress";
describe("scenarios > alert > email_alert", () => {
beforeEach(() => {
cy.intercept("POST", "/api/alert").as("savedAlert");
restore();
cy.signInAsAdmin();
setupSMTP();
});
it("should have no alerts set up initially", () => {
cy.visit("/");
cy.request("/api/alert").then(({ body }) => {
expect(body).to.have.length(0);
});
});
it("should set up an email alert", () => {
openAlertForQuestion();
cy.button("Done").click();
cy.wait("@savedAlert").then(({ response: { body } }) => {
expect(body.channels).to.have.length(1);
expect(body.channels[0].channel_type).to.eq("email");
expect(body.channels[0].enabled).to.eq(true);
});
});
it("should respect email alerts toggled off (metabase#12349)", () => {
openAlertForQuestion();
// Turn off email
toggleChannel("Email");
// Turn on Slack
toggleChannel("Slack");
cy.button("Done").click();
cy.wait("@savedAlert").then(({ response: { body } }) => {
console.log(body);
expect(body.channels).to.have.length(2);
expect(body.channels[0].channel_type).to.eq("email");
expect(body.channels[0].enabled).to.eq(false);
});
});
});
function openAlertForQuestion(id = 1) {
cy.visit(`/question/${id}`);
cy.icon("bell").click();
cy.findByText("Set up an alert").click();
}
function toggleChannel(channel) {
cy.findByText(channel)
.parent()
.find("a")
.click();
}
import { restore, setupLocalHostEmail } from "__support__/e2e/cypress";
function setUpHourlyAlert(question_num) {
cy.visit(`/question/${question_num}`);
cy.icon("bell").click();
cy.findByText("Set up an alert").click();
cy.findByText("Daily").click();
cy.findByText("Hourly").click();
}
describe("scenarios > alert > email_alert", () => {
beforeEach(restore);
beforeEach(cy.signInAsAdmin);
it("should have no alerts set up initially", () => {
cy.server();
cy.visit("/");
cy.request("/api/alert").then(response => {
expect(response.body).to.have.length(0);
});
});
describe.skip("alert set up", () => {
// NOTE: To run tests, first run `python -m smtpd -n -c DebuggingServer localhost:1025` in your terminal
beforeEach(() => {
cy.server();
cy.visit("/admin/settings/email");
cy.findByText("SMTP Host");
setupLocalHostEmail();
});
it("should work with email alerts toggled on", () => {
// Set up alert
setUpHourlyAlert(1);
cy.findByText("Done")
.click()
.then(() => {
cy.findByText("Sample Dataset");
});
// Check alert api is sending email
cy.request("/api/alert").then(response => {
expect(response.body[0].channels).to.have.length(1);
expect(response.body[0].channels[0].recipients).to.have.length(1);
});
});
it("should have email alerts toggled off (metabase#12349)", () => {
// Turn off email alerts during alert setup
setUpHourlyAlert(2);
cy.findByText("Email")
.parent()
.find("a")
.click();
cy.findByText("Done")
.click()
.then(() => {
cy.findAllByText("Orders");
});
// Check alert api is NOT sending email
cy.request("/api/alert").then(response => {
expect(response.body[0].channels).to.have.length(1);
expect(response.body[0].channels[0].recipients).to.equal("null");
});
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment