Skip to content

Fix admin client with custom user models #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
- TESTENV=python3.4-1.5-sqlite_file
- TESTENV=python3.4-1.6-sqlite_file
- TESTENV=python3.4-1.7-sqlite_file
- TESTENV=python3.4-master-custom_user_model
- TESTENV=python3.4-master-postgres
- TESTENV=python3.4-master-sqlite
- TESTENV=python3.4-master-sqlite_file
Expand Down
6 changes: 5 additions & 1 deletion generate_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

PYTHON_VERSIONS = ['python2.6', 'python2.7', 'python3.2', 'python3.3', 'python3.4', 'pypy']
DJANGO_VERSIONS = ['1.3', '1.4', '1.5', '1.6', '1.7', 'master']
SETTINGS = ['sqlite', 'sqlite_file', 'mysql_myisam', 'mysql_innodb', 'postgres']
SETTINGS = ['sqlite', 'sqlite_file', 'mysql_myisam', 'mysql_innodb', 'postgres', 'custom_user_model']
DJANGO_REQUIREMENTS = {
'1.3': 'Django==1.3.7',
'1.4': 'Django==1.4.13',
Expand Down Expand Up @@ -57,6 +57,10 @@ def is_valid_env(env):
if env.python_version == 'python2.6' and env.django_version in ('1.7', 'master'):
return False

# Django 1.5+ supprts custom user models
if env.settings == 'custom_user_model' and env.django_version in ('1.3', '1.4'):
return False

return True


Expand Down
31 changes: 21 additions & 10 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .db_reuse import (monkey_patch_creation_for_db_reuse,
monkey_patch_creation_for_db_suffix)
from .django_compat import is_django_unittest
from .lazy_django import skip_if_no_django
from .lazy_django import get_django_version, skip_if_no_django

__all__ = ['_django_db_setup', 'db', 'transactional_db',
'client', 'admin_client', 'rf', 'settings', 'live_server',
Expand Down Expand Up @@ -142,19 +142,30 @@ def client():

@pytest.fixture()
def admin_client(db):
"""A Django test client logged in as an admin user"""
try:
"""
A Django test client logged in as an admin user

"""
has_custom_user_model_support = get_django_version() >= (1, 5)

# When using Django >= 1.5 the username field is variable, so
# get 'username field' by using UserModel.USERNAME_FIELD
if has_custom_user_model_support:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
UserModel = get_user_model()
username_field = UserModel.USERNAME_FIELD
else:
from django.contrib.auth.models import User as UserModel
username_field = 'username'

from django.test.client import Client

try:
User.objects.get(username='admin')
except User.DoesNotExist:
user = User.objects.create_user('admin', '[email protected]',
'password')
UserModel._default_manager.get(**{username_field: 'admin'})
except UserModel.DoesNotExist:
user = UserModel._default_manager.create_user('admin',
'[email protected]',
'password')
user.is_staff = True
user.is_superuser = True
user.save()
Expand Down
3 changes: 3 additions & 0 deletions pytest_django/lazy_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ def django_settings_is_configured():
"pytest_django: failed to load Django settings: %s" % (e.args))

return settings.configured

def get_django_version():
return __import__('django').VERSION
Empty file added tests/custom_user/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tests/custom_user/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models


class MyCustomUserManager(BaseUserManager):
def _create_user(self, username, email, password, is_staff, is_superuser,
**extra_fields):
email = self.normalize_email(email)
user = self.model(identifier=username, email=email, is_staff=is_staff,
is_superuser=is_superuser, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)

def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True,
**extra_fields)


class MyCustomUser(AbstractBaseUser):
identifier = models.CharField(unique=True, max_length=100)
email = models.EmailField(blank=True)
is_staff = models.BooleanField()
is_superuser = models.BooleanField()

USERNAME_FIELD = 'identifier'

objects = MyCustomUserManager()
14 changes: 14 additions & 0 deletions tests/settings_custom_user_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from tests.settings_base import * # noqa

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
},
}

INSTALLED_APPS += [
'tests.custom_user'
]

AUTH_USER_MODEL = 'custom_user.MyCustomUser'
6 changes: 3 additions & 3 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from __future__ import with_statement

import django
import pytest
from django.conf import settings as real_settings
from django.test.client import Client, RequestFactory
Expand All @@ -16,8 +15,8 @@
from .test_database import noop_transactions
from .compat import force_text, urlopen

from pytest_django.lazy_django import get_django_version

django # Avoid pyflakes complaints


def test_client(client):
Expand Down Expand Up @@ -80,7 +79,8 @@ def test_deleted_again(self, settings):

class TestLiveServer:
pytestmark = [
pytest.mark.skipif('django.VERSION[:2] < (1, 4)'),
pytest.mark.skipif(get_django_version() < (1, 4),
reason="Django > 1.3 required"),
pytest.mark.urls('tests.urls_liveserver'),
]

Expand Down
Loading