From 329e4b75edfc741ffe64c27ccad5f22d8db1cd4c Mon Sep 17 00:00:00 2001 From: David Walsh Date: Thu, 22 Jun 2023 09:07:56 -0500 Subject: [PATCH] Fix #19608 - Make account name validation more strict (#19616) --- ui/helpers/utils/accounts.js | 4 +-- ui/helpers/utils/accounts.test.js | 47 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ui/helpers/utils/accounts.test.js diff --git a/ui/helpers/utils/accounts.js b/ui/helpers/utils/accounts.js index 4f953bf66..cb974f1c6 100644 --- a/ui/helpers/utils/accounts.js +++ b/ui/helpers/utils/accounts.js @@ -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; diff --git a/ui/helpers/utils/accounts.test.js b/ui/helpers/utils/accounts.test.js new file mode 100644 index 000000000..e3b629ae7 --- /dev/null +++ b/ui/helpers/utils/accounts.test.js @@ -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); + }); +});