<reponame>scorpion897657/DataVisible<filename>client/cypress/integration/user/create_user_spec.js<gh_stars>0describe("Create User", () => {
  beforeEach(() => {
    cy.login();
    cy.visit("/users/new");
  });

  const fillUserFormAndSubmit = (name, email) => {
    cy.getByTestId("CreateUserDialog").within(() => {
      cy.getByTestId("Name").type(name);
      cy.getByTestId("Email").type(email);
    });
    cy.getByTestId("SaveUserButton").click();
  };

  it("creates a new user", () => {
    // delete existing "kenaa@example.com"
    cy.request("GET", "api/users?q=new-user")
      .then(({ body }) => body.results.filter(user => user.email === "kenaa@example.com"))
      .each(user => cy.request("DELETE", `api/users/${user.id}`));

    fillUserFormAndSubmit("New User", "lyhxr@example.com");

    cy.getByTestId("CreateUserErrorAlert").should("contain", "Email already taken");

    fillUserFormAndSubmit("{selectall}New User", "{selectall}new-user@redash.io");
    cy.contains("Saved.");
  });
});
