fleet/frontend/components/forms/RegistrationForm/AdminDetails/AdminDetails.tests.jsx
Scott Gress dc4fd676c5
Update password requirements check when setting up (#32261)
for #31876 

# Details

This PR updates the validation for password requirements to be
consistent in all places, and to show more specific error messages when
the entered password does not meet the requirements.

Previously the `valid_password` helper just returned a boolean
indicating whether a password was valid. It now returns an object with
`isValid`, `error` and `error_code` so that different types of password
issues can be surfaced. This allows us to continue having a single
source of truth for password validation, while providing more helpful
error messages when a password doesn't meet our criteria.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [X] Added/updated automated tests
- [X] QA'd all new/changed functionality manually
  - [X] First time setup (adding the initial user)
  - [X] Add user in settings -> manage users
  - [X] Changer user password in settings -> manage users
  - [X] Password reset form
2025-08-26 16:59:05 -05:00

131 lines
4.3 KiB
JavaScript

import React from "react";
import { render, screen } from "@testing-library/react";
import { renderWithSetup } from "test/test-utils";
import AdminDetails from "components/forms/RegistrationForm/AdminDetails";
describe("AdminDetails - form", () => {
const onSubmitSpy = jest.fn();
it("renders", () => {
render(<AdminDetails handleSubmit={onSubmitSpy} />);
expect(screen.getByLabelText("Password")).toBeInTheDocument();
expect(screen.getByLabelText("Confirm password")).toBeInTheDocument();
expect(
screen.getByRole("textbox", { name: "Full name" })
).toBeInTheDocument();
expect(screen.getByRole("textbox", { name: "Email" })).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Next" })).toBeInTheDocument();
});
it("validates missing fields", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.click(screen.getByRole("button", { name: "Next" }));
expect(onSubmitSpy).not.toHaveBeenCalled();
expect(screen.getByText("Email must be present")).toBeInTheDocument();
expect(screen.getByText("Password must be present")).toBeInTheDocument();
expect(
screen.getByText("Password confirmation must be present")
).toBeInTheDocument();
expect(screen.getByText("Full name must be present")).toBeInTheDocument();
});
it("validates the email field", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.type(
screen.getByRole("textbox", { name: "Email" }),
"invalid-email"
);
await user.click(screen.getByRole("button", { name: "Next" }));
// then
expect(onSubmitSpy).not.toHaveBeenCalled();
expect(screen.getByText("Email must be a valid email")).toBeInTheDocument();
});
it("validates the password fields match", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.type(screen.getByLabelText("Password"), "p@ssw0rd");
await user.type(screen.getByLabelText("Confirm password"), "password123");
await user.click(screen.getByRole("button", { name: "Next" }));
// then
expect(onSubmitSpy).not.toHaveBeenCalled();
expect(
screen.getByText("Password confirmation does not match password")
).toBeInTheDocument();
});
it("validates the password is not too long", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.type(
screen.getByLabelText("Password"),
"asasasasasasasasasasasasasasasasasasasasasasasas1!"
);
await user.type(
screen.getByLabelText("Confirm password"),
"asasasasasasasasasasasasasasasasasasasasasasasas1!"
);
await user.click(screen.getByRole("button", { name: "Next" }));
// then
expect(onSubmitSpy).not.toHaveBeenCalled();
expect(
screen.getByText("Password is over the character limit")
).toBeInTheDocument();
});
it("validates the password field", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.type(screen.getByLabelText("Password"), "invalidpassw0rd");
await user.type(
screen.getByLabelText("Confirm password"),
"invalidpassw0rd"
);
await user.click(screen.getByRole("button", { name: "Next" }));
// then
expect(onSubmitSpy).not.toHaveBeenCalled();
expect(
screen.getByText("Password must meet the criteria below")
).toBeInTheDocument();
});
it("submits the form when valid", async () => {
const { user } = renderWithSetup(
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
);
await user.type(
screen.getByRole("textbox", { name: "Email" }),
"hi@gnar.dog"
);
await user.type(screen.getByLabelText("Password"), "password123#");
await user.type(screen.getByLabelText("Confirm password"), "password123#");
await user.type(
screen.getByRole("textbox", { name: "Full name" }),
"Gnar Dog"
);
await user.click(screen.getByRole("button", { name: "Next" }));
// then
expect(onSubmitSpy).toHaveBeenCalledWith({
email: "hi@gnar.dog",
name: "Gnar Dog",
password: "password123#",
password_confirmation: "password123#",
});
});
});