Skip to content

Commit 3158848

Browse files
Reject usernames used by other users (#4643)
Since #4630, a custom profile username takes precedence over a generated User.username when resolving profile URLs. Reject a custom username that matches another user's User.username, ignoring case. The user's own generated username and an empty username are still accepted.
1 parent ad951d4 commit 3158848

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎pontoon/base/forms.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ class Meta:
248248
"bugzilla",
249249
)
250250

251+
def clean_username(self):
252+
username = self.cleaned_data.get("username")
253+
254+
if not username:
255+
return username
256+
257+
# A custom username takes precedence over a generated User.username
258+
# when resolving profile URLs, so it must not match another user's.
259+
other_users = User.objects.exclude(pk=self.instance.pk)
260+
if other_users.filter(username__iexact=username).exists():
261+
raise ValidationError("This username is already taken.")
262+
263+
return username
264+
251265
def clean_github(self):
252266
github_username = self.cleaned_data.get("github")
253267

‎pontoon/contributors/tests/test_views.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,39 @@ def test_profileform_contact_email_verified(member):
125125
assert User.objects.get(pk=member.user.pk).profile.contact_email_verified is False
126126

127127

128+
@pytest.mark.django_db
129+
def test_profileform_username_of_another_user(member, user_b):
130+
"""A custom username can't match another user's username, ignoring case."""
131+
params = {"username": user_b.username.upper()}
132+
133+
response = member.client.post("/user/attributes/field/", params)
134+
assert response.status_code == 400
135+
assert b"This username is already taken." in response.content
136+
assert User.objects.get(pk=member.user.pk).profile.username != params["username"]
137+
138+
139+
@pytest.mark.django_db
140+
def test_profileform_username_own_username(member):
141+
"""Users can pick their own generated username as a custom username."""
142+
params = {"username": member.user.username}
143+
144+
response = member.client.post("/user/attributes/field/", params)
145+
assert response.status_code == 200
146+
assert User.objects.get(pk=member.user.pk).profile.username == member.user.username
147+
148+
149+
@pytest.mark.django_db
150+
def test_profileform_empty_username(member):
151+
"""An empty username is still accepted."""
152+
profile = User.objects.get(pk=member.user.pk).profile
153+
profile.username = "custom"
154+
profile.save()
155+
156+
response = member.client.post("/user/attributes/field/", {"username": ""})
157+
assert response.status_code == 200
158+
assert not User.objects.get(pk=member.user.pk).profile.username
159+
160+
128161
@pytest.fixture
129162
def mock_profile_render():
130163
with patch(

0 commit comments

Comments
 (0)