Skip to content

Commit

Permalink
fix test parametrization
Browse files Browse the repository at this point in the history
  • Loading branch information
dsschult committed Jan 5, 2024
1 parent 5228e85 commit ba3e5ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 6 additions & 4 deletions tests/test_api_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ async def test_username_select(server, reg_token_client):
assert ret['username'] == 'f-bar.bar_'

invalid_usernames = [
('foo',), # too short
('fooooooooooooooooooooooooo',), # too long
('foò',), # unicode
('fo=o',), # invalid char
'foo', # too short
'fooooooooooooooooooooooooo', # too long
'foò', # unicode
'fo=o', # invalid char
'fo o', # space
'f\'oo', # quote
]

@pytest.mark.parametrize('username', invalid_usernames)
Expand Down
4 changes: 2 additions & 2 deletions user_mgmt/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def create_server():
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions', MultiInstitutions, kwargs)
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)', Institution, kwargs)
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)/users', InstitutionMultiUsers, kwargs)
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)/users/(?P<username>[\w\-]+)', InstitutionUser, kwargs)
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/institutions/(?P<institution>[\w\-]+)/users/(?P<username>[\w\-\._]+)', InstitutionUser, kwargs)

server.add_route('/api/inst_approvals', InstApprovals, kwargs)
server.add_route(r'/api/inst_approvals/(?P<approval_id>\w+)/actions/approve', InstApprovalsActionApprove, kwargs)
Expand All @@ -112,7 +112,7 @@ def create_server():
server.add_route(r'/api/group_approvals/(?P<approval_id>\w+)/actions/deny', GroupApprovalsActionDeny, kwargs)

server.add_route(r'/api/users', MultiUser, kwargs)
server.add_route(r'/api/users/(?P<username>[\w\-]+)', User, kwargs)
server.add_route(r'/api/users/(?P<username>[\w\-\._]+)', User, kwargs)
server.add_route('/api/username', Username, kwargs)
server.add_route(r'/api/experiments/(?P<experiment>[\w\-]+)/associates', AssociateUsers, kwargs)

Expand Down
19 changes: 16 additions & 3 deletions user_mgmt/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,25 @@ def _gen_username(first_name, last_name, number):

@staticmethod
def _username_valid(username):
"""Check if a username is valid - length, bad words."""
"""
Check if a username is valid.
Valid:
* ascii string between 4-16 chars
* letters, numbers, -, ., _
Invalid:
* unicode
* quotes
* spaces
* special chars
* BAD_WORDS filter
"""
ascii_username = unidecode.unidecode(username).replace("'", '').replace(' ', '').lower()
if ascii_username != username:
return False
#if not re.fullmatch('[\w\-\._]+', username):
# return False
if not re.fullmatch('[\w\-\._]+', username):
return False
if len(username) < 5:
return False
if len(username) > 16:
Expand Down

0 comments on commit ba3e5ad

Please sign in to comment.