From 9bf9256049f01118d0104745bf20fc4f2b2829e4 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sun, 14 Aug 2022 14:00:48 +0530 Subject: [PATCH] test: validate name --- frappe/tests/test_utils.py | 10 ++++++++++ frappe/utils/__init__.py | 2 ++ 2 files changed, 12 insertions(+) diff --git a/frappe/tests/test_utils.py b/frappe/tests/test_utils.py index cfb9003a5e..7f2f9f5779 100644 --- a/frappe/tests/test_utils.py +++ b/frappe/tests/test_utils.py @@ -32,6 +32,7 @@ from frappe.utils import ( random_string, scrub_urls, validate_email_address, + validate_name, validate_phone_number_with_country_code, validate_url, ) @@ -342,6 +343,15 @@ class TestValidationUtils(unittest.TestCase): "field", ) + def test_validate_name(self): + valid_names = ["", "abc", "asd a13", "asd-asd"] + for name in valid_names: + validate_name(name, True) + + invalid_names = ["asd$wat", "asasd/ads"] + for name in invalid_names: + self.assertRaises(frappe.InvalidNameError, validate_name, name, True) + class TestImage(unittest.TestCase): def test_strip_exif_data(self): diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index bc6c027f35..3390c36b50 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -138,6 +138,8 @@ def validate_name(name, throw=False): """Returns True if the name is valid valid names may have unicode and ascii characters, dash, quotes, numbers anything else is considered invalid + + Note: "Name" here is name of a person, not the primary key in Frappe doctypes. """ if not name: return False