Skip to content

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

6.3 KB
Raw Blame
const { H } = cy;
import { USERS } from "e2e/support/cypress_data";
import {
  ADMIN_PERSONAL_COLLECTION_ID,
  NORMAL_USER_ID,
  NO_DATA_PERSONAL_COLLECTION_ID,
} from "e2e/support/cypress_sample_instance_data";

describe("personal collections", () => {
  beforeEach(() => {
    H.restore();
  });

  describe("admin", () => {
    beforeEach(() => {
      cy.signInAsAdmin();
    });

    it("should see a link to other users' personal collections only if there are other users", () => {
      cy.intercept("GET", "/api/session/properties", (req) => {
        req.continue((res) => {
          res.body["active-users-count"] = 1;
          return res.body;
        });
      }).as("getSessionProperties");

      cy.visit("/");
      cy.wait("@getSessionProperties");

      H.navigationSidebar()
        .findByText("Your personal collection")
        .should("be.visible");
      H.navigationSidebar()
        .findByLabelText("Other users' personal collections")
        .should("not.exist");

      cy.intercept("GET", "/api/session/properties", (req) => {
        req.continue((res) => {
          res.body["active-users-count"] = 2;
          return res.body;
        });
      }).as("getSessionProperties");

      cy.reload();
      cy.wait("@getSessionProperties");

      H.navigationSidebar()
        .findByLabelText("Other users' personal collections")
        .should("be.visible");
    });

    it("should be able to view their own as well as other users' personal collections (including other admins)", () => {
      // Turn normal user into another admin
      cy.request("PUT", `/api/user/${NORMAL_USER_ID}`, {
        is_superuser: true,
      });

      cy.visit("/collection/root");
      cy.findAllByRole("tree")
        .contains("Your personal collection")
        .should("be.visible");
      H.navigationSidebar()
        .findByLabelText("Other users' personal collections")
        .click();
      cy.location("pathname").should("eq", "/collection/users");
      cy.findByTestId("browsercrumbs").findByText(/All personal collections/i);
      Object.values(USERS).forEach((user) => {
        const FULL_NAME = `${user.first_name} ${user.last_name}`;
        cy.findByText(FULL_NAME);
      });
    });

    it("cannot edit details for personal collections nor change permissions for personal collections or sub-collections, but can view other users' personal sub-collections (metabase#8406, metabase#15339)", () => {
      // Let's use the API to create a sub-collection "Foo" in admin's personal collection
      cy.request("POST", "/api/collection", {
        name: "Foo",
        parent_id: ADMIN_PERSONAL_COLLECTION_ID,
      });
      H.createCollection({
        name: "Nodata sub-collection",
        parent_id: NO_DATA_PERSONAL_COLLECTION_ID,
      });

      H.visitCollection(ADMIN_PERSONAL_COLLECTION_ID);

      cy.log(
        "Make sure it's not possible to edit personal collection's permissions",
      );
      H.getCollectionActions().within(() => {
        cy.icon("info").should("exist");
        cy.icon("ellipsis").should("not.exist");
      });

      // Go to the newly created sub-collection "Foo"
      H.navigationSidebar().findByText("Foo").click();
      cy.findByDisplayValue("Foo").should("be.enabled");

      cy.log(
        "Other menu options exist, but editing permissions is not possible",
      );
      H.openCollectionMenu();
      H.popover().within(() => {
        cy.findByText("Move to trash").should("be.visible");
        cy.findByText("Edit permissions").should("not.exist");
      });

      // Go to random user's personal collection
      H.visitCollection(NO_DATA_PERSONAL_COLLECTION_ID);

      H.getCollectionActions().within(() => {
        cy.icon("info").should("exist");
        cy.icon("ellipsis").should("not.exist");
      });

      cy.log(
        "Other users' personal sub-collections are visible (metabase#15339)",
      );
      cy.findByTestId("collection-table")
        .findByText("Nodata sub-collection")
        .should("be.visible");
    });
  });

  describe("admin and non-admin users", () => {
    // The test acts only inside the user's own personal collection, where group
    // collection permissions don't apply; nocollection has the most restricted UI
    ["admin", "nocollection"].forEach((user) => {
      describe(`${user} user`, () => {
        beforeEach(() => {
          cy.signIn(user);

          cy.visit("/collection/root");
          H.navigationSidebar().findByText("Your personal collection").click();

          // Create initial collection inside the personal collection and navigate to it
          addNewCollection("Foo");
          H.navigationSidebar().as("sidebar").findByText("Foo").click();
        });

        it("should be able to edit and trash collection(s) inside personal collection (metabase#15343)", () => {
          cy.intercept("PUT", "/api/collection/*").as("updateCollection");

          // Create new collection inside previously added collection
          addNewCollection("Bar");
          cy.get("@sidebar").findByText("Bar").click();
          cy.findByPlaceholderText("Add title").type("1").blur();
          cy.wait("@updateCollection")
            .its("response.statusCode")
            .should("eq", 200);
          cy.findByPlaceholderText("Add description").type("ex-bar").blur();
          cy.wait("@updateCollection")
            .its("response.statusCode")
            .should("eq", 200);

          cy.findByTestId("collection-name-heading").should(
            "have.value",
            "Bar1",
          );
          cy.findByTestId("collection-caption")
            .findByText("ex-bar")
            .should("be.visible");

          cy.get("@sidebar").findByText("Foo").click();
          cy.get("@sidebar").findByText("Bar1");

          cy.log(
            "should be able to archive collection(s) inside personal collection (metabase#15343)",
          );

          H.openCollectionMenu();
          H.popover().findByText("Move to trash").click();
          H.modal().button("Move to trash").click();
          cy.findByTestId("toast-undo").should("contain", "Trashed collection");
          cy.get("@sidebar").findByText("Foo").should("not.exist");
        });
      });
    });
  });
});

function addNewCollection(name) {
  H.startNewCollectionFromSidebar();
  cy.findByPlaceholderText("My new fantastic collection").type(name, {
    delay: 0,
  });

  cy.findByTestId("new-collection-modal").button("Create").click();
}