Skip to content

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

8.6 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DB_ID, WRITABLE_DB_ID } from "e2e/support/cypress_data";
import { mainAppLinkText } from "e2e/support/helpers";

describe("issue 26470", { tags: "@external" }, () => {
  beforeEach(() => {
    H.restore("postgres-writable");
    cy.signInAsAdmin();
    cy.request("POST", "/api/persist/enable");
  });

  it("Model Cache enable / disable toggle should reflect current state", () => {
    cy.intercept(`/api/persist/database/${WRITABLE_DB_ID}/persist`).as(
      "persist",
    );
    cy.intercept(`/api/persist/database/${WRITABLE_DB_ID}/unpersist`).as(
      "unpersist",
    );

    cy.visit(`/admin/databases/${WRITABLE_DB_ID}`);

    cy.findByTestId("database-model-persistence-section")
      .findByLabelText("Model persistence")
      .should("not.be.checked")
      .click({ force: true });
    cy.wait("@persist").its("response.statusCode").should("eq", 204);

    cy.findByTestId("database-model-persistence-section")
      .findByLabelText("Model persistence")
      .should("be.checked")
      .click({ force: true });
    cy.wait("@unpersist").its("response.statusCode").should("eq", 204);

    cy.findByTestId("database-model-persistence-section")
      .findByLabelText("Model persistence")
      .should("not.be.checked");
  });
});

describe("issue 33035", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();
    cy.request("GET", "/api/user/current").then(({ body: { id: user_id } }) => {
      cy.request("PUT", `/api/user/${user_id}`, { locale: "de" });
    });
  });

  it("databases page should work in a non-default locale (metabase#33035)", () => {
    cy.visit(`/admin/permissions/data/database/${SAMPLE_DB_ID}`);
    cy.findByRole("main").findByText("Orders").should("be.visible");
  });
});

describe("issue 21532", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();
  });

  it("should allow navigating back from admin settings (metabase#21532)", () => {
    cy.visit("/");

    H.goToAdmin();
    cy.findByTestId("admin-layout-content");

    cy.go("back");
    cy.location().should((location) => {
      expect(location.pathname).to.eq("/");
    });
  });
});

describe("issue 41765", { tags: "@external" }, () => {
  // In this test we are testing the in-browser cache that metabase uses,
  // so we need to navigate by clicking trough the UI without reloading the page.

  const WRITABLE_DB_DISPLAY_NAME = "Writable Postgres12";

  const TEST_TABLE = "scoreboard_actions";
  const TEST_TABLE_DISPLAY_NAME = "Scoreboard Actions";

  const COLUMN_NAME = "another_column";
  const COLUMN_DISPLAY_NAME = "Another Column";

  beforeEach(() => {
    H.restore("postgres-writable");
    H.resetTestTable({ type: "postgres", table: TEST_TABLE });
    cy.signInAsAdmin();

    H.resyncDatabase({
      dbId: WRITABLE_DB_ID,
      tableName: TEST_TABLE,
    });
  });

  function openWritableDatabaseQuestion() {
    // start new question without navigating
    H.appBar().findByText("New").click();
    H.popover().findByText("Question").click();

    H.miniPicker().within(() => {
      cy.findByText(WRITABLE_DB_DISPLAY_NAME).click();
      cy.findByText(TEST_TABLE_DISPLAY_NAME).click();
    });
  }

  it("re-syncing a database should invalidate the table cache (metabase#41765)", () => {
    cy.visit("/");
    cy.findByTestId("loading-indicator").should("not.exist");

    openWritableDatabaseQuestion();

    H.getNotebookStep("data").button("Pick columns").click();
    H.popover().findByText(COLUMN_DISPLAY_NAME).should("not.exist");

    H.goToAdmin();

    H.appBar().findByText("Databases").click();
    cy.findAllByRole("link").contains(WRITABLE_DB_DISPLAY_NAME).click();

    H.queryWritableDB(
      `ALTER TABLE ${TEST_TABLE} ADD ${COLUMN_NAME} text;`,
      "postgres",
    );

    cy.button("Sync database schema").click();
    H.waitForSyncToFinish({
      iteration: 0,
      dbId: WRITABLE_DB_ID,
      tableName: TEST_TABLE,
    });

    H.goToMainApp();
    openWritableDatabaseQuestion();

    H.getNotebookStep("data").button("Pick columns").click();
    H.popover().findByText(COLUMN_DISPLAY_NAME).should("be.visible");
  });
});

