Search code examples
djangopytestdjango-authenticationdjango-userspytest-django

pytest-django: users created with create_user are always authenticated


I'm trying to write a fixture to generate Django users from custom user model. The weird thing is that the newly created user results authenticated by default, while I expect it to be anonymous. What am I doing wrong?

Fixture:

import pytest

@pytest.fixture
def create_user(db, django_user_model):
    def make_user(**kwargs):
        return django_user_model.objects.create_user(**kwargs)
    return make_user

Test the fixture:

@pytest.mark.django_db
def test_fixture_create_user(create_user):
    user = create_user(email='foo@bar.com', password='bar')
    assert user.is_authenticated is True  # <-- this should be False but it's True
    assert user.is_anonymous is True      # <-- this fails

The test fails this way:

E       assert False is True
E        +  where False = <CustomUser: foo@bar.com>.is_anonymous

Custom user model:

class CustomUser(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

CustomUserManager:

class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

Solution

  • There is nothing wrong, mate. The is_authenticated property of AbstractUser is hardcoded to be True. Django's middlewares is doing all the magic and returns request.user as AnonymousUser class instance, which is_authenticated property returns False