diff --git a/spongeauth/accounts/models.py b/spongeauth/accounts/models.py index 975365496..0a8531871 100644 --- a/spongeauth/accounts/models.py +++ b/spongeauth/accounts/models.py @@ -23,19 +23,19 @@ def validate_username(username): errs.append(ValidationError( _('Username must be at least 3 characters long.'), code='username_min_length')) - if re.search(r'[^\w_-]', username): + if re.search(r'[^\w-]', username): errs.append(ValidationError( _('Username must only include numbers, letters, underscores and dashes.'), code='username_charset')) - if re.search(r'[^\w_]', username[0]): + if re.search(r'[\W]', username[0]): errs.append(ValidationError( _('Username must begin with a letter, number or underscore.'), code='username_initial_charset')) - if re.search(r'[^\w_]', username[-1]): + if re.search(r'[\W]', username[-1]): errs.append(ValidationError( _('Username must end with a letter, number or underscore.'), code='username_ending_charset')) - if re.search(r'[-_]{2,}', username): + if re.search(r'[^A-Za-z0-9]{2,}', username): errs.append(ValidationError( _('Username must not contain two special characters in a row.'), code='username_double_special')) diff --git a/spongeauth/accounts/tests/test_username_validation.py b/spongeauth/accounts/tests/test_username_validation.py index d335485b8..35fd38a03 100644 --- a/spongeauth/accounts/tests/test_username_validation.py +++ b/spongeauth/accounts/tests/test_username_validation.py @@ -9,15 +9,16 @@ BAD_EXAMPLES = [ ("lukegb", []), ("_lukegb", []), + ("_lukegb_", []), ("a", ['username_min_length']), - ("__", ['username_double_special', 'username_min_length', 'username_ending_charset']), - ("._", ['username_double_special', 'username_min_length', 'username_ending_charset', 'username_initial_charset']), + ("__", ['username_double_special', 'username_min_length']), + ("._", ['username_double_special', 'username_min_length', 'username_charset', 'username_initial_charset']), ("\N{SNOWMAN}", ['username_charset', 'username_min_length', 'username_ending_charset', 'username_initial_charset']), - (".png", ['username_file_suffix', 'username_initial_charset']), - ("lukegb.png", ['username_file_suffix']), + (".png", ['username_charset', 'username_initial_charset']), + ("lukegb.png", ['username_charset']), ("luke__gb", ['username_double_special']), - ("luke_.gb", ['username_double_special']), - ("lukegb_", ['username_ending_charset']), + ("luke_.gb", ['username_charset', 'username_double_special']), + ("lukegb-", ['username_ending_charset']), ("-lukegb", ['username_initial_charset']), ]