From cfe7dc8f9c2dfb1241f6005f5553f5f5821bbf79 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:50:57 -0500 Subject: [PATCH 1/7] refs #2: Updated travis. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e616c83..1def32f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,11 @@ cache: env: matrix: - TOXENV=py27-dj18 - - TOXENV=py33-dj18 - TOXENV=py34-dj18 - TOXENV=py35-dj18 - TOXENV=py27-dj111 + - TOXENV=py34-dj111 + - TOXENV=py35-dj111 install: - pip install tox codecov script: tox -v From 89156ced7a283871a0ed57b56e070113d529fc45 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:51:10 -0500 Subject: [PATCH 2/7] refs #2: Updated command to run tests. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9030fe8..fc98f9b 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,7 @@ basepython = py34: python3.4 py35: python3.5 usedevelop = true -commands = python example/manage.py test authority +commands = python example/manage.py test authority exampleapp deps = dj18: Django<1.9 dj19: Django<1.10 From 7c3756a980a386596a2fffcc3a3a2e8509397450 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:51:45 -0500 Subject: [PATCH 3/7] refs #2: Added a test showing permission_required is busted. --- example/exampleapp/tests.py | 39 ++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/example/exampleapp/tests.py b/example/exampleapp/tests.py index 2247054..36b0ba2 100644 --- a/example/exampleapp/tests.py +++ b/example/exampleapp/tests.py @@ -1,23 +1,26 @@ -""" -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". - -Replace these with more appropriate tests for your application. -""" - +from django.core.urlresolvers import reverse from django.test import TestCase -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.failUnlessEqual(1 + 1, 2) +from authority.compat import get_user_model -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. ->>> 1 + 1 == 2 -True -"""} +class AddPermissionTestCase(TestCase): + def test_add_permission_permission_denied_is_403(self): + user = get_user_model().objects.create( + username='foo', + email='foo@example.com', + ) + user.set_password('pw') + user.save() + assert self.client.login(username='foo@example.com', password='pw') + url = reverse( + 'authority-add-permission-request', + kwargs={ + 'app_label': 'foo', + 'module_name': 'Bar', + 'pk': 1, + }, + ) + r = self.client.get(url) + self.assertEqual(r.status_code, 403) From 6c7bf2f15fef55b07a1abbf2925cc4d04753757b Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:52:10 -0500 Subject: [PATCH 4/7] refs #2: Custom user modal needs a default manager. --- example/users/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/example/users/models.py b/example/users/models.py index 5ebd69b..8d83f56 100644 --- a/example/users/models.py +++ b/example/users/models.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin +from django.contrib.auth.models import UserManager from django.db import models from django.utils import timezone @@ -15,3 +16,5 @@ class User(AbstractBaseUser, PermissionsMixin): is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) date_joined = models.DateTimeField(default=timezone.now) + + objects = UserManager() From 69ab665fcc28c9f2d2c4ba21765a6df5e5d61d9f Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:52:29 -0500 Subject: [PATCH 5/7] refs #2: Updated settings. --- example/settings.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/example/settings.py b/example/settings.py index f438c00..62bccf0 100644 --- a/example/settings.py +++ b/example/settings.py @@ -54,11 +54,6 @@ INTERNAL_IPS = ('127.0.0.1',) TEMPLATE_CONTEXT_PROCESSORS = ( - 'django.core.context_processors.auth', - 'django.core.context_processors.debug', - 'django.core.context_processors.i18n', - 'django.core.context_processors.media', - 'django.core.context_processors.request', ) ROOT_URLCONF = 'example.urls' @@ -80,14 +75,28 @@ if VERSION >= (1, 5): INSTALLED_APPS = INSTALLED_APPS + ('example.users',) AUTH_USER_MODEL = 'users.User' -TEMPLATE_LOADERS = ( - 'django.template.loaders.filesystem.load_template_source', - 'django.template.loaders.app_directories.load_template_source', -) - -TEMPLATE_DIRS = ( - os.path.join(PROJECT_ROOT, "templates"), -) +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + # insert your TEMPLATE_DIRS here + ], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this + # list if you haven't customized them: + 'django.contrib.auth.context_processors.auth', + 'django.template.context_processors.debug', + 'django.template.context_processors.i18n', + 'django.template.context_processors.media', + 'django.template.context_processors.static', + 'django.template.context_processors.tz', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] # Use local_settings.py for things to override privately try: From d9401ec3543be6e2c3aacc3446954822ed3acebf Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:52:50 -0500 Subject: [PATCH 6/7] refs #2: Stop the exception from being raised. --- authority/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/authority/views.py b/authority/views.py index c833d2b..ea6734c 100644 --- a/authority/views.py +++ b/authority/views.py @@ -25,8 +25,9 @@ def add_permission(request, app_label, module_name, pk, approved=False, template_name='authority/permission_form.html', extra_context=None, form_class=UserPermissionForm): codename = request.POST.get('codename', None) - model = apps.get_model(app_label, module_name) - if model is None: + try: + model = apps.get_model(app_label, module_name) + except LookupError: return permission_denied(request) obj = get_object_or_404(model, pk=pk) next = get_next(request, obj) From 0c47ab2f4e4fb7b775c5c35b93b275d858e32741 Mon Sep 17 00:00:00 2001 From: Jason Ward Date: Mon, 20 Nov 2017 14:53:05 -0500 Subject: [PATCH 7/7] refs #2: Fixed a problem with named parameters. --- authority/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/authority/views.py b/authority/views.py index ea6734c..d97e1be 100644 --- a/authority/views.py +++ b/authority/views.py @@ -109,5 +109,8 @@ def permission_denied(request, template_name=None, extra_context=None): } if extra_context: context.update(extra_context) - return HttpResponseForbidden(loader.render_to_string(template_name, - context, request)) + return HttpResponseForbidden(loader.render_to_string( + template_name=template_name, + context=context, + request=request, + ))