diff --git a/openbook_auth/tests/views/test_auth.py b/openbook_auth/tests/views/test_auth.py index 6cc3a937..d30eea17 100644 --- a/openbook_auth/tests/views/test_auth.py +++ b/openbook_auth/tests/views/test_auth.py @@ -96,7 +96,7 @@ def test_invalid_username(self): should return 400 if the username is not valid """ url = self._get_url() - invalid_usernames = ('joel<;<', '<>', ' shantanu space ', 'greater_than_30_characters_username_is_not_valid',) + invalid_usernames = ('joel<;<', '<>', ' shantanu space ', 'greater_than_30_characters_username_is_not_valid','.start_with_period','more_than__one_special_char','end_with_period.') token = self._make_user_invite_token() for username in invalid_usernames: data = { diff --git a/openbook_auth/validators.py b/openbook_auth/validators.py index 5ac1e3fa..d77ee56b 100644 --- a/openbook_auth/validators.py +++ b/openbook_auth/validators.py @@ -6,9 +6,11 @@ def username_characters_validator(username): - if not re.match('^[a-zA-Z0-9_.]*$', username): + if not re.match("^[a-zA-Z0-9](?:[._]?[a-z-A-Z0-9])*$", username): raise ValidationError( - _('Usernames can only contain alphanumeric characters, periods and underscores.'), + _( + 'Usernames can only contain alphanumeric characters, periods and underscores. They must not begin or end with a period or underscore.' + ) )