Skip to content

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

4.1 KB
Raw Blame
import {
  sidebar,
  toggleDashboardSubscriptionsSidebar,
} from "e2e/support/helpers";

import { WEBMAIL_CONFIG } from "../cypress_data";

const INBOX_TIMEOUT = 5000;
const INBOX_INTERVAL = 100;

const { WEB_PORT, SMTP_PORT } = WEBMAIL_CONFIG;

/**
 * `bun run test-cypress` starts maildev for you via
 * e2e/test/scenarios/docker-compose.yml. To start it manually instead:
 * `docker run -d -p 127.0.0.1:1180:1080 -p 127.0.0.1:1125:1025 maildev/maildev:2.2.1`
 * or
 * `npx maildev -s 1125 -w 1180`
 */
export const setupSMTP = () => {
  cy.log("Set up Webmail SMTP server");

  cy.request("PUT", "/api/email", {
    "email-smtp-host": "localhost",
    "email-smtp-port": SMTP_PORT,
    "email-smtp-username": "admin",
    "email-smtp-password": "admin",
    "email-smtp-security": "none",
    "email-from-address": "[email protected]",
    "email-from-name": "Metabase",
    "email-reply-to": ["[email protected]"],
  });

  // We must always clear Webmail's inbox before each test
  clearInbox();
};

export const getInbox = (emailsCount) => {
  return getInboxWithRetry(emailsCount);
};

const getInboxWithRetry = (emailsCount, timeout = INBOX_TIMEOUT) => {
  return cy
    .request("GET", `http://localhost:${WEB_PORT}/email`)
    .then((response) => {
      if (
        response.body.length &&
        (emailsCount == null || emailsCount === response.body.length)
      ) {
        return cy.wrap(response);
      } else if (timeout > 0) {
        cy.wait(INBOX_INTERVAL);
        return getInboxWithRetry(emailsCount, timeout - INBOX_INTERVAL);
      } else {
        throw new Error("Inbox retry timeout");
      }
    });
};

export const clearInbox = () => {
  return cy.request("DELETE", `http://localhost:${WEB_PORT}/email/all`);
};

export const viewEmailPage = (emailSubject) => {
  const webmailInterface = `http://localhost:${WEB_PORT}`;

  cy.window().then((win) => (win.location.href = webmailInterface));
  cy.findByText(emailSubject).click();
};

export const openEmailPage = (emailSubject) => {
  const webmailInterface = `http://localhost:${WEB_PORT}`;

  cy.window().then((win) => (win.location.href = webmailInterface));
  cy.findAllByText(emailSubject).first().click();

  return cy.hash().then((path) => {
    const htmlPath = `${webmailInterface}${path.slice(1)}/html`;
    cy.window().then((win) => (win.location.href = htmlPath));
    cy.findByText(emailSubject);
  });
};

export const clickSend = () => {
  cy.intercept("POST", "/api/pulse/test").as("emailSent");

  cy.findByText("Send email now").click();
  cy.wait("@emailSent");
};

export const openAndAddEmailsToSubscriptions = (recipients) => {
  toggleDashboardSubscriptionsSidebar();

  sidebar().findByText("Set up a dashboard subscription").should("be.visible");

  cy.findByText("Email it").click();

  recipients.forEach((recipient) => {
    cy.findByTestId("token-field")
      .find("input")
      .click()
      .type(`${recipient}{downarrow}{enter}`)
      .blur();
  });
};

export const setupSubscriptionWithRecipients = (recipients) => {
  openAndAddEmailsToSubscriptions(recipients);
  sidebar().findByText("Done").click();
};

export const openPulseSubscription = () => {
  sidebar().findByLabelText("Pulse Card").click();
};

export const emailSubscriptionRecipients = () => {
  openPulseSubscription();
  clickSend();
};

export function sendEmailAndAssert(callback) {
  clickSend();

  cy.request("GET", `http://localhost:${WEB_PORT}/email`).then(({ body }) => {
    callback(body[0]);
  });
}

export function sendEmailAndVisitIt() {
  clickSend();
  const emailUrl = `http://localhost:${WEB_PORT}/email`;
  return cy.request("GET", emailUrl).then(({ body }) => {
    const latest = body.slice(-1)[0];
    cy.visit(`${emailUrl}/${latest.id}/html`);
  });
}

export function sendAlertAndVisitIt() {
  cy.intercept("POST", "/api/notification/send").as("testAlert");
  cy.findByLabelText("New alert")
    .should("be.visible")
    .findByText("Send now")
    .click();
  cy.wait("@testAlert");

  const emailUrl = `http://localhost:${WEB_PORT}/email`;
  return cy.request("GET", emailUrl).then(({ body }) => {
    const latest = body.slice(-1)[0];
    cy.visit(`${emailUrl}/${latest.id}/html`);
  });
}