Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Prevent duplicate django usernames (bug 731639)
  • Loading branch information
kumar303 committed Feb 29, 2012
1 parent c569a7f commit e4d8438
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 20 additions & 6 deletions apps/users/tests/test_user_utils.py
Expand Up @@ -60,10 +60,24 @@ def test_too_many_tries(self, filter):

@fudge.patch('users.utils.UserProfile.objects.filter')
def test_duplicate_username_counter(self, filter):
filter = (filter.is_callable().returns_fake().provides('count')
.returns(1)
.next_call()
.returns(1)
.next_call()
.returns(0))
filter = (filter.expects_call().returns_fake().expects('count')
.returns(1)
.next_call()
.returns(1)
.next_call()
.returns(0))
eq_(autocreate_username('existingname'), 'existingname3')

@fudge.patch('users.utils.DjangoUser.objects.filter')
@fudge.patch('users.utils.UserProfile.objects.filter')
def test_duplicate_django_username_counter(self, du, up):
up = up.expects_call().returns_fake().expects('count').returns(0)
du = (du.expects_call().returns_fake().expects('count')
.returns(1)
.next_call()
.returns(1)
.next_call()
.returns(1)
.next_call()
.returns(0))
eq_(autocreate_username('existingname'), 'existingname4')
4 changes: 3 additions & 1 deletion apps/users/utils.py
Expand Up @@ -5,6 +5,7 @@
import uuid

from django.conf import settings
from django.contrib.auth.models import User as DjangoUser
from django.db.models import Q

import commonware.log
Expand Down Expand Up @@ -118,6 +119,7 @@ def autocreate_username(candidate, tries=1):
log.info('username blocked, empty, max tries reached, or too long;'
' username=%s; max=%s' % (adjusted_u, max_tries))
return autocreate_username(uuid.uuid4().hex[0:15])
if UserProfile.objects.filter(username=adjusted_u).count():
if (UserProfile.objects.filter(username=adjusted_u).count() or
DjangoUser.objects.filter(username=adjusted_u).count()):
return autocreate_username(candidate, tries=tries + 1)
return adjusted_u

0 comments on commit e4d8438

Please sign in to comment.