Return the reason a username validation failed and add tests to verify we are sending the reason to client
This commit is contained in:
parent
a71c682abe
commit
0e54b0501c
3 changed files with 39 additions and 11 deletions
|
@ -61,8 +61,10 @@ class InvalidBuildTriggerException(DataModelException):
|
||||||
def create_user(username, password, email, is_organization=False):
|
def create_user(username, password, email, is_organization=False):
|
||||||
if not validate_email(email):
|
if not validate_email(email):
|
||||||
raise InvalidEmailAddressException('Invalid email address: %s' % email)
|
raise InvalidEmailAddressException('Invalid email address: %s' % email)
|
||||||
if not validate_username(username):
|
|
||||||
raise InvalidUsernameException('Invalid username: %s' % username)
|
(username_valid, username_issue) = validate_username(username)
|
||||||
|
if not username_valid:
|
||||||
|
raise InvalidUsernameException('Invalid username %s: %s' % (username, username_issue))
|
||||||
|
|
||||||
# We allow password none for the federated login case.
|
# We allow password none for the federated login case.
|
||||||
if password is not None and not validate_password(password):
|
if password is not None and not validate_password(password):
|
||||||
|
@ -125,9 +127,10 @@ def create_organization(name, email, creating_user):
|
||||||
|
|
||||||
|
|
||||||
def create_robot(robot_shortname, parent):
|
def create_robot(robot_shortname, parent):
|
||||||
if not validate_username(robot_shortname):
|
(username_valid, username_issue) = validate_username(robot_shortname)
|
||||||
raise InvalidRobotException('The name for the robot \'%s\' is invalid.' %
|
if not username_valid:
|
||||||
robot_shortname)
|
raise InvalidRobotException('The name for the robot \'%s\' is invalid: %s' %
|
||||||
|
(robot_shortname, username_issue))
|
||||||
|
|
||||||
username = format_robot_username(parent.username, robot_shortname)
|
username = format_robot_username(parent.username, robot_shortname)
|
||||||
|
|
||||||
|
@ -214,8 +217,9 @@ def convert_user_to_organization(user, admin_user):
|
||||||
|
|
||||||
|
|
||||||
def create_team(name, org, team_role_name, description=''):
|
def create_team(name, org, team_role_name, description=''):
|
||||||
if not validate_username(name):
|
(username_valid, username_issue) = validate_username(name)
|
||||||
raise InvalidTeamException('Invalid team name: %s' % name)
|
if not username_valid:
|
||||||
|
raise InvalidTeamException('Invalid team name %s: %s' % (name, username_issue))
|
||||||
|
|
||||||
if not org.organization:
|
if not org.organization:
|
||||||
raise InvalidOrganizationException('User with name %s is not an org.' %
|
raise InvalidOrganizationException('User with name %s is not an org.' %
|
||||||
|
|
|
@ -292,6 +292,24 @@ class TestCreateNewUser(ApiTestCase):
|
||||||
expected_code=400)
|
expected_code=400)
|
||||||
|
|
||||||
self.assertEquals('The username already exists', json['message'])
|
self.assertEquals('The username already exists', json['message'])
|
||||||
|
|
||||||
|
def test_trycreatetooshort(self):
|
||||||
|
json = self.postJsonResponse(User,
|
||||||
|
data=dict(username='a',
|
||||||
|
password='password',
|
||||||
|
email='test@example.com'),
|
||||||
|
expected_code=400)
|
||||||
|
|
||||||
|
self.assertEquals('Invalid username a: Username must be between 4 and 30 characters in length', json['error_description'])
|
||||||
|
|
||||||
|
def test_trycreateregexmismatch(self):
|
||||||
|
json = self.postJsonResponse(User,
|
||||||
|
data=dict(username='auserName',
|
||||||
|
password='password',
|
||||||
|
email='test@example.com'),
|
||||||
|
expected_code=400)
|
||||||
|
|
||||||
|
self.assertEquals('Invalid username auserName: Username must match expression [a-z0-9_]+', json['error_description'])
|
||||||
|
|
||||||
def test_createuser(self):
|
def test_createuser(self):
|
||||||
data = self.postResponse(User,
|
data = self.postResponse(User,
|
||||||
|
|
|
@ -10,10 +10,16 @@ def validate_email(email_address):
|
||||||
|
|
||||||
|
|
||||||
def validate_username(username):
|
def validate_username(username):
|
||||||
# Minimum length of 2, maximum length of 255, no url unsafe characters
|
# Based off the restrictions defined in the Docker Registry API spec
|
||||||
return (re.search(r'[^a-z0-9_]', username) is None and
|
regex_match = (re.search(r'[^a-z0-9_]', username) is None)
|
||||||
len(username) >= 4 and
|
if not regex_match:
|
||||||
len(username) <= 30)
|
return (False, 'Username must match expression [a-z0-9_]+')
|
||||||
|
|
||||||
|
length_match = (len(username) >= 4 and len(username) <= 30)
|
||||||
|
if not length_match:
|
||||||
|
return (False, 'Username must be between 4 and 30 characters in length')
|
||||||
|
|
||||||
|
return (True, '')
|
||||||
|
|
||||||
|
|
||||||
def validate_password(password):
|
def validate_password(password):
|
||||||
|
|
Reference in a new issue