diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/auth_tests/models | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/auth_tests/models')
| -rw-r--r-- | tests/auth_tests/models/__init__.py | 33 | ||||
| -rw-r--r-- | tests/auth_tests/models/custom_permissions.py | 9 | ||||
| -rw-r--r-- | tests/auth_tests/models/custom_user.py | 31 | ||||
| -rw-r--r-- | tests/auth_tests/models/invalid_models.py | 5 | ||||
| -rw-r--r-- | tests/auth_tests/models/is_active.py | 3 | ||||
| -rw-r--r-- | tests/auth_tests/models/minimal.py | 2 | ||||
| -rw-r--r-- | tests/auth_tests/models/no_password.py | 2 | ||||
| -rw-r--r-- | tests/auth_tests/models/proxy.py | 8 | ||||
| -rw-r--r-- | tests/auth_tests/models/uuid_pk.py | 2 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_custom_email_field.py | 4 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_foreign_key.py | 12 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_integer_username.py | 4 | ||||
| -rw-r--r-- | tests/auth_tests/models/with_many_to_many.py | 10 |
13 files changed, 73 insertions, 52 deletions
diff --git a/tests/auth_tests/models/__init__.py b/tests/auth_tests/models/__init__.py index b3e06cd7b8..bce5edf883 100644 --- a/tests/auth_tests/models/__init__.py +++ b/tests/auth_tests/models/__init__.py @@ -1,7 +1,5 @@ from .custom_permissions import CustomPermissionsUser -from .custom_user import ( - CustomUser, CustomUserWithoutIsActiveField, ExtensionUser, -) +from .custom_user import CustomUser, CustomUserWithoutIsActiveField, ExtensionUser from .invalid_models import CustomUserNonUniqueUsername from .is_active import IsActiveTestUser1 from .minimal import MinimalUser @@ -12,15 +10,26 @@ from .with_custom_email_field import CustomEmailField from .with_foreign_key import CustomUserWithFK, Email from .with_integer_username import IntegerUsernameUser from .with_last_login_attr import UserWithDisabledLastLoginField -from .with_many_to_many import ( - CustomUserWithM2M, CustomUserWithM2MThrough, Organization, -) +from .with_many_to_many import CustomUserWithM2M, CustomUserWithM2MThrough, Organization __all__ = ( - 'CustomEmailField', 'CustomPermissionsUser', 'CustomUser', - 'CustomUserNonUniqueUsername', 'CustomUserWithFK', 'CustomUserWithM2M', - 'CustomUserWithM2MThrough', 'CustomUserWithoutIsActiveField', 'Email', - 'ExtensionUser', 'IntegerUsernameUser', 'IsActiveTestUser1', 'MinimalUser', - 'NoPasswordUser', 'Organization', 'Proxy', 'UUIDUser', 'UserProxy', - 'UserWithDisabledLastLoginField', + "CustomEmailField", + "CustomPermissionsUser", + "CustomUser", + "CustomUserNonUniqueUsername", + "CustomUserWithFK", + "CustomUserWithM2M", + "CustomUserWithM2MThrough", + "CustomUserWithoutIsActiveField", + "Email", + "ExtensionUser", + "IntegerUsernameUser", + "IsActiveTestUser1", + "MinimalUser", + "NoPasswordUser", + "Organization", + "Proxy", + "UUIDUser", + "UserProxy", + "UserWithDisabledLastLoginField", ) diff --git a/tests/auth_tests/models/custom_permissions.py b/tests/auth_tests/models/custom_permissions.py index bddcfde2eb..52d28bd276 100644 --- a/tests/auth_tests/models/custom_permissions.py +++ b/tests/auth_tests/models/custom_permissions.py @@ -18,14 +18,17 @@ class CustomPermissionsUserManager(CustomUserManager): with RemoveGroupsAndPermissions(): + class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin): - email = models.EmailField(verbose_name='email address', max_length=255, unique=True) + 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'] + USERNAME_FIELD = "email" + REQUIRED_FIELDS = ["date_of_birth"] def __str__(self): return self.email diff --git a/tests/auth_tests/models/custom_user.py b/tests/auth_tests/models/custom_user.py index a46f1d5a9c..b9938681ca 100644 --- a/tests/auth_tests/models/custom_user.py +++ b/tests/auth_tests/models/custom_user.py @@ -1,6 +1,11 @@ from django.contrib.auth.models import ( - AbstractBaseUser, AbstractUser, BaseUserManager, Group, Permission, - PermissionsMixin, UserManager, + AbstractBaseUser, + AbstractUser, + BaseUserManager, + Group, + Permission, + PermissionsMixin, + UserManager, ) from django.db import models @@ -14,12 +19,10 @@ class CustomUserManager(BaseUserManager): Creates and saves a User with the given email and password. """ if not email: - raise ValueError('Users must have an email address') + raise ValueError("Users must have an email address") user = self.model( - email=self.normalize_email(email), - date_of_birth=date_of_birth, - **fields + email=self.normalize_email(email), date_of_birth=date_of_birth, **fields ) user.set_password(password) @@ -27,14 +30,16 @@ class CustomUserManager(BaseUserManager): return user def create_superuser(self, email, password, date_of_birth, **fields): - u = self.create_user(email, password=password, date_of_birth=date_of_birth, **fields) + u = self.create_user( + email, password=password, date_of_birth=date_of_birth, **fields + ) 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) + 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() @@ -42,8 +47,8 @@ class CustomUser(AbstractBaseUser): custom_objects = CustomUserManager() - USERNAME_FIELD = 'email' - REQUIRED_FIELDS = ['date_of_birth', 'first_name'] + USERNAME_FIELD = "email" + REQUIRED_FIELDS = ["date_of_birth", "first_name"] def __str__(self): return self.email @@ -76,6 +81,7 @@ class RemoveGroupsAndPermissions: 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 @@ -97,16 +103,17 @@ class CustomUserWithoutIsActiveField(AbstractBaseUser): objects = UserManager() - USERNAME_FIELD = 'username' + USERNAME_FIELD = "username" # 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'] + REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ["date_of_birth"] diff --git a/tests/auth_tests/models/invalid_models.py b/tests/auth_tests/models/invalid_models.py index 123ed4d865..280c690faa 100644 --- a/tests/auth_tests/models/invalid_models.py +++ b/tests/auth_tests/models/invalid_models.py @@ -9,12 +9,13 @@ class CustomUserNonUniqueUsername(AbstractBaseUser): This model is not invalid if it is used with a custom authentication backend which supports non-unique usernames. """ + username = models.CharField(max_length=30) email = models.EmailField(blank=True) is_staff = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['email'] + USERNAME_FIELD = "username" + REQUIRED_FIELDS = ["email"] objects = UserManager() diff --git a/tests/auth_tests/models/is_active.py b/tests/auth_tests/models/is_active.py index 4c3144ffdc..cc91b8b97e 100644 --- a/tests/auth_tests/models/is_active.py +++ b/tests/auth_tests/models/is_active.py @@ -6,10 +6,11 @@ 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' + USERNAME_FIELD = "username" # the is_active attr is provided by AbstractBaseUser diff --git a/tests/auth_tests/models/minimal.py b/tests/auth_tests/models/minimal.py index 7b8931b231..6fd986848c 100644 --- a/tests/auth_tests/models/minimal.py +++ b/tests/auth_tests/models/minimal.py @@ -3,4 +3,4 @@ from django.db import models class MinimalUser(models.Model): REQUIRED_FIELDS = () - USERNAME_FIELD = 'id' + USERNAME_FIELD = "id" diff --git a/tests/auth_tests/models/no_password.py b/tests/auth_tests/models/no_password.py index c7c40f76f6..33c08b4692 100644 --- a/tests/auth_tests/models/no_password.py +++ b/tests/auth_tests/models/no_password.py @@ -17,5 +17,5 @@ class NoPasswordUser(AbstractBaseUser): last_login = None username = models.CharField(max_length=50, unique=True) - USERNAME_FIELD = 'username' + USERNAME_FIELD = "username" objects = UserManager() diff --git a/tests/auth_tests/models/proxy.py b/tests/auth_tests/models/proxy.py index 9e82a398c2..ddcdfd582c 100644 --- a/tests/auth_tests/models/proxy.py +++ b/tests/auth_tests/models/proxy.py @@ -9,14 +9,10 @@ class Concrete(models.Model): class Proxy(Concrete): class Meta: proxy = True - permissions = ( - ('display_proxys', 'May display proxys information'), - ) + permissions = (("display_proxys", "May display proxys information"),) class UserProxy(User): class Meta: proxy = True - permissions = ( - ('use_different_app_label', 'May use a different app label'), - ) + permissions = (("use_different_app_label", "May use a different app label"),) diff --git a/tests/auth_tests/models/uuid_pk.py b/tests/auth_tests/models/uuid_pk.py index 87a4ee0658..d753660cbe 100644 --- a/tests/auth_tests/models/uuid_pk.py +++ b/tests/auth_tests/models/uuid_pk.py @@ -6,6 +6,8 @@ from django.db import models from .custom_user import RemoveGroupsAndPermissions with RemoveGroupsAndPermissions(): + class UUIDUser(AbstractUser): """A user with a UUID as primary key""" + id = models.UUIDField(default=uuid.uuid4, primary_key=True) diff --git a/tests/auth_tests/models/with_custom_email_field.py b/tests/auth_tests/models/with_custom_email_field.py index 278cd137a4..3cba8802d9 100644 --- a/tests/auth_tests/models/with_custom_email_field.py +++ b/tests/auth_tests/models/with_custom_email_field.py @@ -18,7 +18,7 @@ class CustomEmailField(AbstractBaseUser): email_address = models.EmailField(null=True) is_active = models.BooleanField(default=True) - EMAIL_FIELD = 'email_address' - USERNAME_FIELD = 'username' + EMAIL_FIELD = "email_address" + USERNAME_FIELD = "username" objects = CustomEmailFieldUserManager() diff --git a/tests/auth_tests/models/with_foreign_key.py b/tests/auth_tests/models/with_foreign_key.py index d16329a04b..13100accff 100644 --- a/tests/auth_tests/models/with_foreign_key.py +++ b/tests/auth_tests/models/with_foreign_key.py @@ -3,7 +3,7 @@ from django.db import models class Email(models.Model): - email = models.EmailField(verbose_name='email address', max_length=255, unique=True) + email = models.EmailField(verbose_name="email address", max_length=255, unique=True) class CustomUserWithFKManager(BaseUserManager): @@ -15,11 +15,13 @@ class CustomUserWithFKManager(BaseUserManager): class CustomUserWithFK(AbstractBaseUser): - username = models.ForeignKey(Email, models.CASCADE, related_name='primary') - email = models.ForeignKey(Email, models.CASCADE, to_field='email', related_name='secondary') + username = models.ForeignKey(Email, models.CASCADE, related_name="primary") + email = models.ForeignKey( + Email, models.CASCADE, to_field="email", related_name="secondary" + ) group = models.ForeignKey(Group, models.CASCADE) custom_objects = CustomUserWithFKManager() - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['email', 'group'] + USERNAME_FIELD = "username" + REQUIRED_FIELDS = ["email", "group"] diff --git a/tests/auth_tests/models/with_integer_username.py b/tests/auth_tests/models/with_integer_username.py index fc3728d798..8d99bee13a 100644 --- a/tests/auth_tests/models/with_integer_username.py +++ b/tests/auth_tests/models/with_integer_username.py @@ -17,7 +17,7 @@ class IntegerUsernameUser(AbstractBaseUser): username = models.IntegerField() password = models.CharField(max_length=255) - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['username', 'password'] + USERNAME_FIELD = "username" + REQUIRED_FIELDS = ["username", "password"] objects = IntegerUsernameUserManager() diff --git a/tests/auth_tests/models/with_many_to_many.py b/tests/auth_tests/models/with_many_to_many.py index 82142048ec..0c51365143 100644 --- a/tests/auth_tests/models/with_many_to_many.py +++ b/tests/auth_tests/models/with_many_to_many.py @@ -21,18 +21,18 @@ class CustomUserWithM2M(AbstractBaseUser): custom_objects = CustomUserWithM2MManager() - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['orgs'] + USERNAME_FIELD = "username" + REQUIRED_FIELDS = ["orgs"] class CustomUserWithM2MThrough(AbstractBaseUser): username = models.CharField(max_length=30, unique=True) - orgs = models.ManyToManyField(Organization, through='Membership') + orgs = models.ManyToManyField(Organization, through="Membership") custom_objects = CustomUserWithM2MManager() - USERNAME_FIELD = 'username' - REQUIRED_FIELDS = ['orgs'] + USERNAME_FIELD = "username" + REQUIRED_FIELDS = ["orgs"] class Membership(models.Model): |
