Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from duilio/raise_exception_fix

raise_exception now raises exception
  • Loading branch information...
commit 216b84cdfe5afde2a7828c2767b0c1545c61a855 2 parents 61a5b7f + d5aaac8
Maurizio Sambati authored
8 permission/decorators/class_decorators.py
@@ -34,6 +34,7 @@
34 34 from __future__ import with_statement
35 35 from functools import wraps
36 36 from django.utils.decorators import available_attrs
  37 +from django.core.exceptions import PermissionDenied
37 38
38 39 from utils import redirect_to_login
39 40 from utils import get_object_from_classbased_instance
@@ -51,9 +52,12 @@ def inner(self, request, *args, **kwargs):
51 52 obj = get_object_from_classbased_instance(
52 53 self, queryset, request, *args, **kwargs
53 54 )
54   -
  55 +
55 56 if not request.user.has_perm(perm, obj=obj):
56   - return redirect_to_login(request, login_url, raise_exception)
  57 + if raise_exception:
  58 + raise PermissionDenied
  59 + else:
  60 + return redirect_to_login(request, login_url)
57 61 return view_func(self, request, *args, **kwargs)
58 62 return inner
59 63 cls.dispatch = view_wrapper(cls.dispatch)
7 permission/decorators/function_decorators.py
@@ -37,7 +37,7 @@
37 37 from functools import wraps
38 38 from django.http import Http404
39 39 from django.utils.decorators import available_attrs
40   -
  40 +from django.core.exceptions import PermissionDenied
41 41
42 42 from utils import redirect_to_login
43 43 from utils import get_object_from_date_based_view
@@ -67,7 +67,10 @@ def inner(request, *args, **kwargs):
67 67 obj = None
68 68
69 69 if not request.user.has_perm(perm, obj=obj):
70   - return redirect_to_login(request, login_url, raise_exception)
  70 + if raise_exception:
  71 + raise PermissionDenied
  72 + else:
  73 + return redirect_to_login(request, login_url)
71 74 return view_func(request, *args, **kwargs)
72 75 return inner
73 76 return wrapper
6 permission/decorators/method_decorators.py
@@ -36,6 +36,7 @@
36 36 from functools import wraps
37 37 from django.http import HttpRequest
38 38 from django.utils.decorators import available_attrs
  39 +from django.core.exceptions import PermissionDenied
39 40
40 41 from utils import redirect_to_login
41 42 from utils import get_object_from_classbased_instance
@@ -63,7 +64,10 @@ def inner(self, request=None, *args, **kwargs):
63 64 )
64 65
65 66 if not request.user.has_perm(perm, obj=obj):
66   - return redirect_to_login(request, login_url, raise_exception)
  67 + if raise_exception:
  68 + raise PermissionDenied
  69 + else:
  70 + return redirect_to_login(request, login_url)
67 71 return view_method(self, request, *args, **kwargs)
68 72 return inner
69 73 return wrapper
15 permission/tests/test_decorators/test_class_decorators.py
@@ -35,6 +35,7 @@
35 35 from django.test import TestCase
36 36 from django.http import HttpResponse
37 37 from django.views.generic import View
  38 +from django.core.exceptions import PermissionDenied
38 39
39 40 from permission import registry
40 41 from permission.decorators.class_decorators import permission_required
@@ -72,7 +73,15 @@ def setUp(self):
72 73 view_class = type('MockView', (View,), {})
73 74 view_class.dispatch = self.view_func
74 75 view_class = permission_required('permission.add_article')(view_class)
  76 +
  77 + view_class_exc = type('MockView', (View,), {})
  78 + view_class_exc.dispatch = self.view_func
  79 + view_class_exc = \
  80 + permission_required('permission.add_article',
  81 + raise_exception=True)(view_class_exc)
  82 +
75 83 self.view_class = view_class
  84 + self.view_class_exc = view_class_exc
76 85
77 86 def tearDown(self):
78 87 # restore original reigstry
@@ -93,6 +102,12 @@ def test_with_object(self):
93 102 obj=Article.objects.get(pk=1)
94 103 )
95 104 self.assertFalse(self.view_func.called)
  105 +
  106 + self.view_class_exc.object = Article.objects.get(pk=1)
  107 + self.assertRaises(PermissionDenied, self.view_class_exc.as_view(),
  108 + self.mock_request, pk=1)
  109 + self.assertFalse(self.view_func.called)
  110 +
96 111 # has_perm always return True
97 112 self.mock_handler.has_perm.return_value = True
98 113 self.view_class.as_view()(self.mock_request, pk=1)
9 permission/tests/test_decorators/test_function_decorators.py
@@ -34,6 +34,7 @@
34 34 from mock import MagicMock as Mock
35 35 from django.test import TestCase
36 36 from django.http import HttpResponse
  37 +from django.core.exceptions import PermissionDenied
37 38
38 39 from permission import registry
39 40 from permission.decorators.function_decorators import permission_required
@@ -69,6 +70,8 @@ def setUp(self):
69 70 )
70 71 self.view_func = Mock(return_value=HttpResponse())
71 72 self.decorated = permission_required('permission.add_article')(self.view_func)
  73 + self.decorated_exc = permission_required('permission.add_article',
  74 + raise_exception=True)(self.view_func)
72 75
73 76 def tearDown(self):
74 77 # restore original reigstry
@@ -92,6 +95,12 @@ def test_list_detail_object_id(self):
92 95 obj=Article.objects.get(pk=1)
93 96 )
94 97 self.assertFalse(self.view_func.called)
  98 +
  99 + self.assertRaises(PermissionDenied, self.decorated_exc,
  100 + self.mock_request, queryset=Article.objects.all(),
  101 + object_id=1)
  102 + self.assertFalse(self.view_func.called)
  103 +
95 104 # has_perm always return True
96 105 self.mock_handler.has_perm.return_value = True
97 106 self.decorated(
15 permission/tests/test_decorators/test_method_decorators.py
@@ -35,6 +35,7 @@
35 35 from django.test import TestCase
36 36 from django.http import HttpResponse
37 37 from django.views.generic import View
  38 +from django.core.exceptions import PermissionDenied
38 39
39 40 from permission import registry
40 41 from permission.decorators.method_decorators import permission_required
@@ -72,7 +73,13 @@ def setUp(self):
72 73 view_class = type('MockView', (View,), {})
73 74 view_class.dispatch = self.view_func
74 75 view_class.dispatch = permission_required('permission.add_article')(view_class.dispatch)
75   - self.view_class = view_class
  76 +
  77 + view_class_exc = type('MockView', (View,), {})
  78 + view_class_exc.dispatch = self.view_func
  79 + view_class_exc.dispatch = permission_required('permission.add_article',
  80 + raise_exception=True)(view_class_exc.dispatch)
  81 +
  82 + self.view_class_exc = view_class_exc
76 83
77 84 def tearDown(self):
78 85 # restore original reigstry
@@ -93,6 +100,12 @@ def test_with_object(self):
93 100 obj=Article.objects.get(pk=1)
94 101 )
95 102 self.assertFalse(self.view_func.called)
  103 +
  104 + self.assertRaises(PermissionDenied,
  105 + self.view_class_exc.as_view(),
  106 + self.mock_request, pk=1)
  107 + self.assertFalse(self.view_func.called)
  108 +
96 109 # has_perm always return True
97 110 self.mock_handler.has_perm.return_value = True
98 111 self.view_class.as_view()(self.mock_request, pk=1)

0 comments on commit 216b84c

Please sign in to comment.
Something went wrong with that request. Please try again.