Skip to content

Commit

Permalink
user email duplication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban committed Apr 1, 2015
1 parent 33c91ee commit 8956eed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions spirit/backends/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def authenticate(self, username=None, password=None, **kwargs):
pass

def get_user(self, user_id):
# This get called if the user get authenticated with email
# This is called if the user get authenticated with email
try:
return User._default_manager\
.select_related('st')\
.get(pk=user_id)
except User.DoesNotExist:
return None
pass
3 changes: 1 addition & 2 deletions spirit/forms/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class EmailUniqueMixin(object):
def clean_email(self):
email = self.cleaned_data["email"]

# TODO: test!
if not settings.ST_UNIQUE_EMAILS:
return email

Expand All @@ -39,7 +38,7 @@ def get_email(self):


class EmailCheckForm(EmailUniqueMixin, forms.Form):
# TODO: test!

email = forms.CharField(label=_("Email"), widget=forms.EmailInput, max_length=254)


Expand Down
53 changes: 51 additions & 2 deletions tests/tests_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

import datetime

from django.test import TestCase, RequestFactory
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.contrib.auth import get_user_model, HASH_SESSION_KEY
from django.core import mail
from django.utils.translation import ugettext as _
from django.utils import timezone
from django.test.utils import override_settings

from djconfig.utils import override_djconfig

from . import utils

from spirit.forms.user import RegistrationForm, UserProfileForm, EmailChangeForm, ResendActivationForm, UserForm
from spirit.forms.user import RegistrationForm, UserProfileForm, \
EmailChangeForm, ResendActivationForm, UserForm, EmailCheckForm
from spirit.backends.user import EmailAuthBackend
from spirit.models.comment_like import CommentLike
from spirit.utils.user.tokens import UserActivationTokenGenerator, UserEmailChangeTokenGenerator
Expand Down Expand Up @@ -680,6 +682,17 @@ def test_registration_email_duplication(self):
self.assertEqual(form.is_valid(), False)
self.assertNotIn('email', form.cleaned_data)

@override_settings(ST_UNIQUE_EMAILS=False)
def test_registration_email_duplication_allowed(self):
"""
Duplicated email allowed
"""
utils.create_user(email='duplicated@bar.com')
form_data = {'username': 'foo', 'email': 'duplicated@bar.com',
'password1': 'pass', 'password2': 'pass'}
form = RegistrationForm(data=form_data)
self.assertEqual(form.is_valid(), True)

def test_profile(self):
"""
edit user profile
Expand Down Expand Up @@ -723,6 +736,17 @@ def test_email_change_email_duplication(self):
self.assertEqual(form.is_valid(), False)
self.assertNotIn('email', form.cleaned_data)

@override_settings(ST_UNIQUE_EMAILS=False)
def test_email_change_email_duplication_allowed(self):
"""
Duplicated email allowed
"""
utils.create_user(email="duplicated@bar.com")
user = utils.create_user(password="foo")
form_data = {'email': 'duplicated@bar.com', 'password': 'foo'}
form = EmailChangeForm(data=form_data, user=user)
self.assertEqual(form.is_valid(), True)

def test_resend_activation_email(self):
"""
resend activation
Expand Down Expand Up @@ -759,6 +783,31 @@ def test_resend_activation_email_duplication(self):
self.assertTrue(form.is_valid())
self.assertEqual(form.get_user(), user2)

def test_email_check(self):
"""
Check it's an email
"""
# Unique email
form_data = {'email': 'unique@bar.com', }
form = EmailCheckForm(form_data)
self.assertTrue(form.is_valid())

# Duplicated email
utils.create_user(email="duplicated@bar.com")
form_data['email'] = "duplicated@bar.com"
form = EmailCheckForm(form_data)
self.assertFalse(form.is_valid())

@override_settings(ST_UNIQUE_EMAILS=False)
def test_email_check_non_unique(self):
"""
Duplicated email allowed
"""
utils.create_user(email="duplicated@bar.com")
form_data = {'email': 'duplicated@bar.com', }
form = EmailCheckForm(form_data)
self.assertTrue(form.is_valid())


class UserBackendTest(TestCase):

Expand Down

0 comments on commit 8956eed

Please sign in to comment.