Skip to content

Commit

Permalink
test(clerk-js): Update password complexity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Jun 29, 2023
1 parent e496fc3 commit ae0aad0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .changeset/forty-hornets-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('usePasswordComplexity', () => {
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('password1');
result.current.getComplexity('password1');
});

expect(result.current.password).toBe('password1');
Expand All @@ -31,7 +31,7 @@ describe('usePasswordComplexity', () => {
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('thispasswordfails');
result.current.getComplexity('thispasswordfails');
});

expect(result.current.hasFailedComplexity).toBe(true);
Expand All @@ -43,7 +43,7 @@ describe('usePasswordComplexity', () => {
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('th1sp@sswordPasses');
result.current.getComplexity('th1sp@sswordPasses');
});

expect(result.current.hasFailedComplexity).toBe(false);
Expand All @@ -55,7 +55,7 @@ describe('usePasswordComplexity', () => {
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('thispasswordfails');
result.current.getComplexity('thispasswordfails');
});

expect(result.current.failedValidations).toHaveProperty('require_uppercase');
Expand All @@ -66,7 +66,7 @@ describe('usePasswordComplexity', () => {
expect(result.current.failedValidations).not.toHaveProperty('min_length');

await act(() => {
result.current.setPassword(`thispasswordfails"`);
result.current.getComplexity(`thispasswordfails"`);
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');
Expand All @@ -89,7 +89,7 @@ describe('usePasswordComplexity', () => {
});

await act(() => {
result.current.setPassword('@');
result.current.getComplexity('@');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');
Expand All @@ -112,66 +112,48 @@ describe('usePasswordComplexity', () => {
});

await act(() => {
result.current.setPassword('[');
result.current.getComplexity('[');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');

await act(() => {
result.current.setPassword(']');
result.current.getComplexity(']');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');

await act(() => {
result.current.setPassword('[test]');
result.current.getComplexity('[test]');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');

await act(() => {
result.current.setPassword('test[]');
result.current.getComplexity('test[]');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');

await act(() => {
result.current.setPassword('[!"#$%&\'()*+,-./:;<=>?@^_`{|}~]');
result.current.getComplexity('[!"#$%&\'()*+,-./:;<=>?@^_`{|}~]');
});

expect(result.current.failedValidations).not.toHaveProperty('require_special_char');
});

it('returns an object with all validations and their status (true:passed, false:failed)', async () => {
const { wrapper } = await createFixtures();
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('thispasswordfails');
});

expect(result.current.passwordComplexity).toMatchObject({
max_length: true,
min_length: true,
require_special_char: false,
require_numbers: false,
require_lowercase: true,
require_uppercase: false,
});
});

it('returns error message with localized conjunction', async () => {
const { wrapper } = await createFixtures();
const { result } = renderHook(defaultRenderer, { wrapper });

await act(() => {
result.current.setPassword('@aP');
result.current.getComplexity('@aP');
});

expect(result.current.failedValidationsText).toBe('Your password must contain 8 or more characters and a number');

await act(() => {
result.current.setPassword('aP');
result.current.getComplexity('aP');
});

expect(result.current.failedValidationsText).toBe(
Expand Down
54 changes: 11 additions & 43 deletions packages/clerk-js/src/ui/hooks/usePasswordComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { LocalizationKey, localizationKeys, useLocalizations } from '../localization';
import { canUseListFormat } from '../utils';

// type ComplexityErrorMessages = {
// [key in keyof Partial<Omit<PasswordSettingsData, 'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn'>>]: string;
// };

export type ComplexityErrors = {
[key in keyof Partial<Omit<PasswordSettingsData, 'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn'>>]?: boolean;
};
Expand Down Expand Up @@ -91,16 +87,6 @@ export const generateErrorTextUtil = ({
return `${t(localizationKeys('unstable__errors.passwordComplexity.sentencePrefix'))} ${messageWithPrefix}`;
};

// const useTestComplexityCases = ({
// allowed_special_characters,
// }: Pick<UsePasswordComplexityConfig, 'allowed_special_characters'>) => {
// return useMemo(() => {
// return {
// testComplexityCases: createTestComplexityCases({ allowed_special_characters }),
// };
// }, [allowed_special_characters]);
// };

const validate = (password: string, config: UsePasswordComplexityConfig): ComplexityErrors => {
const { max_length, min_length, require_special_char, require_lowercase, require_numbers, require_uppercase } =
config;
Expand Down Expand Up @@ -149,35 +135,15 @@ export const usePasswordComplexity = (config: UsePasswordComplexityConfig) => {
getComplexity('');
}, []);

// const errorMessages = useMemo(
// () =>
// ({
// max_length: t(localizationKeys('unstable__errors.passwordComplexity.maximumLength', { length: max_length })),
// min_length: t(localizationKeys('unstable__errors.passwordComplexity.minimumLength', { length: min_length })),
// require_numbers: t(localizationKeys('unstable__errors.passwordComplexity.requireNumbers')),
// require_lowercase: t(localizationKeys('unstable__errors.passwordComplexity.requireLowercase')),
// require_uppercase: t(localizationKeys('unstable__errors.passwordComplexity.requireUppercase')),
// require_special_char: t(localizationKeys('unstable__errors.passwordComplexity.requireSpecialCharacter')),
// } satisfies ComplexityErrorMessages),
// [min_length],
// );

// const passwordComplexity = useMemo(() => {
// return testComplexityCases(password, {
// maxLength: max_length,
// minLength: min_length,
// });
// }, [password, min_length, max_length]);

// const hasPassedComplexity = useMemo(
// () => !!password && Object.keys(failedValidations || {}).length === 0,
// [failedValidations, password],
// );
//
// const hasFailedComplexity = useMemo(
// () => !!password && Object.keys(failedValidations || {}).length > 0,
// [failedValidations, password],
// );
const hasPassedComplexity = useMemo(
() => !!password && Object.keys(failedValidations || {}).length === 0,
[failedValidations, password],
);

const hasFailedComplexity = useMemo(
() => !!password && Object.keys(failedValidations || {}).length > 0,
[failedValidations, password],
);

const generateErrorText = useCallback(
(failedValidations: ComplexityErrors | undefined) => {
Expand Down Expand Up @@ -207,5 +173,7 @@ export const usePasswordComplexity = (config: UsePasswordComplexityConfig) => {
getComplexity,
failedValidations,
failedValidationsText,
hasFailedComplexity,
hasPassedComplexity,
};
};

0 comments on commit ae0aad0

Please sign in to comment.