Skip to content

Commit

Permalink
fix(web/react-native/ui): use translated strings and censorContactMet…
Browse files Browse the repository at this point in the history
…hod for VerifyUser screen (#5034)

* fix(vue): use translated strings for VerifyUser screen

* Create cyan-taxis-explode.md

* update test and snapshots

* fix tests

* undo test changes

* Update usage of contactCensorInformation across angular, vue, react

* update react-native usage of censorContactInformation

* update changeset

* More refactor of censorContactInformation -> censorContactMethod util

* use radioField instead of checkboxField since they are radios

* fix imports, use renamed censorContactMethod

* update jest coverage :(

* Update .changeset/cyan-taxis-explode.md

* Update .changeset/cyan-taxis-explode.md

* fix authenticator snapshot to show unverified attribute correctly

* bring jest coverage back up with test for missing attribute value
  • Loading branch information
hbuchel authored Feb 28, 2024
1 parent 4eae32e commit 1d9c5c8
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 106 deletions.
13 changes: 13 additions & 0 deletions .changeset/cyan-taxis-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@aws-amplify/ui": patch
"@aws-amplify/ui-angular": patch
"@aws-amplify/ui-react": patch
"@aws-amplify/ui-react-native": patch
"@aws-amplify/ui-vue": patch
---

fix(web/react-native/ui): use translated strings for VerifyUser screen and use censorContactMethod util

**ui/Angular/React/Vue/ReactNative:** adds a `censorContactMethod()` utility to the `ui` package and refactors the VerifyUser screen in Angular, React, Vue, and ReactNative packages to use this utility.

**Vue:** Fixes an issue where translated strings were not being properly used for the VerifyUser screen. Additionally, removes duplicate "verify" id that was on multiple elements.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ <h3 class="amplify-heading amplify-heading--3">{{ this.headerText }}</h3>
[value]="unverifiedUserAttribute.key"
[id]="labelId"
/>
<label [for]="labelId">{{ getLabel(unverifiedUserAttribute.key) }}</label>
<label [for]="labelId">{{
getLabel(unverifiedUserAttribute.key, unverifiedUserAttribute.value)
}}</label>
</div>

<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
translate,
authenticatorTextUtil,
UnverifiedUserAttributes,
censorContactMethod,
ContactMethod,
} from '@aws-amplify/ui';
import { AuthenticatorService } from '../../../../services/authenticator.service';
import { getAttributeMap } from '../../../../common';
Expand Down Expand Up @@ -48,10 +50,15 @@ export class VerifyUserComponent implements OnInit {
this.unverifiedUserAttributes = actorState.context.unverifiedUserAttributes;
}

getLabel(attr: keyof UnverifiedUserAttributes): string {
getLabel(attr: keyof UnverifiedUserAttributes, value: string): string {
const attributeMap = getAttributeMap();
const { label } = attributeMap[attr];
return translate(label);
const translatedTypeLabel = translate(label);
const censoredAttributeValue = censorContactMethod(
label as ContactMethod,
value
);
return `${translatedTypeLabel}: ${censoredAttributeValue}`;
}

onInput(event: Event): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React from 'react';
import { censorAllButFirstAndLast, censorPhoneNumber } from '@aws-amplify/ui';
import { censorContactMethod, ContactMethod } from '@aws-amplify/ui';

import { Radio, RadioGroup } from '../../../primitives';
import { DefaultRadioFormFieldsProps } from './types';

const censorContactInformation = (name: string, value: string): string => {
let censoredVal = value;
if (name === 'email') {
const splitEmail = value.split('@');
const censoredName = censorAllButFirstAndLast(splitEmail[0]);
interface AttributeMap {
email: ContactMethod;
phone_number: ContactMethod;
}

censoredVal = `${censoredName}@${splitEmail[1]}`;
} else if (name === 'phone_number') {
censoredVal = censorPhoneNumber(value);
}
return censoredVal;
const attributeMap: AttributeMap = {
email: 'Email',
phone_number: 'Phone Number',
};

const DefaultRadioFormFields = ({
Expand All @@ -26,18 +23,22 @@ const DefaultRadioFormFields = ({
}: DefaultRadioFormFieldsProps): JSX.Element => {
return (
<RadioGroup disabled={isPending} style={style}>
{(fields ?? []).map(({ name, value, ...props }) => (
<Radio
{...props}
key={value}
// value has to be name, because Auth is only interested in the
// string "email" or "phone_number", not the actual value
value={name}
label={censorContactInformation(name, value)}
labelStyle={fieldLabelStyle}
style={fieldContainerStyle}
/>
))}
{(fields ?? []).map(({ name, value, ...props }) => {
const attributeType = attributeMap[name as keyof AttributeMap];

return (
<Radio
{...props}
key={value}
// value has to be name, because Auth is only interested in the
// string "email" or "phone_number", not the actual value
value={name}
label={censorContactMethod(attributeType, value)}
labelStyle={fieldLabelStyle}
style={fieldContainerStyle}
/>
);
})}
</RadioGroup>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import {
defaultFormFieldOptions,
censorAllButFirstAndLast,
censorPhoneNumber,
censorContactMethod,
ContactMethod,
translate,
UnverifiedUserAttributes,
Expand All @@ -27,36 +26,20 @@ const {
getAccountRecoveryInfoText,
} = authenticatorTextUtil;

const censorContactInformation = (
type: ContactMethod,
value: string
): string => {
const translated = translate(type);
let newVal = value;

if (type === 'Phone Number') {
newVal = censorPhoneNumber(value);
} else if (type === 'Email') {
const splitEmail = value.split('@');
const censoredName = censorAllButFirstAndLast(splitEmail[0]);

newVal = `${censoredName}@${splitEmail[1]}`;
}

return `${translated}: ${newVal}`;
};

const generateRadioGroup = (
attributes: UnverifiedUserAttributes
): JSX.Element[] => {
return Object.entries(attributes).map(([key, value]: [string, string]) => (
<Radio name="unverifiedAttr" value={key} key={key}>
{censorContactInformation(
(defaultFormFieldOptions[key] as { label: ContactMethod }).label,
value
)}
</Radio>
));
return Object.entries(attributes).map(([key, value]: [string, string]) => {
const verificationType = (
defaultFormFieldOptions[key] as { label: ContactMethod }
).label;
return (
<Radio name="unverifiedAttr" value={key} key={key}>
{translate(verificationType)}:{' '}
{censorContactMethod(verificationType, value)}
</Radio>
);
});
};

export const VerifyUser = ({
Expand Down
31 changes: 30 additions & 1 deletion packages/ui/src/helpers/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { censorAllButFirstAndLast, censorPhoneNumber } from '..';
import {
censorAllButFirstAndLast,
censorContactMethod,
censorEmail,
censorPhoneNumber,
} from '..';

describe('censorAllButFirstAndLast', () => {
it('should only censor characters between the first and last indexes of a string', () => {
Expand Down Expand Up @@ -53,3 +58,27 @@ describe('censorPhoneNumber', () => {
expect(phone6).toEqual('+12');
});
});

describe('censorEmail', () => {
it('should return a censored email', () => {
const censoredEmail = censorEmail('[email protected]');
expect(censoredEmail).toEqual('e*****[email protected]');
});
});

describe('censorContactMethod', () => {
it('should return a censored phone number when passed type of Phone Number', () => {
const censoredPhoneNumber = censorContactMethod(
'Phone Number',
'+11231234567'
);
expect(censoredPhoneNumber).toEqual('********4567');
});
it('should return a censored email when passed type of Email', () => {
const censoredPhoneNumber = censorContactMethod(
'Email',
'[email protected]'
);
expect(censoredPhoneNumber).toEqual('e*****[email protected]');
});
});
19 changes: 19 additions & 0 deletions packages/ui/src/helpers/authenticator/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ALLOWED_SPECIAL_CHARACTERS, emailRegex } from './constants';
import type { ContactMethod } from '../../types';

// replaces all characters in a string with '*', except for the first and last char
export const censorAllButFirstAndLast = (value: string): string => {
Expand Down Expand Up @@ -26,6 +27,24 @@ export const censorPhoneNumber = (val: string): string => {
return split.join('');
};

// censors all but the first and last of the name of an email and keeps domain
export const censorEmail = (val: string): string => {
const splitEmail = val.split('@');
const censoredName = censorAllButFirstAndLast(splitEmail[0]);

return `${censoredName}@${splitEmail[1]}`;
};

// based on the ContactMethod type, returns a censored contact value
export const censorContactMethod = (
type: ContactMethod,
value: string
): string => {
return type === 'Phone Number'
? censorPhoneNumber(value)
: censorEmail(value);
};

export const hasSpecialChars = (password: string) =>
ALLOWED_SPECIAL_CHARACTERS.some((char) => password.includes(char));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,19 +1265,28 @@ exports[`authenticator renders verifyUser subcomponent 1`] = `
>
<label
class="amplify-flex amplify-radio"
data-amplify-label=""
data-amplify-verify-label=""
id="verify"
>
<span
class="amplify-text amplify-radio__label"
data-amplify-text=""
>
Email: t**[email protected]
</span>
<input
aria-invalid="false"
class="amplify-input amplify-field-group__control amplify-visually-hidden amplify-radio__input"
data-amplify-input=""
data-amplify-verify-input=""
id="verify"
name="unverifiedAttr"
type="radio"
value="email"
Expand All @@ -1293,19 +1302,10 @@ exports[`authenticator renders verifyUser subcomponent 1`] = `
</span>
<span
class="amplify-text amplify-radio__label"
data-amplify-text=""
>
Email
</span>
</label>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,28 @@ exports[`VerifyUser renders as expected 1`] = `
>
<label
class="amplify-flex amplify-radio"
data-amplify-label=""
data-amplify-verify-label=""
id="verify"
>
<span
class="amplify-text amplify-radio__label"
data-amplify-text=""
>
Email: t**[email protected]
</span>
<input
aria-invalid="false"
class="amplify-input amplify-field-group__control amplify-visually-hidden amplify-radio__input"
data-amplify-input=""
data-amplify-verify-input=""
id="verify"
name="unverifiedAttr"
type="radio"
value="email"
Expand All @@ -72,19 +81,10 @@ exports[`VerifyUser renders as expected 1`] = `
</span>
<span
class="amplify-text amplify-radio__label"
data-amplify-text=""
>
Email
</span>
</label>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions packages/vue/src/components/__tests__/authenticator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AuthEvent,
AuthInterpreter,
AuthMachineState,
UnverifiedUserAttributes,
} from '@aws-amplify/ui';

import { components } from '../../../global-spec';
Expand Down Expand Up @@ -59,6 +60,10 @@ const setupState = {
matches: (state: string) => state === 'setup',
} as unknown as AuthMachineState;

const unverifiedUserAttributes: UnverifiedUserAttributes = {
email: '[email protected]',
};

const mockStateRef = ref(idleState) as unknown as Ref<AuthMachineState>;

const sendSpy = jest.fn();
Expand Down Expand Up @@ -181,6 +186,7 @@ describe('authenticator', () => {
user,
totpSecretCode:
route === 'setupTotp' ? 'totp-mock-secret-code' : undefined,
unverifiedUserAttributes,
})
);

Expand Down
Loading

0 comments on commit 1d9c5c8

Please sign in to comment.