From 9ff1492f9f7f96754b618eabc7d9e69600fa9ca5 Mon Sep 17 00:00:00 2001 From: Oleg Chendighelean Date: Tue, 1 Oct 2024 17:25:53 +0100 Subject: [PATCH] Update usePasswordValidation tests --- .../ChangePasswordForm.test.tsx | 6 ++-- .../ChangePassword/ChangePasswordForm.tsx | 4 +-- .../password/EnterAndConfirmPassword.test.tsx | 5 ---- .../password/EnterAndConfirmPassword.tsx | 3 +- .../password/EnterPassword.test.tsx | 5 ---- .../restoreSecretKey/RestoreSecretKey.tsx | 2 +- apps/desktop/src/components/PasswordInput.tsx | 24 +++++---------- .../src/components/SendFlow/SignButton.tsx | 2 +- apps/desktop/src/setupTests.tsx | 8 ----- .../MasterPasswordModal.tsx | 1 - .../ChangePasswordMenu.test.tsx | 2 +- .../ChangePasswordMenu/ChangePasswordMenu.tsx | 4 +-- .../ImportWallet/ImportBackupTab.tsx | 2 +- .../Onboarding/ImportWallet/SecretKeyTab.tsx | 2 +- .../SetupPassword/SetupPassword.tsx | 3 +- .../PasswordInput/PasswordInput.tsx | 28 +++++++---------- .../src/components/SendFlow/SignButton.tsx | 2 +- .../src/hooks/usePasswordValidation.test.tsx | 29 +++++++++++------- .../src/hooks/usePasswordValidation.tsx | 30 +++++++++++-------- pnpm-lock.yaml | 26 ++++++++-------- 20 files changed, 80 insertions(+), 108 deletions(-) diff --git a/apps/desktop/src/components/ChangePassword/ChangePasswordForm.test.tsx b/apps/desktop/src/components/ChangePassword/ChangePasswordForm.test.tsx index dd8afc071d..d9f239e32d 100644 --- a/apps/desktop/src/components/ChangePassword/ChangePasswordForm.test.tsx +++ b/apps/desktop/src/components/ChangePassword/ChangePasswordForm.test.tsx @@ -55,7 +55,7 @@ describe("ChangePassword Form", () => { await waitFor(() => { expect(screen.getByTestId("new-password-error")).toHaveTextContent( - "Your password must be at least 12 characters long" + "Password must be at least 12 characters long" ); }); }); @@ -104,8 +104,8 @@ describe("ChangePassword Form", () => { const newPasswordConfirmationInput = screen.getByTestId("new-password-confirmation"); fireEvent.change(currentPasswordInput, { target: { value: "myOldPassword" } }); - fireEvent.change(newPasswordInput, { target: { value: "myNewPassword" } }); - fireEvent.change(newPasswordConfirmationInput, { target: { value: "myNewPassword" } }); + fireEvent.change(newPasswordInput, { target: { value: "myNewPassword123L!" } }); + fireEvent.change(newPasswordConfirmationInput, { target: { value: "myNewPassword123L!" } }); await waitFor(() => { expect(screen.getByRole("button", { name: "Update Password" })).toBeEnabled(); diff --git a/apps/desktop/src/components/ChangePassword/ChangePasswordForm.tsx b/apps/desktop/src/components/ChangePassword/ChangePasswordForm.tsx index 876989e937..34b99db8fb 100644 --- a/apps/desktop/src/components/ChangePassword/ChangePasswordForm.tsx +++ b/apps/desktop/src/components/ChangePassword/ChangePasswordForm.tsx @@ -66,7 +66,6 @@ export const ChangePasswordForm = () => { data-testid="current-password" inputName="currentPassword" label="Current Password" - minLength={0} placeholder="Enter your current password" required="Current password is required" /> @@ -81,7 +80,7 @@ export const ChangePasswordForm = () => { { data-testid="new-password-confirmation" inputName="newPasswordConfirmation" label="Confirm New Password" - minLength={0} placeholder="Confirm new password" required="Confirmation is required" validate={(val: string) => diff --git a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.test.tsx b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.test.tsx index 1a4cc67fdb..d08a988ba8 100644 --- a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.test.tsx +++ b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.test.tsx @@ -34,11 +34,6 @@ describe("", () => { await checkPasswords(mockPassword, "password1", false); }); - test("Not meeting password policy", async () => { - render(fixture(false)); - await checkPasswords("tes", "tes", false); - }); - test("Form is loading", () => { render(fixture(true)); const submit = screen.getByRole("button", { name: /submit/i }); diff --git a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.tsx b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.tsx index 717a38e117..118ca06f61 100644 --- a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.tsx +++ b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterAndConfirmPassword.tsx @@ -42,7 +42,7 @@ export const EnterAndConfirmPassword = ({ {errors.password && {errors.password.message}} @@ -53,7 +53,6 @@ export const EnterAndConfirmPassword = ({ data-testid="confirmation" inputName="confirm" label="Confirm Password" - minLength={0} placeholder="Confirm your password" required="Confirmation is required" validate={(val: string) => diff --git a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterPassword.test.tsx b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterPassword.test.tsx index d5955ad3a9..7d6d17c547 100644 --- a/apps/desktop/src/components/Onboarding/masterPassword/password/EnterPassword.test.tsx +++ b/apps/desktop/src/components/Onboarding/masterPassword/password/EnterPassword.test.tsx @@ -24,11 +24,6 @@ describe("", () => { await checkPasswords(mockPassword, true); }); - test("Not meeting password policy", async () => { - render(fixture(false)); - await checkPasswords("tes", false); - }); - test("Form is loading", () => { render(fixture(true)); diff --git a/apps/desktop/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx b/apps/desktop/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx index 45d93f07c9..4ac2842376 100644 --- a/apps/desktop/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx +++ b/apps/desktop/src/components/Onboarding/restoreSecretKey/RestoreSecretKey.tsx @@ -61,7 +61,7 @@ export const RestoreSecretKey = ({ goToStep }: { goToStep: (step: OnboardingStep {isEncrypted && ( - + {errors.password && {errors.password.message}} )} diff --git a/apps/desktop/src/components/PasswordInput.tsx b/apps/desktop/src/components/PasswordInput.tsx index 0650e1e313..63f1a0843a 100644 --- a/apps/desktop/src/components/PasswordInput.tsx +++ b/apps/desktop/src/components/PasswordInput.tsx @@ -6,22 +6,20 @@ import { type InputProps, InputRightElement, } from "@chakra-ui/react"; -import { usePasswordValidation } from "@umami/components"; +import { DEFAULT_MIN_LENGTH, usePasswordValidation } from "@umami/components"; import { useState } from "react"; import { type FieldValues, type Path, type RegisterOptions, useFormContext } from "react-hook-form"; import { EyeIcon, EyeSlashIcon } from "../assets/icons"; import colors from "../style/colors"; -const MIN_LENGTH = 12; - // is needed to be compatible with the useForm's type parameter (FormData) // > makes sure that we can pass in only valid inputName that exists in FormData type PasswordInputProps> = { inputName: U; label?: string; placeholder?: string; - isCheckPasswordStrengthEnabled?: boolean; + isStrengthCheckEnabled?: boolean; required?: string | boolean; minLength?: RegisterOptions["minLength"]; validate?: (val: string) => string | boolean; @@ -33,8 +31,8 @@ export const PasswordInput = >({ label = "Password", placeholder = "Enter your password", required = "Password is required", - isCheckPasswordStrengthEnabled = false, - minLength = MIN_LENGTH, + isStrengthCheckEnabled = false, + minLength = DEFAULT_MIN_LENGTH, validate, ...rest }: PasswordInputProps) => { @@ -43,18 +41,19 @@ export const PasswordInput = >({ const { validatePasswordStrength, PasswordStrengthBar } = usePasswordValidation({ color: colors.gray[500], inputName, + minLength, }); const handleValidate = (val: string) => { if (validate) { const validationResult = validate(val); - if (isCheckPasswordStrengthEnabled && validationResult === true) { + if (isStrengthCheckEnabled && validationResult === true) { return validatePasswordStrength(val); } return validationResult; - } else if (isCheckPasswordStrengthEnabled) { + } else if (isStrengthCheckEnabled) { return validatePasswordStrength(val); } }; @@ -70,13 +69,6 @@ export const PasswordInput = >({ type={showPassword ? "text" : "password"} {...register(inputName, { required, - minLength: - minLength && required - ? { - value: minLength, - message: `Your password must be at least ${minLength} characters long`, - } - : undefined, validate: handleValidate, })} {...rest} @@ -91,7 +83,7 @@ export const PasswordInput = >({ - {isCheckPasswordStrengthEnabled && PasswordStrengthBar} + {isStrengthCheckEnabled && PasswordStrengthBar} ); }; diff --git a/apps/desktop/src/components/SendFlow/SignButton.tsx b/apps/desktop/src/components/SendFlow/SignButton.tsx index 4fc6b12360..594b6c0627 100644 --- a/apps/desktop/src/components/SendFlow/SignButton.tsx +++ b/apps/desktop/src/components/SendFlow/SignButton.tsx @@ -101,7 +101,7 @@ export const SignButton = ({ - + {errors.password && {errors.password.message}} diff --git a/apps/web/src/components/Onboarding/ImportWallet/SecretKeyTab.tsx b/apps/web/src/components/Onboarding/ImportWallet/SecretKeyTab.tsx index f8fa7e2347..8d8f31d7e5 100644 --- a/apps/web/src/components/Onboarding/ImportWallet/SecretKeyTab.tsx +++ b/apps/web/src/components/Onboarding/ImportWallet/SecretKeyTab.tsx @@ -52,7 +52,7 @@ export const SecretKeyTab = () => { {errors.secretKey && {errors.secretKey.message}} - {isEncrypted && } + {isEncrypted && }