-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: PasswordVerifier spec (#29995)
Co-authored-by: juliajforesti <[email protected]>
- Loading branch information
1 parent
1b42dfc
commit 781ed63
Showing
17 changed files
with
402 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/ui-client/src/components/PasswordVerifier/PasswordVerifier.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { PasswordVerifier } from './PasswordVerifier'; | ||
|
||
type Response = { | ||
enabled: boolean; | ||
policy: [ | ||
name: string, | ||
value?: | ||
| { | ||
[x: string]: number; | ||
} | ||
| undefined, | ||
][]; | ||
}; | ||
|
||
export default { | ||
title: 'Components/PasswordVerifier', | ||
component: PasswordVerifier, | ||
} as ComponentMeta<typeof PasswordVerifier>; | ||
|
||
const response: Response = { | ||
enabled: true, | ||
policy: [ | ||
['get-password-policy-minLength', { minLength: 10 }], | ||
['get-password-policy-forbidRepeatingCharactersCount', { maxRepeatingChars: 3 }], | ||
['get-password-policy-mustContainAtLeastOneLowercase'], | ||
['get-password-policy-mustContainAtLeastOneUppercase'], | ||
['get-password-policy-mustContainAtLeastOneNumber'], | ||
['get-password-policy-mustContainAtLeastOneSpecialCharacter'], | ||
], | ||
}; | ||
|
||
const Wrapper = mockAppRoot() | ||
.withEndpoint('GET', '/v1/pw.getPolicy', () => response) | ||
.build(); | ||
|
||
export const Default: ComponentStory<typeof PasswordVerifier> = (args) => ( | ||
<Wrapper> | ||
<PasswordVerifier {...args} /> | ||
</Wrapper> | ||
); | ||
|
||
Default.storyName = 'PasswordVerifier'; | ||
Default.args = { | ||
password: 'asd', | ||
}; |
47 changes: 27 additions & 20 deletions
47
packages/ui-client/src/components/PasswordVerifier/PasswordVerifier.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
import { Box } from '@rocket.chat/fuselage'; | ||
import { Box, Skeleton } from '@rocket.chat/fuselage'; | ||
import { useUniqueId } from '@rocket.chat/fuselage-hooks'; | ||
import { useVerifyPassword } from '@rocket.chat/ui-contexts'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { PasswordVerifierItemCorrect } from './PasswordVerifierCorrect'; | ||
import { PasswordVerifierItemInvalid } from './PasswordVerifierInvalid'; | ||
import { PasswordVerifierItem } from './PasswordVerifierItem'; | ||
|
||
type PasswordVerifierProps = { | ||
password: string; | ||
id?: string; | ||
}; | ||
|
||
export const PasswordVerifier = ({ password }: PasswordVerifierProps) => { | ||
export const PasswordVerifier = ({ password, id }: PasswordVerifierProps) => { | ||
const { t } = useTranslation(); | ||
const uniqueId = useUniqueId(); | ||
|
||
const passwordVerifications = useVerifyPassword(password); | ||
const { data: passwordVerifications, isLoading } = useVerifyPassword(password); | ||
|
||
if (!passwordVerifications.length) { | ||
return <></>; | ||
if (isLoading) { | ||
return <Skeleton data-testid='password-verifier-skeleton' w='full' mbe={8} />; | ||
} | ||
|
||
if (!passwordVerifications?.length) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box display='flex' flexDirection='column' mbs={8}> | ||
<Box mbe={8} fontScale='c2'> | ||
{t('Password_must_have')} | ||
</Box> | ||
<Box display='flex' flexWrap='wrap'> | ||
{passwordVerifications.map(({ isValid, limit, name }) => | ||
isValid ? ( | ||
<PasswordVerifierItemCorrect key={name} text={t(`${name}-label`, { limit })} /> | ||
) : ( | ||
<PasswordVerifierItemInvalid key={name} text={t(`${name}-label`, { limit })} /> | ||
), | ||
)} | ||
<> | ||
<span id={id} hidden> | ||
{t('Password_Policy_Aria_Description')} | ||
</span> | ||
<Box display='flex' flexDirection='column' mbs={8}> | ||
<Box mbe={8} fontScale='c2' id={uniqueId} aria-hidden> | ||
{t('Password_must_have')} | ||
</Box> | ||
<Box display='flex' flexWrap='wrap' role='list' aria-labelledby={uniqueId}> | ||
{passwordVerifications.map(({ isValid, limit, name }) => ( | ||
<PasswordVerifierItem key={name} text={t(`${name}-label`, { limit })} isValid={isValid} aria-invalid={!isValid} /> | ||
))} | ||
</Box> | ||
</Box> | ||
</Box> | ||
</> | ||
); | ||
}; |
5 changes: 0 additions & 5 deletions
5
packages/ui-client/src/components/PasswordVerifier/PasswordVerifierCorrect.tsx
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/ui-client/src/components/PasswordVerifier/PasswordVerifierInvalid.tsx
This file was deleted.
Oops, something went wrong.
50 changes: 38 additions & 12 deletions
50
packages/ui-client/src/components/PasswordVerifier/PasswordVerifierItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
import { Box, Icon } from '@rocket.chat/fuselage'; | ||
import { AllHTMLAttributes, ComponentProps } from 'react'; | ||
|
||
const variants: { | ||
[key: string]: { | ||
icon: ComponentProps<typeof Icon>['name']; | ||
color: string; | ||
}; | ||
} = { | ||
success: { | ||
icon: 'success-circle', | ||
color: 'status-font-on-success', | ||
}, | ||
error: { | ||
icon: 'error-circle', | ||
color: 'status-font-on-danger', | ||
}, | ||
}; | ||
|
||
export const PasswordVerifierItem = ({ | ||
text, | ||
color, | ||
icon, | ||
}: { | ||
text: string; | ||
color: 'status-font-on-success' | 'status-font-on-danger'; | ||
icon: 'success-circle' | 'error-circle'; | ||
}) => ( | ||
<Box display='flex' flexBasis='50%' alignItems='center' mbe={8} fontScale='c1' color={color}> | ||
<Icon name={icon} color={color} size='x16' mie={4} /> | ||
{text} | ||
</Box> | ||
); | ||
isValid, | ||
...props | ||
}: { text: string; isValid: boolean } & Omit<AllHTMLAttributes<HTMLElement>, 'is'>) => { | ||
const { icon, color } = variants[isValid ? 'success' : 'error']; | ||
return ( | ||
<Box | ||
display='flex' | ||
flexBasis='50%' | ||
alignItems='center' | ||
mbe={8} | ||
fontScale='c1' | ||
color={color} | ||
role='listitem' | ||
aria-label={text} | ||
{...props} | ||
> | ||
<Icon name={icon} color={color} size='x16' mie={4} /> | ||
<span aria-hidden>{text}</span> | ||
</Box> | ||
); | ||
}; |
Oops, something went wrong.