Skip to content
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

Fix combining LoginRequired with other access mixins #208

Merged
merged 3 commits into from
May 31, 2016
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/.coverage.xml
/htmlcov
/.tox
/.cache
dist/
.idea
build/
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Direct Contributors
* Ben Cardy
* Rag Sagar.V
* Lacey Williams Henschel
* Gregory Shikhman
* Mike Bryant

Other Contributors
==================
Expand Down
22 changes: 13 additions & 9 deletions braces/views/_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ def get_redirect_field_name(self):
return self.redirect_field_name

def handle_no_permission(self, request):
if self.raise_exception and not self.redirect_unauthenticated_users:
if (inspect.isclass(self.raise_exception)
and issubclass(self.raise_exception, Exception)):
raise self.raise_exception
if callable(self.raise_exception):
ret = self.raise_exception(request)
if isinstance(ret, (HttpResponse, StreamingHttpResponse)):
return ret
raise PermissionDenied
if self.raise_exception:
if (self.redirect_unauthenticated_users
and not self.request.user.is_authenticated()):
return self.no_permissions_fail(request)
else:
if (inspect.isclass(self.raise_exception)
and issubclass(self.raise_exception, Exception)):
raise self.raise_exception
if callable(self.raise_exception):
ret = self.raise_exception(request)
if isinstance(ret, (HttpResponse, StreamingHttpResponse)):
return ret
raise PermissionDenied

return self.no_permissions_fail(request)

Expand Down
60 changes: 59 additions & 1 deletion tests/test_access_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
SuperuserRequiredView, StaffuserRequiredView,
LoginRequiredView, GroupRequiredView, UserPassesTestView,
UserPassesTestNotImplementedView, AnonymousRequiredView,
SSLRequiredView, RecentLoginRequiredView)
SSLRequiredView, RecentLoginRequiredView,
UserPassesTestLoginRequiredView)


class _TestAccessBasicsMixin(TestViewHelper):
Expand Down Expand Up @@ -246,6 +247,63 @@ def test_anonymous_redirects(self):
assert resp['Location'] == '/accounts/login/?next=/login_required/'


class TestChainedLoginRequiredMixin(TestViewHelper, test.TestCase):
"""
Tests for LoginRequiredMixin combined with another AccessMixin.
"""
view_class = UserPassesTestLoginRequiredView
view_url = '/chained_view/'

def assert_redirect_to_login(self, response):
"""
Check that the response is a redirect to the login view.
"""
assert response.status_code == 302
assert response['Location'] == '/accounts/login/?next=/chained_view/'

def test_anonymous(self):
"""
Check that anonymous users redirect to login by default.
"""
resp = self.dispatch_view(
self.build_request(path=self.view_url))
self.assert_redirect_to_login(resp)

def test_anonymous_raises_exception(self):
"""
Check that when anonymous users hit a view that has only
raise_exception set, they get a PermissionDenied.
"""
with self.assertRaises(PermissionDenied):
self.dispatch_view(
self.build_request(path=self.view_url), raise_exception=True)

def test_authenticated_raises_exception(self):
"""
Check that when authenticated users hit a view that has raise_exception
set, they get a PermissionDenied.
"""
user = UserFactory()
with self.assertRaises(PermissionDenied):
self.dispatch_view(
self.build_request(path=self.view_url, user=user),
raise_exception=True)
with self.assertRaises(PermissionDenied):
self.dispatch_view(
self.build_request(path=self.view_url, user=user),
raise_exception=True, redirect_unauthenticated_users=True)

def test_anonymous_redirects(self):
"""
Check that anonymous users are redirected to login when raise_exception
is overridden by redirect_unauthenticated_users.
"""
resp = self.dispatch_view(
self.build_request(path=self.view_url), raise_exception=True,
redirect_unauthenticated_users=True)
self.assert_redirect_to_login(resp)


class TestAnonymousRequiredMixin(TestViewHelper, test.TestCase):
"""
Tests for AnonymousRequiredMixin.
Expand Down
7 changes: 7 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ def test_func(self, user):
and user.email.endswith('@mydomain.com')


class UserPassesTestLoginRequiredView(views.LoginRequiredMixin,
views.UserPassesTestMixin, OkView):
def test_func(self, user):
return user.is_staff and not user.is_superuser \
and user.email.endswith('@mydomain.com')


class UserPassesTestNotImplementedView(views.UserPassesTestMixin, OkView):
pass

Expand Down