-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #946: Make usernames case insensitive
- Loading branch information
bohare
committed
Mar 10, 2017
1 parent
cd3865a
commit 974590a
Showing
5 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import random | ||
|
||
from django.utils.translation import gettext as _ | ||
from django.test import TestCase | ||
from core.tests.utils.cases import UserTestCase | ||
from django.contrib.messages.storage.fallback import FallbackStorage | ||
from django.core import mail | ||
from django.http import HttpRequest | ||
from django.contrib.messages.storage.fallback import FallbackStorage | ||
from django.test import TestCase | ||
from django.utils.translation import gettext as _ | ||
|
||
from ..models import User | ||
from .. import forms | ||
from core.tests.utils.cases import UserTestCase | ||
|
||
from ..models import User | ||
from .factories import UserFactory | ||
|
||
|
||
|
@@ -31,6 +30,37 @@ def test_valid_data(self): | |
user = User.objects.first() | ||
assert user.check_password('iloveyoko79!') is True | ||
|
||
def test_username_case_insensitive_matching(self): | ||
chars = ['', 'İ', 'É', 'Ø', 'œ', 'ß', 'ss', 'Ξ', 'ф', '大家好'] | ||
usernames = ['UsEr%s' % c for c in chars] | ||
users = [ | ||
UserFactory.create(username=username) for username in usernames] | ||
for user in users: | ||
data = { | ||
'username': user.username.lower(), | ||
'email': '%[email protected]' % user.username, | ||
'password1': 'iloveyoko79!', | ||
'password2': 'iloveyoko79!', | ||
'full_name': 'John Lennon', | ||
} | ||
form = forms.RegisterForm(data) | ||
assert form.is_valid() is False | ||
assert (_("A user with that username already exists") in | ||
form.errors.get('username')) | ||
|
||
user = UserFactory.create(username='JohNlEnNoN') | ||
data = { | ||
'username': 'johnLennon', | ||
'email': '[email protected]', | ||
'password1': 'iloveyoko79!', | ||
'password2': 'iloveyoko79!', | ||
'full_name': 'John Lennon', | ||
} | ||
form = forms.RegisterForm(data) | ||
assert form.is_valid() is False | ||
assert (_("A user with that username already exists") in | ||
form.errors.get('username')) | ||
|
||
def test_passwords_do_not_match(self): | ||
data = { | ||
'username': 'imagine71', | ||
|
@@ -241,6 +271,39 @@ def test_update_user_with_existing_username(self): | |
form = forms.ProfileForm(data, instance=user) | ||
assert form.is_valid() is False | ||
|
||
def test_username_case_insensitive_matching(self): | ||
existing_user = UserFactory.create(username='TestUser') | ||
chars = ['', 'İ', 'É', 'Ø', 'œ', 'ß', 'ss', 'Ξ', 'ф', '大家好'] | ||
usernames = ['UsEr%s' % c for c in chars] | ||
users = [ | ||
UserFactory.create(username=username) for username in usernames] | ||
for user in users: | ||
data = { | ||
'username': user.username.lower(), | ||
'email': '%[email protected]' % user.username, | ||
'full_name': 'John Lennon', | ||
} | ||
form = forms.ProfileForm(data, instance=existing_user) | ||
assert form.is_valid() is False | ||
assert (_("A user with that username already exists") in | ||
form.errors.get('username')) | ||
existing_user.refresh_from_db() | ||
assert existing_user.username == 'TestUser' | ||
|
||
user = UserFactory.create( | ||
username='JohNlEnNoN', email='[email protected]') | ||
data = { | ||
'username': 'johnLennon', | ||
'email': '[email protected]', | ||
'full_name': 'John Lennon', | ||
} | ||
form = forms.ProfileForm(data, instance=user) | ||
assert form.is_valid() is True | ||
form.save() | ||
|
||
user.refresh_from_db() | ||
assert user.username == 'johnLennon' | ||
|
||
def test_update_user_with_existing_email(self): | ||
UserFactory.create(email='[email protected]') | ||
user = UserFactory.create(username='imagine71', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,24 @@ def test_create_with_valid_data(self): | |
assert user_obj.is_active | ||
assert not user_obj.email_verified | ||
|
||
def test_case_insensitive_username(self): | ||
usernames = ['UsErOne', 'useRtWo', 'uSERthReE'] | ||
users = [ | ||
UserFactory.create(username=username) for username in usernames] | ||
for user in users: | ||
TEST_DATA = BASIC_TEST_DATA.copy() | ||
TEST_DATA['username'] = user.username.lower() | ||
serializer = serializers.RegistrationSerializer(data=TEST_DATA) | ||
assert serializer.is_valid() is False | ||
assert (_("A user with that username already exists") | ||
in serializer._errors['username']) | ||
TEST_DATA = BASIC_TEST_DATA.copy() | ||
TEST_DATA['username'] = 'userFour' | ||
serializer = serializers.RegistrationSerializer(data=TEST_DATA) | ||
assert serializer.is_valid() | ||
serializer.save() | ||
assert User.objects.count() == 4 | ||
|
||
def test_create_without_email(self): | ||
"""Serialiser should be invalid when no email address is provided.""" | ||
|
||
|
@@ -208,6 +226,26 @@ def test_update_username_fails(self): | |
assert not serializer2.is_valid() | ||
assert serializer2.errors['username'] == ['Cannot update username'] | ||
|
||
def test_case_insensitive_username(self): | ||
data = { | ||
'username': 'USERtwO', | ||
'email': '[email protected]', | ||
'password': 'iloveyoko79', | ||
'full_name': 'John Lennon' | ||
} | ||
usernames = ['UsErOne', 'useRtWo', 'uSERthReE'] | ||
users = [ | ||
UserFactory.create(username=username) for username in usernames] | ||
request = APIRequestFactory().patch( | ||
'/user/userone', data) | ||
force_authenticate(request, user=users[0]) | ||
serializer = serializers.UserSerializer( | ||
users[0], data, context={'request': Request(request)}) | ||
assert serializer.is_valid() is False | ||
assert serializer.errors['username'] == [ | ||
_("A user with that username already exists") | ||
] | ||
|
||
def test_update_last_login_fails(self): | ||
serializer = serializers.UserSerializer(data=BASIC_TEST_DATA) | ||
assert serializer.is_valid() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters