fix: conditions and tests
This commit is contained in:
@@ -34,14 +34,22 @@ class TestCustomUser(unittest.TestCase):
|
||||
}).insert()
|
||||
self.assertEqual(new_user.username[:8], "username")
|
||||
|
||||
def test_with_hyphen_at_end(self):
|
||||
def test_with_underscore_at_end(self):
|
||||
new_user = frappe.get_doc({
|
||||
"doctype": "User",
|
||||
"email": "test_with_hyphen_at_end@example.com",
|
||||
"first_name": "Username---"
|
||||
"email": "test_with_underscore_at_end@example.com",
|
||||
"first_name": "Username___"
|
||||
}).insert()
|
||||
length = len(new_user.username)
|
||||
self.assertNotEqual(new_user.username[length-1], "-")
|
||||
self.assertNotEqual(new_user.username[-1], "_")
|
||||
|
||||
|
||||
def test_with_short_first_name(self):
|
||||
new_user = frappe.get_doc({
|
||||
"doctype": "User",
|
||||
"email": "test_with_short_first_name@example.com",
|
||||
"first_name": "USN"
|
||||
}).insert()
|
||||
self.assertGreaterEqual(len(new_user.username), 4)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
@@ -49,6 +57,7 @@ class TestCustomUser(unittest.TestCase):
|
||||
"test_with_basic_username@example.com",
|
||||
"test-without-username@example.com",
|
||||
"test_with_illegal_characters@example.com",
|
||||
"test_with_hyphen_at_end@example.com"
|
||||
"test_with_underscore_at_end@example.com",
|
||||
"test_with_short_first_name@example.com"
|
||||
]
|
||||
frappe.db.delete("User", {"name": ["in", users]})
|
||||
|
||||
@@ -13,17 +13,24 @@ class CustomUser(User):
|
||||
self.validate_username_characters()
|
||||
|
||||
def validate_username_characters(self):
|
||||
if self.is_new():
|
||||
if len(self.username):
|
||||
underscore_condition = self.username[0] == "_" or self.username[-1] == "_"
|
||||
else:
|
||||
underscore_condition = ''
|
||||
|
||||
if self.is_new():
|
||||
if not self.username:
|
||||
self.username = self.get_username_from_first_name()
|
||||
|
||||
if self.username.find(" "):
|
||||
self.username.replace(" ", "")
|
||||
|
||||
if not re.match("^[A-Za-z0-9_]*$", self.username):
|
||||
if not re.match("^[A-Za-z0-9_]*$", self.username) or underscore_condition:
|
||||
self.username = self.remove_illegal_characters()
|
||||
|
||||
if len(self.username) < 4:
|
||||
self.username = self.email.replace("@", "").replace(".", "")
|
||||
|
||||
while self.username_exists():
|
||||
self.username = self.remove_illegal_characters() + str(random.randint(0, 99))
|
||||
|
||||
@@ -34,9 +41,12 @@ class CustomUser(User):
|
||||
if not re.match("^[A-Za-z0-9_]*$", self.username):
|
||||
frappe.throw(_("Username can only contain alphabets, numbers and unedrscore."))
|
||||
|
||||
if self.username[0] == "_" or self.username[len(self.username) - 1] == "_":
|
||||
if underscore_condition:
|
||||
frappe.throw(_("First and Last character of username cannot be Underscore(_)."))
|
||||
|
||||
if len(self.username) < 4:
|
||||
frappe.throw(_("Username cannot be less than 4 characters"))
|
||||
|
||||
def get_username_from_first_name(self):
|
||||
return frappe.scrub(self.first_name) + str(random.randint(0, 99))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user