-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
8 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 |
---|---|---|
|
@@ -8,11 +8,15 @@ | |
from registration.users import UserModel | ||
|
||
|
||
DJANGO_VERSION = StrictVersion(get_version()) | ||
|
||
|
||
class RegistrationFormTests(TestCase): | ||
""" | ||
Test the default registration forms. | ||
""" | ||
|
||
def test_registration_form(self): | ||
""" | ||
Test that ``RegistrationForm`` enforces username constraints | ||
|
@@ -24,7 +28,7 @@ def test_registration_form(self): | |
UserModel().objects.create_user('alice', '[email protected]', 'secret') | ||
|
||
bad_username_error = 'This value may contain only letters, numbers and @/./+/-/_ characters.' | ||
if StrictVersion(get_version()) >= StrictVersion('1.8'): | ||
if DJANGO_VERSION >= StrictVersion('1.8'): | ||
bad_username_error = 'Enter a valid username. ' + bad_username_error | ||
|
||
invalid_data_dicts = [ | ||
|
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import unicode_literals | ||
from distutils.version import StrictVersion | ||
try: | ||
from importlib import reload # Python 3.4+ reload() | ||
except: | ||
try: | ||
from imp import reload # Python 3.0+ reload() | ||
except: | ||
pass # Python 2 reload() | ||
|
||
from django import get_version | ||
from django.conf import settings | ||
from django.test import TestCase | ||
|
||
from registration import forms | ||
from registration.users import UsernameField | ||
from test_app.models import CustomUser | ||
|
||
|
||
DJANGO_VERSION = StrictVersion(get_version()) | ||
|
||
|
||
class RegistrationFormTests(TestCase): | ||
""" | ||
Test the default registration forms. | ||
""" | ||
def setUp(self): | ||
self.old_auth_model = getattr(settings, 'AUTH_USER_MODEL', None) | ||
settings.AUTH_USER_MODEL = 'test_app.CustomUser' | ||
# The form's Meta class is created on import. We have to reload() | ||
# to apply the new AUTH_USER_MODEL to the Meta class. | ||
reload(forms) | ||
|
||
def tearDown(self): | ||
settings.AUTH_USER_MODEL = self.old_auth_model | ||
|
||
def test_registration_form_adds_custom_user_name_field(self): | ||
""" | ||
Test that ``RegistrationForm`` adds custom username | ||
field and does not raise errors | ||
""" | ||
|
||
form = forms.RegistrationForm() | ||
|
||
self.assertTrue(UsernameField() in form.fields) | ||
|
||
|
||
def test_registration_form_subclass_is_valid_for_django_18(self): | ||
""" | ||
Test that ``RegistrationForm`` subclasses can save in | ||
Django > 1.8 | ||
""" | ||
if DJANGO_VERSION >= StrictVersion('1.8'): | ||
data = {'new_field': 'custom username', | ||
'email': '[email protected]', | ||
'password1': 'foo', | ||
'password2': 'foo'} | ||
|
||
form = forms.RegistrationForm(data=data) | ||
|
||
self.assertTrue(form.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
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 +1,15 @@ | ||
# coding: utf-8 | ||
|
||
try: | ||
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager | ||
except ImportError: | ||
from django.contrib.auth.models import User as AbstractBaseUser, UserManager as BaseUserManager | ||
|
||
from django.db import models | ||
|
||
|
||
class CustomUser(AbstractBaseUser): | ||
new_field = models.CharField(max_length=25) | ||
objects = BaseUserManager() | ||
|
||
USERNAME_FIELD = 'new_field' |