Skip to content

Commit

Permalink
Added form validation on username. Fixes #2489.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeray Diaz Diaz committed Oct 21, 2017
1 parent dd8cd1c commit fee2df3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/unit/accounts/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,28 @@ def test_username_exists(self):
"Please choose a different username."
)

@pytest.mark.parametrize("username", ['_foo', 'bar_', 'foo^bar'])
def test_username_is_valid(self, username):
form = forms.RegistrationForm(
data={"username": username},
user_service=pretend.stub(
find_userid=pretend.call_recorder(lambda _: None),
),
recaptcha_service=pretend.stub(
enabled=False,
verify_response=pretend.call_recorder(lambda _: None),
),
)
assert not form.validate()
assert (
form.username.errors.pop() ==
"The username is invalid. Usernames "
"must be composed of letters, numbers, "
"dots, hyphens and underscores. And must "
"also start and finish with a letter or number. "
"Please choose a different username."
)

def test_password_strength(self):
cases = (
("foobar", False),
Expand Down
12 changes: 12 additions & 0 deletions warehouse/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class CredentialsMixin:
"a username with under 50 characters."
)
),
# the regexp below must match the CheckConstraint
# for the username field in accounts.models.User
wtforms.validators.Regexp(
r'^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$',
message=(
"The username is invalid. Usernames "
"must be composed of letters, numbers, "
"dots, hyphens and underscores. And must "
"also start and finish with a letter or number. "
"Please choose a different username."
)
)
],
)

Expand Down

0 comments on commit fee2df3

Please sign in to comment.