Skip to content

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

10.6 KB
Raw Blame
const { H } = cy;

import { USER_GROUPS } from "e2e/support/cypress_data";
import type { GroupListQuery } from "metabase-types/api";

import { groupMappingCardHelpers } from "./shared/group-mapping-card";

const { ADMIN_GROUP, NOSQL_GROUP, READONLY_GROUP } = USER_GROUPS;

const {
  groupMappingSection,
  groupMappingSwitch,
  mappingRow,
  newMappingButton,
  groupsPicker,
  toggleGroupMapping,
  addMapping,
  deleteMapping,
} = groupMappingCardHelpers({
  sectionTestId: "ldap-group-mapping-section",
  nameLabel: "LDAP group name",
});

describe(
  "scenarios > admin > settings > SSO > LDAP",
  { tags: "@external" },
  () => {
    beforeEach(() => {
      H.restore();
      cy.signInAsAdmin();
      cy.intercept("PUT", /\/api\/setting$/).as("updateSettings");
      cy.intercept("PUT", "/api/setting/*").as("updateSetting");
      cy.intercept("PUT", "/api/ldap/settings").as("updateLdapSettings");
      cy.intercept("POST", "/api/dataset").as("dataset");
    });

    it("should update ldap settings and not show the user provisioning UI to OSS users", () => {
      H.setupLdap();
      cy.visit("/admin/settings/authentication/ldap");

      enterLdapPort("389");

      cy.log("User provisioning is not offered to OSS users");
      cy.findByTestId("admin-layout-content")
        .findByText(/User Provisioning/i)
        .should("not.exist");

      cy.button("Save changes").click();
      cy.wait("@updateLdapSettings");

      H.goToAuthOverviewPage();

      getLdapCard().findByText("Active").should("exist");
    });

    it("should allow to disable and enable ldap, then reset its settings", () => {
      H.setupLdap();
      cy.visit("/admin/settings/authentication");

      getLdapCard().icon("ellipsis").click();
      H.popover().findByText("Pause").click();
      cy.wait("@updateSetting");
      getLdapCard().findByText("Paused").should("exist");

      getLdapCard().icon("ellipsis").click();
      H.popover().findByText("Resume").click();
      cy.wait("@updateSetting");
      getLdapCard().findByText("Active").should("exist");

      cy.log("Deactivating resets the ldap settings");
      getLdapCard().icon("ellipsis").click();
      H.popover().findByText("Deactivate").click();
      H.modal().button("Deactivate").click();
      cy.wait("@updateSettings");

      getLdapCard().findByText("Set up").should("exist");
    });

    it("should validate the port, not reset previously populated fields when connection validation fails, and setup ldap once the port is valid (metabase#13313, metabase#16173, metabase#16226)", () => {
      cy.visit("/admin/settings/authentication/ldap");

      cy.findByLabelText(/LDAP Port/i)
        .parent()
        .parent()
        .as("portSection");

      enterLdapSettings();

      cy.log("A non-integer port is not accepted (metabase#13313)");
      enterLdapPort("asd");
      cy.get("@portSection").findByDisplayValue("asd").should("not.exist");

      enterLdapPort("21.3");
      cy.get("@portSection")
        .findByText("Port must be a whole number between 1 and 65535")
        .should("be.visible");

      cy.log("Connection validation error (metabase#16226)");
      enterLdapPort("1");
      cy.button("Save and enable").click();
      cy.wait("@updateLdapSettings");

      cy.findAllByText("Wrong host or port").should("exist");
      cy.findByDisplayValue("localhost").should("exist");

      cy.log(
        "A valid port, even with a trailing space, sets up ldap (metabase#13313, metabase#16173)",
      );
      enterLdapPort("389 ");
      cy.get("@portSection")
        .findByText("Port must be a whole number between 1 and 65535")
        .should("not.exist");

      // the submit button can still read "Failed" from the rejected save above
      cy.button(/Save and enable|Failed/).click();
      cy.wait("@updateLdapSettings");
      cy.findByTestId("admin-layout-content")
        .findByText("Success")
        .should("exist");
    });

    it("should allow user login on OSS when LDAP is enabled", () => {
      H.setupLdap();
      cy.signOut();
      cy.visit("/auth/login");
      cy.findByLabelText("Username or email address").type(
        "[email protected]",
      );
      cy.findByLabelText("Password").type("123456");
      cy.button("Sign in").click();
      cy.findByTestId("main-navbar-root").within(() => {
        cy.findByText("Home").should("exist");
      });
    });

    describe("Group mapping", () => {
      beforeEach(() => {
        cy.intercept("DELETE", "/api/permissions/group/*").as("deleteGroup");
        cy.intercept("PUT", "/api/permissions/membership/*/clear").as(
          "clearGroup",
        );
        H.setupLdap();
        cy.visit("/admin/settings/authentication/ldap");
      });

      it("should delete or clear mapped groups with their mappings and keep the remaining mappings consistent", () => {
        toggleGroupMapping(true);
        addMapping("cn=People1", ["Administrators", "data", "nosql"]);
        addMapping("cn=People2", ["data", "collection"]);
        addMapping("cn=People3", ["collection", "readonly"]);

        cy.log(
          "Deleting a mapping's groups removes them from the other mappings too",
        );
        deleteMapping(
          "cn=People2",
          /delete the groups/i,
          "Remove mapping and delete groups",
        );
        cy.wait(["@deleteGroup", "@deleteGroup"]);
        cy.wait("@updateSettings")
          .its("request.body.ldap-group-mappings")
          .should("deep.equal", {
            "cn=People1": [ADMIN_GROUP, NOSQL_GROUP],
            "cn=People3": [READONLY_GROUP],
          });
        mappingRow("cn=People1").should("contain", "Administrators, nosql");
        mappingRow("cn=People3")
          .should("contain", "readonly")
          .and("not.contain", "collection");

        cy.log("Deleted groups are no longer offered for new mappings");
        newMappingButton().click();
        groupsPicker().click();
        cy.findByRole("listbox")
          .should("contain", "readonly")
          .and("not.contain", "data")
          .and("not.contain", "collection");
        cy.button("Cancel").click();

        cy.log("The same mappings come back after a reload");
        cy.reload();
        mappingRow("cn=People1").should("contain", "Administrators, nosql");
        mappingRow("cn=People3")
          .should("contain", "readonly")
          .and("not.contain", "collection");

        cy.log(
          "Clearing the last mappings empties their groups, skips Administrators and keeps group mapping on",
        );
        deleteMapping(
          "cn=People3",
          /remove all members/i,
          "Remove mapping and members",
        );
        cy.wait("@clearGroup");
        deleteMapping(
          "cn=People1",
          /remove all members/i,
          "Remove mapping and members",
        );
        cy.wait("@clearGroup");
        groupMappingSwitch().should("be.checked");
        groupMappingSection()
          .findByText("No mappings yet")
          .should("be.visible");

        cy.log("Deleted groups are gone and cleared groups have no members");
        cy.request<GroupListQuery[]>("GET", "/api/permissions/group").then(
          ({ body: groups }) => {
            const names = groups.map((group) => group.name);
            expect(names).to.include.members(["nosql", "readonly"]);
            expect(names).not.to.include("data");
            expect(names).not.to.include("collection");
            const memberCount = (name: string) =>
              groups.find((group) => group.name === name)?.member_count;
            expect(memberCount("nosql")).to.equal(0);
            expect(memberCount("readonly")).to.equal(0);
          },
        );
      });
    });
  },
);

