Skip to content

Commit

Permalink
tests: Use realistic custom user model
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jnns authored and bluetech committed Oct 16, 2020
1 parent 33ad7c8 commit a079cd6
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,40 @@ def test_with_live_server(live_server):
def test_custom_user_model(django_testdir, username_field):
django_testdir.create_app_file(
"""
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
class MyCustomUser(AbstractUser):
class MyCustomUserManager(BaseUserManager):
def create_user(self, {username_field}, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
user = self.model({username_field}={username_field}, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, {username_field}, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(
{username_field}={username_field},
password=password,
**extra_fields
)
class MyCustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=100, unique=True)
identifier = models.CharField(unique=True, max_length=100)
is_staff = models.BooleanField(
'staff status',
default=False,
help_text='Designates whether the user can log into this admin site.'
)
USERNAME_FIELD = '%s'
"""
% (username_field),
objects = MyCustomUserManager()
USERNAME_FIELD = '{username_field}'
""".format(username_field=username_field),
"models.py",
)
django_testdir.create_app_file(
Expand Down Expand Up @@ -551,19 +576,13 @@ class Migration(migrations.Migration):
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('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')),
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)),
('email', models.EmailField(error_messages={'unique': 'A user with that email address already exists.'}, max_length=100, unique=True, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('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')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('identifier', models.CharField(unique=True, max_length=100)),
('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')),
('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')),
],
options={
'abstract': False,
'verbose_name': 'user',
'verbose_name_plural': 'users',
},
Expand Down

0 comments on commit a079cd6

Please sign in to comment.