Skip to content

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

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

const { normal } = USERS;

const { first_name, last_name, email, password } = normal;

describe("user > settings", () => {
  const fullName = H.getFullName(normal);

  beforeEach(() => {
    H.restore();
    cy.signInAsNormalUser();
  });

  it("should show, update, and clear user details without fetching memberships (metabase#22754)", () => {
    cy.intercept("GET", "/api/permissions/membership").as("membership");
    cy.intercept("PUT", "/api/user/*").as("updateUser");
    cy.visit("/account/profile");
    cy.findByTestId("account-header").within(() => {
      cy.findByText(fullName).should("be.visible");
      cy.findByText(email).should("be.visible");
      cy.findByRole("tab", { name: "Authentication" }).should("be.visible");
    });
    cy.findByDisplayValue(first_name).should("be.visible");
    cy.findByDisplayValue(last_name).should("be.visible");
    cy.findByDisplayValue(email).should("be.visible");
    cy.button("Update").should("be.disabled");
    cy.findByLabelText("First name").clear().type("John");
    cy.clock(Date.now(), ["setTimeout", "clearTimeout"]);
    cy.button("Update").click();
    cy.wait("@updateUser").its("response.statusCode").should("eq", 200);
    cy.button("Success").should("be.visible");
    cy.tick(5000);
    cy.button("Update").should("be.visible");
    cy.clock().then((clock) => clock.restore());
    cy.findByTestId("account-header").should("contain", `John ${last_name}`);
    cy.findByDisplayValue("John").should("be.visible");
    cy.get("@membership.all").should("have.length", 0);

    cy.findByLabelText("First name").clear();
    cy.findByLabelText("Last name").clear();
    cy.button("Update").click();
    cy.wait("@updateUser").its("response.statusCode").should("eq", 200);
    cy.reload();
    cy.findByLabelText("First name").should("be.empty");
    cy.findByLabelText("Last name").should("be.empty");
  });

  it("should redirect to the login page when the user has signed out but tries to visit `/account/profile` (metabase#15471)", () => {
    cy.signOut();
    cy.visit("/account/profile");
    cy.url().should("include", "/auth/login");
    // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
    cy.findByText("Sign in to Metabase");
  });

  it("should validate passwords and redirect after saving and signing out (metabase#23259, metabase#18151)", () => {
    cy.visit("/account/password");

    // Validate common passwords
    cy.findByLabelText(/Create a password/i)
      .as("passwordInput")
      .type("qwerty123")
      .blur();

    // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
    cy.contains("password is too common");
    cy.get("@passwordInput").clear();

    // Validate invalid current password
    cy.findByLabelText("Current password")
      .as("currentPassword")
      .type("invalid");

    cy.get("@passwordInput").type("new_password1");
    cy.findByLabelText("Confirm your password").type("new_password1");

    cy.button("Save").click();
    // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
    cy.contains("Invalid password");

    cy.findByLabelText("Current password").clear().type(password);
    cy.findByLabelText("Create a password").clear().type(password);
    cy.findByLabelText("Confirm your password").clear().type(password);
    cy.button("Save").click();
    H.main().findByText("Success").should("be.visible");
    H.getProfileLink().click();
    H.popover().findByText("Sign out").click();
    cy.findByRole("heading", { name: "Sign in to Metabase" }).should(
      "be.visible",
    );
  });

  it("should be able to change a language (metabase#22192)", () => {
    cy.intercept("PUT", "/api/user/*").as("updateUserSettings");

    cy.visit("/account/profile");

    cy.findByTestId("user-locale-select").findByRole("textbox").click();

    // eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
    H.popover().within(() => cy.findByText("Indonesian").click());

    cy.button("Update").click();
    cy.wait("@updateUserSettings");

    // Assert that the page reloaded with the new language
    cy.findByLabelText("Nama depan").should("exist");

    // We need some UI element other than a string, and cannot get by labels as they could be translated
    H.getProfileLink().should("exist");
  });

  it("should show correct translations when a user logs in with a locale that is different from the site locale", () => {
    cy.intercept("GET", "/api/user/current").as("getUser");
    cy.request("PUT", `/api/user/${NORMAL_USER_ID}`, { locale: "fr" });
    cy.signOut();
    cy.visit("/question/notebook");
    cy.wait("@getUser");
    cy.findByLabelText("Email address").type(email);
    cy.findByLabelText("Password").type(password);
    cy.button("Sign in").click();

    // should be redirected to new question page
    cy.wait("@getUser");
    H.miniPicker().findByText("Parcourir tout").click();
    H.pickEntity({ path: ["Nos analyses", "Orders Model"] });
    cy.findByTestId("step-summarize-0-0")
      .findByText("Summarize")
      .should("not.exist");
    cy.findByTestId("step-summarize-0-0").findByText("Résumer").should("exist");
  });

  ["ldap", "google", "jwt", "saml"].forEach((ssoSource) => {
    it(`should prevent ${ssoSource} users from editing SSO credentials (metabase#23298)`, () => {
      stubCurrentUser({ sso_source: ssoSource });
      cy.visit("/account/profile");
      cy.wait("@getUser");
      cy.findByTestId("account-header").should("contain", email);
      cy.findByTestId("user-locale-select").should("be.visible");
      cy.findByTestId("account-header")
        .findByRole("tab", { name: "Authentication" })
        .should("not.exist");
      cy.findByLabelText("First name").should("not.exist");
      cy.findByLabelText("Last name").should("not.exist");
      cy.findByLabelText("Email").should("not.exist");

      cy.visit("/account/password");
      cy.wait("@getUser");
      cy.findByTestId("account-header").should("contain", email);
      cy.findByLabelText("Current password").should("not.exist");
      cy.findByLabelText("Create a password").should("not.exist");
      cy.findByLabelText("Confirm your password").should("not.exist");
    });
  });

  describe("dark mode", () => {
    const isMac = Cypress.platform === "darwin";

    it("should toggle through light and dark mode when clicking on the label or icon", () => {
      cy.visit("/account/profile");

      cy.findByDisplayValue("Use system default").click();
      H.popover().findByText("Dark").click();
      assertDarkMode();

      cy.findByDisplayValue("Dark").click();
      H.popover().findByText("Light").click();
      assertLightMode();

      //Need to take focus off the input
      H.navigationSidebar().findByRole("link", { name: /Home/ }).click();
      // Wait for navigation to complete so kbar shortcut handlers are re-registered
      cy.location("pathname").should("eq", "/");
      // Use eventConstructor: "KeyboardEvent" so tinykeys' instanceof check
      // passes. cy.trigger() defaults to generic Event which kbar rejects.
      // Chrome v123+ headless also intercepts cy.realPress() CDP keyboard
      // events before they reach the page.
      cy.get("body").trigger("keydown", {
        eventConstructor: "KeyboardEvent",
        key: "L",
        code: "KeyL",
        metaKey: isMac,
        ctrlKey: !isMac,
        shiftKey: true,
      });
      assertDarkMode();
    });

    it("should persist theme selection on browser change", () => {
      cy.intercept("PUT", "/api/setting/color-scheme").as("saveSetting");

      cy.visit("/account/profile");

      cy.findByDisplayValue("Use system default").click();
      H.popover().findByText("Dark").click();
      assertDarkMode();

      cy.wait("@saveSetting");

      // emulate browser change by deleting localStorage values
      cy.window().then((win) => {
        win.sessionStorage.clear();
        win.localStorage.clear();
      });

      cy.visit("/account/profile");
      assertDarkMode();
    });

    it("should apply user's selected theme instead of browser's OS theme preference", () => {
      cy.visit("/account/profile");

      cy.findByDisplayValue("Use system default").click();
      H.popover().findByText("Light").click();

      assertLightMode();

      H.navigationSidebar().findByText("Our analytics").click();
      H.collectionTable().findByText("Orders").should("exist").click();

      cy.findByTestId("table-body").should("be.visible"); // wait for table to be rendered

      cy.window().then((win) => {
        H.getProfileLink()
          .findByText("RT")
          .should("exist")
          .then(($button) => {
            cy.wrap(win.getComputedStyle($button[0]).color).should(
              "eq",
              "rgba(7, 23, 34, 0.84)", // text-dark
            );
          });

        cy.findByTestId("viz-type-button").click();
        cy.findByTestId("sidebar-left")
          .findByText("More charts")
          .then(($text) => {
            cy.wrap(win.getComputedStyle($text[0]).color).should(
              "eq",
              "rgba(7, 23, 34, 0.62)", // text-medium
            );
          });
      });

      H.goToProfile();
      cy.findByDisplayValue("Light").click();
      H.popover().findByText("Dark").click();

      H.openNavigationSidebar();
      H.navigationSidebar().findByText("Our analytics").click();
      H.collectionTable().findByText("Orders").should("exist").click();

      cy.findByTestId("table-body").should("be.visible"); // wait for table to be rendered

      cy.window().then((win) => {
        H.getProfileLink()
          .findByText("RT")
          .should("exist")
          .then(($button) => {
            cy.wrap(win.getComputedStyle($button[0]).color).should(
              "eq",
              "rgba(255, 255, 255, 0.95)", // text-dark
            );
          });

        cy.findByTestId("viz-type-button").click();
        cy.findByTestId("sidebar-left")
          .findByText("More charts")
          .then(($text) => {
            cy.wrap(win.getComputedStyle($text[0]).color).should(
              "eq",
              "rgba(255, 255, 255, 0.69)", // text-medium
            );
          });
      });
    });

    it("should apply user's color scheme preference immediately after login (metabase#66874)", () => {
      // First, set the color scheme preference while logged in
      cy.intercept("PUT", "/api/setting/color-scheme").as("saveColorScheme");

      cy.visit("/account/profile", stubSystemColorScheme("dark"));
      cy.findByDisplayValue("Use system default").click();
      H.popover().findByText("Light").click();

      cy.wait("@saveColorScheme");

      assertLightMode();

      cy.signOut();
      cy.visit("/", stubSystemColorScheme("dark"));

      // Verify that the theme is restored to "auto" after sign out
      assertDarkMode();

      cy.intercept("GET", "/api/session/properties").as("sessionProperties");

      // Sign-in is done manually in order to test theme replacement throughout
      // react navigation, where no new metadata is passed or injected into the
      // window object, and the theme is purely updated from session properties
      cy.findByLabelText("Email address").type(email);
      cy.findByLabelText("Password").type(password);
      cy.button("Sign in").click();

      cy.wait("@sessionProperties");

      // Verify light mode is applied immediately after login
      assertLightMode();

      // Verify the theme selector shows the correct value
      cy.visit("/account/profile", stubSystemColorScheme("dark"));
      cy.findByDisplayValue("Light").should("exist");
    });
  });
});