describe(
  "scenarios > admin > settings > SSO > LDAP",
  { tags: "@external" },
  () => {
    beforeEach(() => {
      H.restore();
      cy.signInAsAdmin();
      H.activateToken("pro-self-hosted");
      cy.intercept("PUT", "/api/ldap/settings").as("updateLdapSettings");
    });

    it("should allow the user to enable/disable user provisioning", () => {
      H.setupLdap();
      cy.visit("/admin/settings/authentication/ldap");

      cy.findByRole("switch", { name: "User provisioning" }).should(
        "be.checked",
      );
      cy.contains("label", "User provisioning").click();

      H.undoToast().findByText("Changes saved").should("be.visible");
      cy.findByRole("switch", { name: "User provisioning" }).should(
        "not.be.checked",
      );
    });

    it("should show the login form when ldap is enabled but password login isn't (metabase#25661)", () => {
      H.setupLdap();
      H.updateSetting("enable-password-login", false);
      cy.signOut();
      cy.visit("/auth/login");

      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Username or email address").should("be.visible");
      // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
      cy.findByText("Password").should("be.visible");
    });

    it("should allow user login on EE when LDAP is enabled", () => {
      H.setupLdap();
      // Only allowlisted directory attributes are synced, so name the ones this test checks.
      cy.request("PUT", "/api/setting/ldap-sync-user-attributes-allowlist", {
        value: "uid,homedirectory",
      });
      cy.signOut();
      cy.visit("/auth/login");
      cy.findByLabelText("Username or email address").type(
        "[email protected]",
      );
      cy.findByLabelText("Password").type("123456");
      cy.button("Sign in").click();
      cy.findByTestId("main-navbar-root").within(() => {
        cy.findByText("Home").should("exist");
      });

      cy.signOut();
      cy.signInAsAdmin();

      // Check that attributes are synced
      cy.visit("/admin/people");
      cy.findByTestId("admin-people-list-table").within(() => {
        cy.findByText("Bar1 Bar1")
          .closest("tr")
          .within(() => {
            cy.icon("ellipsis").click();
          });
      });
      H.popover().within(() => {
        cy.findByText("Edit user").click();
      });
      cy.findByDisplayValue("uid").should("exist");
      cy.findByDisplayValue("homedirectory").should("exist");
    });
  },
);

const getLdapCard = () => {
  return cy
    .findByTestId("admin-layout-content")
    .findByText("LDAP")
    .parent()
    .parent();
};

const enterLdapPort = (value: string) => {
  H.typeAndBlurUsingLabel(/LDAP Port/i, value);
};

const enterLdapSettings = () => {
  H.typeAndBlurUsingLabel(/LDAP Host/i, "localhost");
  H.typeAndBlurUsingLabel(/LDAP Port/i, "389");
  H.typeAndBlurUsingLabel("Username or DN", "cn=admin,dc=example,dc=org");
  H.typeAndBlurUsingLabel("Password", "adminpass");
  H.typeAndBlurUsingLabel(/User search base/i, "ou=users,dc=example,dc=org");
};