Skip to content

Commit a079cd6

Browse files
jnnsbluetech
authored andcommitted
tests: Use realistic custom user model
The previous test setup inherited from AbstractUser thus MyCustomUser still had a username field. The problems people are having when using the admin_client fixture in combination with a custom user model are due to the username field not being present. This change accounts for the more realistic scenario.
1 parent 33ad7c8 commit a079cd6

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

tests/test_fixtures.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,40 @@ def test_with_live_server(live_server):
483483
def test_custom_user_model(django_testdir, username_field):
484484
django_testdir.create_app_file(
485485
"""
486-
from django.contrib.auth.models import AbstractUser
486+
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
487487
from django.db import models
488488
489-
class MyCustomUser(AbstractUser):
489+
class MyCustomUserManager(BaseUserManager):
490+
def create_user(self, {username_field}, password=None, **extra_fields):
491+
extra_fields.setdefault('is_staff', False)
492+
extra_fields.setdefault('is_superuser', False)
493+
user = self.model({username_field}={username_field}, **extra_fields)
494+
user.set_password(password)
495+
user.save()
496+
return user
497+
498+
def create_superuser(self, {username_field}, password=None, **extra_fields):
499+
extra_fields.setdefault('is_staff', True)
500+
extra_fields.setdefault('is_superuser', True)
501+
return self.create_user(
502+
{username_field}={username_field},
503+
password=password,
504+
**extra_fields
505+
)
506+
507+
class MyCustomUser(AbstractBaseUser, PermissionsMixin):
508+
email = models.EmailField(max_length=100, unique=True)
490509
identifier = models.CharField(unique=True, max_length=100)
510+
is_staff = models.BooleanField(
511+
'staff status',
512+
default=False,
513+
help_text='Designates whether the user can log into this admin site.'
514+
)
491515
492-
USERNAME_FIELD = '%s'
493-
"""
494-
% (username_field),
516+
objects = MyCustomUserManager()
517+
518+
USERNAME_FIELD = '{username_field}'
519+
""".format(username_field=username_field),
495520
"models.py",
496521
)
497522
django_testdir.create_app_file(
@@ -551,19 +576,13 @@ class Migration(migrations.Migration):
551576
('password', models.CharField(max_length=128, verbose_name='password')),
552577
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
553578
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
554-
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator(r'^[\\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')),
555-
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
556-
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
557-
('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)),
579+
('email', models.EmailField(error_messages={'unique': 'A user with that email address already exists.'}, max_length=100, unique=True, verbose_name='email address')),
558580
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
559-
('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')),
560-
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
561581
('identifier', models.CharField(unique=True, max_length=100)),
562582
('groups', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', verbose_name='groups')),
563583
('user_permissions', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Permission', blank=True, help_text='Specific permissions for this user.', verbose_name='user permissions')),
564584
],
565585
options={
566-
'abstract': False,
567586
'verbose_name': 'user',
568587
'verbose_name_plural': 'users',
569588
},

0 commit comments

Comments
 (0)