Merge pull request #13195 from bernt-matthias/topic/min-length

Fix a bug in username minimum length transformation and set minimum length to 1
This commit is contained in:
Nicola Soranzo
2022-04-23 09:57:22 +01:00
committed by GitHub
3 changed files with 10 additions and 10 deletions
+7 -7
View File
@@ -16,7 +16,6 @@ VALID_EMAIL_RE = re.compile(r"[^@]+@[^@]+\.[^@]+")
EMAIL_MAX_LEN = 255
# Public name validity parameters
PUBLICNAME_MIN_LEN = 3
PUBLICNAME_MAX_LEN = 255
VALID_PUBLICNAME_RE = re.compile(r"^[a-z0-9._\-]+$")
VALID_PUBLICNAME_SUB = re.compile(r"[^a-z0-9._\-]")
@@ -45,8 +44,8 @@ def validate_password_str(password):
def validate_publicname_str(publicname):
"""Validates a string containing a public username."""
if len(publicname) < PUBLICNAME_MIN_LEN:
return "Public name must be at least %d characters in length." % (PUBLICNAME_MIN_LEN)
if not publicname:
return "Public name cannot be empty"
if len(publicname) > PUBLICNAME_MAX_LEN:
return "Public name cannot be more than %d characters in length." % (PUBLICNAME_MAX_LEN)
if not (VALID_PUBLICNAME_RE.match(publicname)):
@@ -113,10 +112,11 @@ def transform_publicname(publicname):
FILL_CHAR is used to extend or replace characters.
"""
# TODO: Enhance to allow generation of semi-random publicnnames e.g., when valid but taken
if publicname not in ["None", None, ""]:
publicname = publicname.lower()
publicname = re.sub(VALID_PUBLICNAME_SUB, FILL_CHAR, publicname)
publicname = publicname.ljust(PUBLICNAME_MIN_LEN + 1, FILL_CHAR)[:PUBLICNAME_MAX_LEN]
if not publicname:
raise ValueError("Public name cannot be empty")
publicname = publicname.lower()
publicname = re.sub(VALID_PUBLICNAME_SUB, FILL_CHAR, publicname)
publicname = publicname[:PUBLICNAME_MAX_LEN]
return publicname
+1 -1
View File
@@ -55,7 +55,7 @@ class UsersApiTestCase(ApiTestCase):
self.assertEqual(update_json["username"], new_name)
# too short
update_response = self.__update(user, username="mu")
update_response = self.__update(user, username="")
self._assert_status_code_is(update_response, 400)
# not them
+2 -2
View File
@@ -313,10 +313,10 @@ class UserDeserializerTestCase(BaseTestCase):
base_manager.ModelDeserializingError,
self.deserializer.deserialize,
user,
{"username": "ed"},
{"username": ""},
trans=self.trans,
)
self.assertTrue("Public name must be at least" in str(exception))
self.assertTrue("Public name cannot be empty" in str(exception))
self.assertRaises(
base_manager.ModelDeserializingError,
self.deserializer.deserialize,