Skip to content

fix(site): fix validation server error on change password form #15170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion site/src/pages/ResetPasswordPage/ChangePasswordPage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const WrongConfirmationPassword: Story = {
},
};

export const ServerError: Story = {
export const GeneralServerError: Story = {
play: async ({ canvasElement }) => {
const serverError =
"New password should be different from the old password";
Expand All @@ -71,3 +71,28 @@ export const ServerError: Story = {
await canvas.findByText(serverError);
},
};

export const ValidationServerError: Story = {
play: async ({ canvasElement }) => {
const error = mockApiError({
message: "Invalid password.",
validations: [
{
field: "password",
detail:
"insecure password, try including more special characters, using uppercase letters, using numbers or using a longer password",
},
],
});
spyOn(API, "changePasswordWithOTP").mockRejectedValueOnce(error);
const canvas = within(canvasElement);
const user = userEvent.setup();
const newPasswordInput = await canvas.findByLabelText("Password *");
await user.type(newPasswordInput, "password");
const confirmPasswordInput =
await canvas.findByLabelText("Confirm password *");
await user.type(confirmPasswordInput, "password");
await user.click(canvas.getByRole("button", { name: /reset password/i }));
await canvas.findByText(error.response.data.message);
},
};
6 changes: 4 additions & 2 deletions site/src/pages/ResetPasswordPage/ChangePasswordPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Interpolation, Theme } from "@emotion/react";
import LoadingButton from "@mui/lab/LoadingButton";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
import { isApiError, isApiValidationError } from "api/errors";
import { changePasswordWithOTP } from "api/queries/users";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { CustomLogo } from "components/CustomLogo/CustomLogo";
Expand Down Expand Up @@ -64,7 +65,7 @@ const ChangePasswordPage: FC<ChangePasswordChangeProps> = ({ redirect }) => {
}
},
});
const getFieldHelpers = getFormHelpers(form);
const getFieldHelpers = getFormHelpers(form, changePasswordMutation.error);

return (
<>
Expand All @@ -86,7 +87,8 @@ const ChangePasswordPage: FC<ChangePasswordChangeProps> = ({ redirect }) => {
>
Choose a new password
</h1>
{changePasswordMutation.error ? (
{changePasswordMutation.error &&
!isApiValidationError(changePasswordMutation.error) ? (
<ErrorAlert
error={changePasswordMutation.error}
css={{ marginBottom: 24 }}
Expand Down
5 changes: 5 additions & 0 deletions site/src/theme/mui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ export const components = {
sx: {
marginLeft: 0,
marginTop: 1,
"&::first-letter": {
// Server errors are returned in all lowercase. To display them as
// field errors in the UI, we capitalize the first letter.
textTransform: "uppercase",
},
},
},
},
Expand Down
Loading