Skip to content

Commit

Permalink
Fixed tests for older Django releases
Browse files Browse the repository at this point in the history
Also updated example project to be run with Django 1.5+
so it can always use custom user model
  • Loading branch information
lukaszb committed Dec 11, 2013
1 parent 620248b commit 545abc0
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 19 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Authors ordered by first contribution
- Yonel Ceruto <yceruto@abalt.org>
- Luke Faraone <lfaraone@humbughq.com>
- John Wegis <john@presencelearning.com>
- Florentin Sardan <florentin.sardan@gmail.com>
8 changes: 8 additions & 0 deletions example_project/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@


class CustomUser(AbstractUser):
real_username = models.CharField(max_length=120, unique=True)
birth_date = models.DateField(null=True, blank=True)

USERNAME_FIELD = 'real_username'

def save(self, *args, **kwargs):
if not self.real_username:
self.real_username = self.username
return super(CustomUser, self).save(*args, **kwargs)

8 changes: 7 additions & 1 deletion example_project/manage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import django
import os
import sys

if django.VERSION < (1, 5):
sys.stderr.write("ERROR: guardian's example project must be run with "
"Django 1.5 or later!\n")
sys.exit(1)


if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

Expand Down
7 changes: 2 additions & 5 deletions example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'guardian',
'guardian.tests.testapp',
'posts',
'core',
)
if django.VERSION < (1, 3):
INSTALLED_APPS += ('staticfiles',)
Expand Down Expand Up @@ -110,11 +111,7 @@
# are not migrated for tests
SOUTH_TESTS_MIGRATE = TEST_SOUTH

# Django >= 1.5 (earlier versoions would ignore this setting; we don't want this
# however, to be set for earlier versions so we don't relay on it)
if django.VERSION >= (1, 5):
INSTALLED_APPS += ('core',)
AUTH_USER_MODEL = 'core.CustomUser'
AUTH_USER_MODEL = 'core.CustomUser'

try:
from conf.localsettings import *
Expand Down
4 changes: 3 additions & 1 deletion guardian/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,11 @@ def clean_user(self):
"""
identification = self.cleaned_data['user']
user_model = get_user_model()

try:
username_field = user_model.USERNAME_FIELD
except AttributeError:
username_field = 'username'
try:
user = user_model.objects.get(**{username_field: identification})
return user
except user_model.DoesNotExist:
Expand Down
8 changes: 7 additions & 1 deletion guardian/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
'patterns',
'include',
'handler404',
'handler500'
'handler500',
'mock',''
]

try:
from unittest import mock # Since Python 3.3 mock is is in stdlib
except ImportError:
import mock # pyflakes:ignore

# Django 1.5 compatibility utilities, providing support for custom User models.
# Since get_user_model() causes a circular import if called when app models are
# being loaded, the user_model_label should be used when possible, with calls
Expand Down
4 changes: 2 additions & 2 deletions guardian/tests/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def setUp(self):
from django.contrib.auth.models import User
Group.objects.create(pk=1, name='admins')
jack_group = Group.objects.create(pk=2, name='jackGroup')
User.objects.get_or_create(id=settings.ANONYMOUS_USER_ID)
jack = User.objects.create(id=1, username='jack', is_active=True,
User.objects.get_or_create(pk=settings.ANONYMOUS_USER_ID)
jack = User.objects.create(pk=1, username='jack', is_active=True,
is_superuser=False, is_staff=False)
jack.groups.add(jack_group)

Expand Down
2 changes: 1 addition & 1 deletion guardian/tests/conf_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
import mock
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from guardian.compat import mock
from guardian.conf import settings as guardian_settings


Expand Down
11 changes: 8 additions & 3 deletions guardian/tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def setUp(self):
self.user.groups.add(self.group)
self.ctype = ContentType.objects.create(name='foo', model='bar',
app_label='fake-for-guardian-tests')
self.anonymous_user, created = User.objects.get_or_create(
id=settings.ANONYMOUS_USER_ID,
username='AnonymousUser')
try:
self.anonymous_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
except User.DoesNotExist:
self.anonymous_user = User(
id=settings.ANONYMOUS_USER_ID,
username='AnonymousUser',
)
self.anonymous_user.save()


class ObjectPermissionCheckerTest(ObjectPermissionTestCase):
Expand Down
2 changes: 1 addition & 1 deletion guardian/tests/decorators_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import unicode_literals
import mock
from django.conf import settings
from django.contrib.auth.models import Group, AnonymousUser
from django.core.exceptions import PermissionDenied
Expand All @@ -15,6 +14,7 @@
from guardian.compat import get_user_model
from guardian.compat import get_user_model_path
from guardian.compat import get_user_permission_full_codename
from guardian.compat import mock
from guardian.decorators import permission_required, permission_required_or_403
from guardian.exceptions import GuardianError
from guardian.exceptions import WrongAppError
Expand Down
2 changes: 1 addition & 1 deletion guardian/tests/managers_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import unicode_literals
from django.test import TestCase
from guardian.compat import mock
from guardian.managers import UserObjectPermissionManager
from guardian.managers import GroupObjectPermissionManager
import mock


class TestManagers(TestCase):
Expand Down
4 changes: 2 additions & 2 deletions guardian/tests/mixins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.views.generic import View
from mock import Mock

from guardian.compat import get_user_model
from guardian.compat import mock
from guardian.mixins import LoginRequiredMixin
from guardian.mixins import PermissionRequiredMixin

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_permission_required_as_list(self):

global TestView
class SecretView(TestView):
on_permission_check_fail = Mock()
on_permission_check_fail = mock.Mock()

request = self.factory.get('/')
request.user = self.user
Expand Down
22 changes: 21 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ envlist =
py27-django16,
py33-django15,
py33-django16,
custom-user-model,
migrations,
docs,
flakes,
Expand Down Expand Up @@ -129,6 +130,13 @@ deps =
{[django12]deps}
django-grappelli==2.3.5

[testenv:custom-user-model]
basepython = python3.3
deps = {[django16]deps}
changedir = example_project
commands =
{envpython} manage.py test guardian --traceback

[testenv:migrations]
setenv =
GUARDIAN_TEST_SOUTH="yes"
Expand All @@ -143,10 +151,22 @@ commands =
python manage.py test guardian --traceback


[testenv:docs]
[testenv:docs27]
basepython = python2.7
changedir = docs
deps =
sphinx
mock
{[django12]deps}
commands =
sphinx-build -b html -d {envtmpdir}/doctrees . {envtmpdir}/html

[testenv:docs33]
basepython = python3.3
changedir = docs
deps =
sphinx
{[django16]deps}
commands =
sphinx-build -b html -d {envtmpdir}/doctrees . {envtmpdir}/html

Expand Down

0 comments on commit 545abc0

Please sign in to comment.