summaryrefslogtreecommitdiff
path: root/tests/auth_tests/models
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2016-02-04 11:47:51 -0500
committerSimon Charette <charette.s@gmail.com>2016-02-04 12:30:34 -0500
commit6eb3ce11e4d6be237ad2e4895d8ae0b7bc1803dc (patch)
tree0b14e1f387c7009cb89fcf905722805bc43392f2 /tests/auth_tests/models
parent19318507d92d35f5f5cee6bfb7c379efa309c4f8 (diff)
Fixed #26089 -- Removed custom user test models from public API.
Thanks to Tim Graham for the review.
Diffstat (limited to 'tests/auth_tests/models')
-rw-r--r--tests/auth_tests/models/__init__.py6
-rw-r--r--tests/auth_tests/models/custom_permissions.py5
-rw-r--r--tests/auth_tests/models/custom_user.py107
-rw-r--r--tests/auth_tests/models/uuid_pk.py3
4 files changed, 115 insertions, 6 deletions
diff --git a/tests/auth_tests/models/__init__.py b/tests/auth_tests/models/__init__.py
index 3f3bd89793..d7275e177f 100644
--- a/tests/auth_tests/models/__init__.py
+++ b/tests/auth_tests/models/__init__.py
@@ -1,10 +1,12 @@
from .custom_permissions import CustomPermissionsUser
+from .custom_user import CustomUser, ExtensionUser
from .invalid_models import CustomUserNonUniqueUsername
from .is_active import IsActiveTestUser1
from .uuid_pk import UUIDUser
from .with_foreign_key import CustomUserWithFK, Email
__all__ = (
- 'CustomPermissionsUser', 'CustomUserWithFK', 'Email',
- 'IsActiveTestUser1', 'UUIDUser', 'CustomUserNonUniqueUsername',
+ 'CustomUser', 'CustomPermissionsUser', 'CustomUserWithFK', 'Email',
+ 'ExtensionUser', 'IsActiveTestUser1', 'UUIDUser',
+ 'CustomUserNonUniqueUsername',
)
diff --git a/tests/auth_tests/models/custom_permissions.py b/tests/auth_tests/models/custom_permissions.py
index 7687d95df6..a6c5a1e96a 100644
--- a/tests/auth_tests/models/custom_permissions.py
+++ b/tests/auth_tests/models/custom_permissions.py
@@ -4,12 +4,11 @@ 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
+from .custom_user import CustomUserManager, RemoveGroupsAndPermissions
+
class CustomPermissionsUserManager(CustomUserManager):
def create_superuser(self, email, password, date_of_birth):
diff --git a/tests/auth_tests/models/custom_user.py b/tests/auth_tests/models/custom_user.py
new file mode 100644
index 0000000000..0519b09b75
--- /dev/null
+++ b/tests/auth_tests/models/custom_user.py
@@ -0,0 +1,107 @@
+from django.contrib.auth.models import (
+ AbstractBaseUser, AbstractUser, BaseUserManager, Group, Permission,
+ PermissionsMixin, UserManager,
+)
+from django.db import models
+
+
+# The custom User uses email as the unique identifier, and requires
+# that every user provide a date of birth. This lets us test
+# changes in username datatype, and non-text required fields.
+class CustomUserManager(BaseUserManager):
+ def create_user(self, email, date_of_birth, password=None):
+ """
+ Creates and saves a User with the given email and password.
+ """
+ if not email:
+ raise ValueError('Users must have an email address')
+
+ user = self.model(
+ email=self.normalize_email(email),
+ date_of_birth=date_of_birth,
+ )
+
+ user.set_password(password)
+ user.save(using=self._db)
+ return user
+
+ def create_superuser(self, email, password, date_of_birth):
+ u = self.create_user(email, password=password, date_of_birth=date_of_birth)
+ u.is_admin = True
+ u.save(using=self._db)
+ return u
+
+
+class CustomUser(AbstractBaseUser):
+ email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
+ is_active = models.BooleanField(default=True)
+ is_admin = models.BooleanField(default=False)
+ date_of_birth = models.DateField()
+
+ custom_objects = CustomUserManager()
+
+ USERNAME_FIELD = 'email'
+ REQUIRED_FIELDS = ['date_of_birth']
+
+ def get_full_name(self):
+ return self.email
+
+ def get_short_name(self):
+ return self.email
+
+ def __unicode__(self):
+ return self.email
+
+ # Maybe required?
+ def get_group_permissions(self, obj=None):
+ return set()
+
+ def get_all_permissions(self, obj=None):
+ return set()
+
+ def has_perm(self, perm, obj=None):
+ return True
+
+ def has_perms(self, perm_list, obj=None):
+ return True
+
+ def has_module_perms(self, app_label):
+ return True
+
+ # Admin required fields
+ @property
+ def is_staff(self):
+ return self.is_admin
+
+
+class RemoveGroupsAndPermissions(object):
+ """
+ A context manager to temporarily remove the groups and user_permissions M2M
+ fields from the AbstractUser class, so they don't clash with the
+ related_name sets.
+ """
+ def __enter__(self):
+ self._old_au_local_m2m = AbstractUser._meta.local_many_to_many
+ self._old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
+ groups = models.ManyToManyField(Group, blank=True)
+ groups.contribute_to_class(PermissionsMixin, "groups")
+ user_permissions = models.ManyToManyField(Permission, blank=True)
+ user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
+ PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
+ AbstractUser._meta.local_many_to_many = [groups, user_permissions]
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ AbstractUser._meta.local_many_to_many = self._old_au_local_m2m
+ PermissionsMixin._meta.local_many_to_many = self._old_pm_local_m2m
+
+
+# The extension user is a simple extension of the built-in user class,
+# adding a required date_of_birth field. This allows us to check for
+# any hard references to the name "User" in forms/handlers etc.
+with RemoveGroupsAndPermissions():
+ class ExtensionUser(AbstractUser):
+ date_of_birth = models.DateField()
+
+ custom_objects = UserManager()
+
+ REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
diff --git a/tests/auth_tests/models/uuid_pk.py b/tests/auth_tests/models/uuid_pk.py
index 7c65226493..87a4ee0658 100644
--- a/tests/auth_tests/models/uuid_pk.py
+++ b/tests/auth_tests/models/uuid_pk.py
@@ -1,9 +1,10 @@
import uuid
from django.contrib.auth.models import AbstractUser
-from django.contrib.auth.tests.custom_user import RemoveGroupsAndPermissions
from django.db import models
+from .custom_user import RemoveGroupsAndPermissions
+
with RemoveGroupsAndPermissions():
class UUIDUser(AbstractUser):
"""A user with a UUID as primary key"""