Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

raise_exception now raises exception

  • Loading branch information...
commit d5aaac85ba3664039301c7963c048a620a0e9551 1 parent 61a5b7f
@duilio duilio authored
View
8 permission/decorators/class_decorators.py
@@ -34,6 +34,7 @@
from __future__ import with_statement
from functools import wraps
from django.utils.decorators import available_attrs
+from django.core.exceptions import PermissionDenied
from utils import redirect_to_login
from utils import get_object_from_classbased_instance
@@ -51,9 +52,12 @@ def inner(self, request, *args, **kwargs):
obj = get_object_from_classbased_instance(
self, queryset, request, *args, **kwargs
)
-
+
if not request.user.has_perm(perm, obj=obj):
- return redirect_to_login(request, login_url, raise_exception)
+ if raise_exception:
+ raise PermissionDenied
+ else:
+ return redirect_to_login(request, login_url)
return view_func(self, request, *args, **kwargs)
return inner
cls.dispatch = view_wrapper(cls.dispatch)
View
7 permission/decorators/function_decorators.py
@@ -37,7 +37,7 @@
from functools import wraps
from django.http import Http404
from django.utils.decorators import available_attrs
-
+from django.core.exceptions import PermissionDenied
from utils import redirect_to_login
from utils import get_object_from_date_based_view
@@ -67,7 +67,10 @@ def inner(request, *args, **kwargs):
obj = None
if not request.user.has_perm(perm, obj=obj):
- return redirect_to_login(request, login_url, raise_exception)
+ if raise_exception:
+ raise PermissionDenied
+ else:
+ return redirect_to_login(request, login_url)
return view_func(request, *args, **kwargs)
return inner
return wrapper
View
6 permission/decorators/method_decorators.py
@@ -36,6 +36,7 @@
from functools import wraps
from django.http import HttpRequest
from django.utils.decorators import available_attrs
+from django.core.exceptions import PermissionDenied
from utils import redirect_to_login
from utils import get_object_from_classbased_instance
@@ -63,7 +64,10 @@ def inner(self, request=None, *args, **kwargs):
)
if not request.user.has_perm(perm, obj=obj):
- return redirect_to_login(request, login_url, raise_exception)
+ if raise_exception:
+ raise PermissionDenied
+ else:
+ return redirect_to_login(request, login_url)
return view_method(self, request, *args, **kwargs)
return inner
return wrapper
View
15 permission/tests/test_decorators/test_class_decorators.py
@@ -35,6 +35,7 @@
from django.test import TestCase
from django.http import HttpResponse
from django.views.generic import View
+from django.core.exceptions import PermissionDenied
from permission import registry
from permission.decorators.class_decorators import permission_required
@@ -72,7 +73,15 @@ def setUp(self):
view_class = type('MockView', (View,), {})
view_class.dispatch = self.view_func
view_class = permission_required('permission.add_article')(view_class)
+
+ view_class_exc = type('MockView', (View,), {})
+ view_class_exc.dispatch = self.view_func
+ view_class_exc = \
+ permission_required('permission.add_article',
+ raise_exception=True)(view_class_exc)
+
self.view_class = view_class
+ self.view_class_exc = view_class_exc
def tearDown(self):
# restore original reigstry
@@ -93,6 +102,12 @@ def test_with_object(self):
obj=Article.objects.get(pk=1)
)
self.assertFalse(self.view_func.called)
+
+ self.view_class_exc.object = Article.objects.get(pk=1)
+ self.assertRaises(PermissionDenied, self.view_class_exc.as_view(),
+ self.mock_request, pk=1)
+ self.assertFalse(self.view_func.called)
+
# has_perm always return True
self.mock_handler.has_perm.return_value = True
self.view_class.as_view()(self.mock_request, pk=1)
View
9 permission/tests/test_decorators/test_function_decorators.py
@@ -34,6 +34,7 @@
from mock import MagicMock as Mock
from django.test import TestCase
from django.http import HttpResponse
+from django.core.exceptions import PermissionDenied
from permission import registry
from permission.decorators.function_decorators import permission_required
@@ -69,6 +70,8 @@ def setUp(self):
)
self.view_func = Mock(return_value=HttpResponse())
self.decorated = permission_required('permission.add_article')(self.view_func)
+ self.decorated_exc = permission_required('permission.add_article',
+ raise_exception=True)(self.view_func)
def tearDown(self):
# restore original reigstry
@@ -92,6 +95,12 @@ def test_list_detail_object_id(self):
obj=Article.objects.get(pk=1)
)
self.assertFalse(self.view_func.called)
+
+ self.assertRaises(PermissionDenied, self.decorated_exc,
+ self.mock_request, queryset=Article.objects.all(),
+ object_id=1)
+ self.assertFalse(self.view_func.called)
+
# has_perm always return True
self.mock_handler.has_perm.return_value = True
self.decorated(
View
15 permission/tests/test_decorators/test_method_decorators.py
@@ -35,6 +35,7 @@
from django.test import TestCase
from django.http import HttpResponse
from django.views.generic import View
+from django.core.exceptions import PermissionDenied
from permission import registry
from permission.decorators.method_decorators import permission_required
@@ -72,7 +73,13 @@ def setUp(self):
view_class = type('MockView', (View,), {})
view_class.dispatch = self.view_func
view_class.dispatch = permission_required('permission.add_article')(view_class.dispatch)
- self.view_class = view_class
+
+ view_class_exc = type('MockView', (View,), {})
+ view_class_exc.dispatch = self.view_func
+ view_class_exc.dispatch = permission_required('permission.add_article',
+ raise_exception=True)(view_class_exc.dispatch)
+
+ self.view_class_exc = view_class_exc
def tearDown(self):
# restore original reigstry
@@ -93,6 +100,12 @@ def test_with_object(self):
obj=Article.objects.get(pk=1)
)
self.assertFalse(self.view_func.called)
+
+ self.assertRaises(PermissionDenied,
+ self.view_class_exc.as_view(),
+ self.mock_request, pk=1)
+ self.assertFalse(self.view_func.called)
+
# has_perm always return True
self.mock_handler.has_perm.return_value = True
self.view_class.as_view()(self.mock_request, pk=1)
Please sign in to comment.
Something went wrong with that request. Please try again.