describe("(metabase#45042)", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();
  });

  it("Should display tabs in normal view, and a nav menu in mobile view", () => {
    cy.visit("/admin");

    //Ensure tabs are present in normal view
    cy.findByTestId("admin-navbar").within(() => {
      cy.findByRole("link", { name: "Settings" }).should("exist");
      H.getProfileLink().should("exist");
    });

    //Shrink viewport
    cy.viewport(500, 750);

    //ensure that hamburger is visible and functional
    cy.findByTestId("admin-navbar").within(() => {
      cy.findByRole("button", { name: "Toggle navigation" })
        .should("be.visible")
        .click();
      cy.findByRole("list", { name: "Navigation links" }).should("exist");
      cy.findByRole("link", { name: "Settings" }).should("exist");
    });

    // dismiss nav list
    cy.findByRole("button", { name: "Toggle navigation" })
      .should("be.visible")
      .click();
    cy.findByRole("list", { name: "Navigation links" }).should("not.exist");

    //ensure that app switcher is visible and functional
    H.getProfileLink().click();
    H.popover().findByText(mainAppLinkText).should("exist");
  });
});

describe("(metabase#46714)", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();

    cy.visit("/admin/datamodel/segment/create");

    cy.findByTestId("segment-editor").findByText("Select a table").click();

    H.pickEntity({ path: ["Databases", /Sample Database/, "Orders"] });

    cy.findByTestId("entity-picker-modal").should("not.exist");
    cy.findByTestId("segment-editor").findByText("Orders").should("be.visible");

    cy.findByTestId("segment-editor")
      .findByText("Add filters to narrow your answer")
      .click();
  });

  it("should allow users to apply relative date options in the segment date picker", () => {
    H.popover().within(() => {
      cy.findByText("Created At").click();
      cy.findByText("Relative date range…").click();
      cy.findByRole("tab", { name: "Previous" }).click();
      cy.findByLabelText("Starting from…").click();
    });

    H.relativeDatePicker.setValue(
      { value: 68, unit: "day" },
      H.segmentEditorPopover,
    );

    H.relativeDatePicker.setStartingFrom(
      {
        value: 70,
        unit: "day",
      },
      H.segmentEditorPopover,
    );

    H.popover().findByText("Add filter").click();

    cy.findByTestId("filter-pill").should(
      "have.text",
      "Created At is in the previous 68 days, starting 70 days ago",
    );
  });

  it("should not hide operator select menu behind the main filter popover", () => {
    H.popover().within(() => {
      cy.findByText("Total").click();
    });

    cy.findByLabelText("Filter operator")
      .should("have.text", "Between")
      .click();
    // eslint-disable-next-line metabase/no-unsafe-element-filtering
    H.popover().last().findByText("Less than").click();
    cy.findByLabelText("Filter operator").should("have.text", "Less than");
    H.popover().findByPlaceholderText("Enter a number").clear().type("1000");
    H.popover().findByText("Add filter").click();

    cy.findByTestId("filter-pill").should(
      "have.text",
      "Total is less than 1000",
    );
  });
});

describe("issue 45890", () => {
  beforeEach(() => {
    H.restore();
    cy.signInAsAdmin();
    H.activateToken("pro-self-hosted");

    cy.visit("/admin/performance/databases");
    H.main()
      .findByLabelText(/Edit policy for database 'Sample Database'/)
      .click();

    // The strategy form opens in a side panel; the picker options render in a portal.
    cy.findByTestId("cache-policy-panel")
      .findByTestId("cache-strategy-select")
      .click();
    cy.findByRole("option", { name: /Schedule/ }).click();

    cy.intercept("PUT", "/api/cache").as("putCacheConfig");
    cy.findByTestId("cache-policy-panel").button("Save").click();
    cy.wait("@putCacheConfig");
  });

  it("should correctly reset caching schedule form when discarding changes", () => {
    cy.findByTestId("cache-policy-panel").findByLabelText("Frequency").click();
    H.popover().findByText("weekly").click();

    cy.log("Cancel the dirty form and confirm discarding the changes");
    cy.findByTestId("cache-policy-panel").button("Cancel").click();
    cy.findByTestId("confirm-modal").button("Discard").click();

    cy.log("Reopening the form shows the saved schedule, not the draft");
    H.main()
      .findByLabelText(/Edit policy for database 'Sample Database'/)
      .click();
    cy.findByTestId("cache-policy-panel")
      .findByLabelText("Frequency")
      .should("have.value", "hourly");
  });
});