// I wanted to examine the value of a color vairable, but it's hard to inspect hsla colors between local and CI.
// sometimes the alpha is a decimal value, sometimes it isnt...
const assertLightMode = () =>
  cy.get("body").should("have.css", "background-color", "rgb(249, 249, 250)");

const assertDarkMode = () =>
  cy.get("body").should("have.css", "background-color", "rgb(7, 23, 34)");

/**
 * Stub the current user authentication method
 *
 * @param {Object} authenticationMethod
 */
function stubCurrentUser(authenticationMethod) {
  cy.request("GET", "/api/user/current").then(({ body: user }) => {
    cy.intercept(
      "GET",
      "/api/user/current",
      Object.assign({}, user, authenticationMethod),
    ).as("getUser");
  });
}

/**
 * Stub the system color scheme preference
 *
 * @param {boolean} prefersDark - Whether the system prefers dark mode
 * @returns {Object} - The stub object
 */
function stubSystemColorScheme(preferredColorScheme = "light") {
  return {
    onBeforeLoad: (win) => {
      cy.stub(win, "matchMedia").callsFake((query) => {
        return {
          matches: query === `(prefers-color-scheme: ${preferredColorScheme})`,
          media: query,
          addEventListener: cy.stub(),
          removeEventListener: cy.stub(),
          addListener: cy.stub(), // deprecated but sometimes needed
          removeListener: cy.stub(),
          dispatchEvent: cy.stub(),
        };
      });
    },
  };
}