diff options
| author | Tim Graham <timograham@gmail.com> | 2015-02-11 08:24:42 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-02-11 10:29:48 -0500 |
| commit | 5ab327a3894c26f57baabe14084bcce2a71b8af8 (patch) | |
| tree | 33fdf55b341739952a80226536568ed60fe4be40 /tests | |
| parent | 2d7aca3da0a46c09e9c70ebdb56ed340691a999f (diff) | |
Moved non-documented auth test models to the new test location.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/auth_tests/models/__init__.py | 13 | ||||
| -rw-r--r-- | tests/auth_tests/models/custom_permissions.py | 43 | ||||
| -rw-r--r-- | tests/auth_tests/models/invalid_models.py | 36 | ||||
| -rw-r--r-- | tests/auth_tests/models/is_active.py | 18 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_foreign_key.py | 31 | ||||
| -rw-r--r-- | tests/auth_tests/test_auth_backends.py | 6 | ||||
| -rw-r--r-- | tests/auth_tests/test_management.py | 10 | ||||
| -rw-r--r-- | tests/auth_tests/test_models.py | 2 | ||||
| -rw-r--r-- | tests/auth_tests/test_views.py | 2 |
9 files changed, 150 insertions, 11 deletions
diff --git a/tests/auth_tests/models/__init__.py b/tests/auth_tests/models/__init__.py new file mode 100644 index 0000000000..edeaef5347 --- /dev/null +++ b/tests/auth_tests/models/__init__.py @@ -0,0 +1,13 @@ +from .custom_permissions import CustomPermissionsUser +from .is_active import IsActiveTestUser1 +from .invalid_models import ( + CustomUserNonUniqueUsername, CustomUserNonListRequiredFields, + CustomUserBadRequiredFields, +) +from .with_foreign_key import CustomUserWithFK, Email + +__all__ = ( + 'CustomPermissionsUser', 'CustomUserNonUniqueUsername', + 'CustomUserNonListRequiredFields', 'CustomUserBadRequiredFields', + 'CustomUserWithFK', 'Email', 'IsActiveTestUser1', +) diff --git a/tests/auth_tests/models/custom_permissions.py b/tests/auth_tests/models/custom_permissions.py new file mode 100644 index 0000000000..7693730745 --- /dev/null +++ b/tests/auth_tests/models/custom_permissions.py @@ -0,0 +1,43 @@ +""" +The CustomPermissionsUser users email as the identifier, but uses the normal +Django permissions model. This allows us to check that the PermissionsMixin +includes everything that is needed to interact with the ModelBackend. +""" +from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin +from django.contrib.auth.tests.custom_user import ( + CustomUserManager, RemoveGroupsAndPermissions, +) +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +class CustomPermissionsUserManager(CustomUserManager): + def create_superuser(self, email, password, date_of_birth): + u = self.create_user(email, password=password, date_of_birth=date_of_birth) + u.is_superuser = True + u.save(using=self._db) + return u + + +with RemoveGroupsAndPermissions(): + @python_2_unicode_compatible + class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin): + email = models.EmailField(verbose_name='email address', max_length=255, unique=True) + date_of_birth = models.DateField() + + custom_objects = CustomPermissionsUserManager() + + USERNAME_FIELD = 'email' + REQUIRED_FIELDS = ['date_of_birth'] + + class Meta: + app_label = 'auth' + + def get_full_name(self): + return self.email + + def get_short_name(self): + return self.email + + def __str__(self): + return self.email diff --git a/tests/auth_tests/models/invalid_models.py b/tests/auth_tests/models/invalid_models.py new file mode 100644 index 0000000000..46b4819fa4 --- /dev/null +++ b/tests/auth_tests/models/invalid_models.py @@ -0,0 +1,36 @@ +from django.contrib.auth.models import AbstractBaseUser +from django.db import models + + +class CustomUserNonUniqueUsername(AbstractBaseUser): + "A user with a non-unique username" + username = models.CharField(max_length=30) + + USERNAME_FIELD = 'username' + + class Meta: + app_label = 'auth' + + +class CustomUserNonListRequiredFields(AbstractBaseUser): + "A user with a non-list REQUIRED_FIELDS" + username = models.CharField(max_length=30, unique=True) + date_of_birth = models.DateField() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = 'date_of_birth' + + class Meta: + app_label = 'auth' + + +class CustomUserBadRequiredFields(AbstractBaseUser): + "A user with a USERNAME_FIELD that appears in REQUIRED_FIELDS (invalid)" + username = models.CharField(max_length=30, unique=True) + date_of_birth = models.DateField() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = ['username', 'date_of_birth'] + + class Meta: + app_label = 'auth' diff --git a/tests/auth_tests/models/is_active.py b/tests/auth_tests/models/is_active.py new file mode 100644 index 0000000000..6e4368f927 --- /dev/null +++ b/tests/auth_tests/models/is_active.py @@ -0,0 +1,18 @@ +from django.contrib.auth.models import AbstractBaseUser, BaseUserManager +from django.db import models + + +class IsActiveTestUser1(AbstractBaseUser): + """ + This test user class and derivatives test the default is_active behavior + """ + username = models.CharField(max_length=30, unique=True) + + custom_objects = BaseUserManager() + + USERNAME_FIELD = 'username' + + class Meta: + app_label = 'auth' + + # the is_active attr is provided by AbstractBaseUser diff --git a/tests/auth_tests/models/with_foreign_key.py b/tests/auth_tests/models/with_foreign_key.py new file mode 100644 index 0000000000..21addfc589 --- /dev/null +++ b/tests/auth_tests/models/with_foreign_key.py @@ -0,0 +1,31 @@ +from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, Group +from django.db import models + + +class Email(models.Model): + email = models.EmailField(verbose_name='email address', max_length=255, unique=True) + + class Meta: + app_label = 'auth' + + +class CustomUserWithFKManager(BaseUserManager): + def create_superuser(self, username, email, group, password): + user = self.model(username_id=username, email_id=email, group_id=group) + user.set_password(password) + user.save(using=self._db) + return user + + +class CustomUserWithFK(AbstractBaseUser): + username = models.ForeignKey(Email, related_name='primary') + email = models.ForeignKey(Email, to_field='email', related_name='secondary') + group = models.ForeignKey(Group) + + custom_objects = CustomUserWithFKManager() + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = ['email', 'group'] + + class Meta: + app_label = 'auth' diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index 5dde319197..b5d6b82fcc 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -6,14 +6,14 @@ from django.contrib.auth import BACKEND_SESSION_KEY, authenticate, get_user from django.contrib.auth.backends import ModelBackend from django.contrib.auth.hashers import MD5PasswordHasher from django.contrib.auth.models import AnonymousUser, Group, Permission, User -from django.contrib.auth.tests.custom_user import ( - CustomPermissionsUser, CustomUser, ExtensionUser, -) +from django.contrib.auth.tests.custom_user import CustomUser, ExtensionUser from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.http import HttpRequest from django.test import TestCase, modify_settings, override_settings +from .models import CustomPermissionsUser + class CountingMD5PasswordHasher(MD5PasswordHasher): """Hasher that counts how many times it computes a hash.""" diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index 159dc8894d..0e2fe8d0e7 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -12,10 +12,7 @@ from django.contrib.auth.management.commands import ( changepassword, createsuperuser, ) from django.contrib.auth.models import Group, User -from django.contrib.auth.tests.custom_user import ( - CustomUser, CustomUserBadRequiredFields, CustomUserNonListRequiredFields, - CustomUserNonUniqueUsername, CustomUserWithFK, Email, -) +from django.contrib.auth.tests.custom_user import CustomUser from django.contrib.contenttypes.models import ContentType from django.core import checks, exceptions from django.core.management import call_command @@ -25,6 +22,11 @@ from django.utils import six from django.utils.encoding import force_str from django.utils.translation import ugettext_lazy as _ +from .models import ( + CustomUserBadRequiredFields, CustomUserNonListRequiredFields, + CustomUserNonUniqueUsername, CustomUserWithFK, Email, +) + def mock_inputs(inputs): """ diff --git a/tests/auth_tests/test_models.py b/tests/auth_tests/test_models.py index 66e1d17696..5dff9c16b0 100644 --- a/tests/auth_tests/test_models.py +++ b/tests/auth_tests/test_models.py @@ -2,8 +2,6 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import ( AbstractUser, Group, Permission, User, UserManager, ) -# Needed so model is installed when tests are run independently: -from django.contrib.auth.tests.custom_user import IsActiveTestUser1 # NOQA from django.contrib.contenttypes.models import ContentType from django.core import mail from django.db.models.signals import post_save diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index 628513c60a..ec991153a0 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -13,8 +13,6 @@ from django.contrib.auth.forms import ( AuthenticationForm, PasswordChangeForm, SetPasswordForm, ) from django.contrib.auth.models import User -# Needed so model is installed when tests are run independently: -from django.contrib.auth.tests.custom_user import CustomUser # NOQA from django.contrib.auth.views import login as login_view, redirect_to_login from django.contrib.sessions.middleware import SessionMiddleware from django.contrib.sites.requests import RequestSite |
