diff --git a/kairon/shared/account/processor.py b/kairon/shared/account/processor.py index b006035af..cb7ab92b5 100644 --- a/kairon/shared/account/processor.py +++ b/kairon/shared/account/processor.py @@ -169,8 +169,8 @@ def add_bot( if Utility.check_empty_string(name): raise AppException("Bot Name cannot be empty or blank spaces") - if name and Utility.special_match(name): - raise AppException("Invalid name! Only letters, numbers, and underscores (_) are allowed.") + if name and Utility.contains_special_characters(name): + raise AppException("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).") if not Utility.check_character_limit(name): raise AppException("Bot Name cannot be more than 60 characters.") @@ -225,8 +225,8 @@ def update_bot(name: Text, bot: Text): if Utility.check_empty_string(name): raise AppException('Name cannot be empty') - if name and Utility.special_match(name): - raise AppException("Invalid name! Only letters, numbers, and underscores (_) are allowed.") + if name and Utility.contains_special_characters(name): + raise AppException("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).") if not Utility.check_character_limit(name): raise AppException("Bot Name cannot be more than 60 characters.") diff --git a/kairon/shared/utils.py b/kairon/shared/utils.py index f1849d4f2..8785bdfcd 100644 --- a/kairon/shared/utils.py +++ b/kairon/shared/utils.py @@ -683,6 +683,17 @@ def special_match(strg, search=re.compile(r"[^a-zA-Z0-9_]").search): """ return bool(search(strg)) + @staticmethod + def contains_special_characters(strg, search=re.compile(r"[^a-zA-Z0-9 _-]").search): + """ + Check if the string contains special characters other than allowed ones (space, _ and -). + + :param strg: text value + :param search: search pattern + :return: boolean + """ + return bool(search(strg)) + @staticmethod def list_directories(path: Text): """ diff --git a/tests/unit_test/api/api_processor_test.py b/tests/unit_test/api/api_processor_test.py index a93c1bb3d..23946c2b2 100644 --- a/tests/unit_test/api/api_processor_test.py +++ b/tests/unit_test/api/api_processor_test.py @@ -107,19 +107,22 @@ def test_add_bot_with_invalid_name(self): account = pytest.account user = "fshaikh@digite.com" is_new_account = True - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): name = "test#21" AccountProcessor.add_bot(name=name, account=account, user=user, is_new_account=is_new_account) - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): name = "test@3" AccountProcessor.add_bot(name=name, account=account, user=user, is_new_account=is_new_account) - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): - name = " test 5" + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): + name = "test&5" AccountProcessor.add_bot(name=name, account=account, user=user, is_new_account=is_new_account) def test_update_bot_with_invalid_name(self): @@ -129,18 +132,21 @@ def test_update_bot_with_invalid_name(self): user = "fshaikh@digite.com" is_new_account = True bot = "test_bot" - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): name = "test?17" AccountProcessor.update_bot(name=name, bot=bot) - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): - name = "test-7" + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): + name = "test(7)" AccountProcessor.update_bot(name=name, bot=bot) - with pytest.raises(AppException, - match=re.escape("Invalid name! Only letters, numbers, and underscores (_) are allowed.")): + with pytest.raises( + AppException, + match=re.escape("Invalid name! Use only letters, numbers, spaces, hyphens (-), and underscores (_).")): name = "18" AccountProcessor.update_bot(name=name, bot=bot) diff --git a/tests/unit_test/utility_test.py b/tests/unit_test/utility_test.py index f745815f4..a93a9d81c 100644 --- a/tests/unit_test/utility_test.py +++ b/tests/unit_test/utility_test.py @@ -2806,6 +2806,22 @@ def test_special_match(self): def test_special_match_without_special_character(self): assert Utility.special_match(strg="Testing123") is False + def test_contains_special_characters(self): + assert Utility.contains_special_characters(strg="Testing@123") is True + + def test_contains_special_characters_with_space(self): + assert Utility.contains_special_characters(strg="Testing 123") is False + + def test_contains_special_characters_with_hyphens(self): + assert Utility.contains_special_characters(strg="Testing-123") is False + + def test_contains_special_characters_with_underscores(self): + assert Utility.contains_special_characters(strg="Testing_123") is False + + def test_contains_special_characters_with_ampersand(self): + assert Utility.contains_special_characters(strg="Tom & Jerry") is True + + def test_load_json_file(self): testing_path = "./template/chat-client/default-config.json" expected_output = {