Skip to content

Commit

Permalink
Added validations for all components names and added and fixed test c…
Browse files Browse the repository at this point in the history
…ases for the same.
  • Loading branch information
maheshsattala committed Feb 12, 2025
1 parent f9467c0 commit 02224bb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
8 changes: 4 additions & 4 deletions kairon/shared/account/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down
11 changes: 11 additions & 0 deletions kairon/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
34 changes: 20 additions & 14 deletions tests/unit_test/api/api_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@ def test_add_bot_with_invalid_name(self):
account = pytest.account
user = "[email protected]"
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):
Expand All @@ -129,18 +132,21 @@ def test_update_bot_with_invalid_name(self):
user = "[email protected]"
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 = "<test>18"
AccountProcessor.update_bot(name=name, bot=bot)

Expand Down
16 changes: 16 additions & 0 deletions tests/unit_test/utility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 02224bb

Please sign in to comment.