1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Fix #19608 - Make account name validation more strict (#19616)

This commit is contained in:
David Walsh 2023-06-22 09:07:56 -05:00 committed by GitHub
parent ed702af8ce
commit 329e4b75ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -7,7 +7,7 @@ export function getAccountNameErrorMessage(
defaultAccountName,
) {
const isDuplicateAccountName = accounts.some(
(item) => item.name === newAccountName,
(item) => item.name.toLowerCase() === newAccountName.toLowerCase(),
);
const isEmptyAccountName = newAccountName === '';
@ -25,7 +25,7 @@ export function getAccountNameErrorMessage(
const isReservedAccountName = reservedRegEx.test(newAccountName);
const isValidAccountName =
newAccountName === defaultAccountName || // What is written in the text field is the same as the placeholder
newAccountName.toLowerCase() === defaultAccountName.toLowerCase() || // What is written in the text field is the same as the placeholder
(!isDuplicateAccountName && !isReservedAccountName && !isEmptyAccountName);
let errorMessage;

View File

@ -0,0 +1,47 @@
import { getAccountNameErrorMessage } from './accounts';
const mockAccounts = [{ name: 'Account 1' }, { name: 'Account 2' }];
const mockLocalization = { t: jest.fn().mockReturnValue('Account') };
describe('Accounts', () => {
it('does not allow duplicate names', () => {
const { isValidAccountName } = getAccountNameErrorMessage(
mockAccounts,
mockLocalization,
'Account 2',
'Account 3',
);
expect(isValidAccountName).toBe(false);
});
it('does not allow reserved name patterns', () => {
const { isValidAccountName } = getAccountNameErrorMessage(
mockAccounts,
mockLocalization,
'Account 7',
'Account 3',
);
expect(isValidAccountName).toBe(false);
});
it('does not allow reserved name patterns in lowercase', () => {
const { isValidAccountName } = getAccountNameErrorMessage(
mockAccounts,
mockLocalization,
'account 7',
'Account 3',
);
expect(isValidAccountName).toBe(false);
});
it('allows proposed name in lowercase', () => {
const { isValidAccountName } = getAccountNameErrorMessage(
mockAccounts,
mockLocalization,
'account 3',
'Account 3',
);
expect(isValidAccountName).toBe(true);
});
});