Skip to content

Commit 8b45607

Browse files
committed
Add whole custom user model in test_custom_user_model_email_as_username because we had to use custom email field which was not possible when inheriting from AbstractUser
1 parent 9dfe306 commit 8b45607

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

tests/test_fixtures.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,43 @@ def test_mail_again(mailoutbox):
497497
""")
498498
def test_custom_user_model_email_as_username(django_testdir):
499499
django_testdir.create_app_file("""
500-
from django.contrib.auth.models import AbstractUser
500+
from django.contrib.auth.models import (AbstractBaseUser, BaseUserManager,
501+
PermissionsMixin)
502+
from django.utils import timezone
501503
from django.db import models
502504
503-
class MyCustomUser(AbstractUser):
504-
email = models.EmailField(unique=True)
505+
class UserManager(BaseUserManager):
506+
use_in_migrations = True
507+
508+
def _create_user(self, username, email, password, **extra_fields):
509+
email = self.normalize_email(email)
510+
user = self.model(username=username, email=email, **extra_fields)
511+
user.set_password(password)
512+
user.save(using=self._db)
513+
return user
514+
515+
def create_user(self, username, email, password=None, **extra_fields):
516+
extra_fields.setdefault('is_superuser', False)
517+
return self._create_user(username, email, password, **extra_fields)
518+
519+
def create_superuser(self, username, email, password, **extra_fields):
520+
extra_fields.setdefault('is_superuser', True)
521+
extra_fields.setdefault('is_staff', True)
522+
523+
return self._create_user(username, email, password, **extra_fields)
524+
525+
526+
class MyCustomUser(AbstractBaseUser, PermissionsMixin):
527+
username = models.CharField(max_length=30, unique=True)
528+
email = models.EmailField(blank=False, unique=True)
529+
is_staff = models.BooleanField(default=False)
530+
is_active = models.BooleanField(default=True)
531+
date_joined = models.DateTimeField(default=timezone.now)
532+
533+
objects = UserManager()
505534
506535
USERNAME_FIELD = 'email'
536+
REQUIRED_FIELDS = ['username']
507537
""", 'models.py')
508538
django_testdir.create_app_file("""
509539
from django.conf.urls import url
@@ -563,8 +593,6 @@ class Migration(migrations.Migration):
563593
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
564594
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
565595
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, verbose_name='username')),
566-
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
567-
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
568596
('email', models.EmailField(max_length=254, unique=True)),
569597
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
570598
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),

0 commit comments

Comments
 (0)