-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dmpayton/master
SudoMixin for class-based views
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from sudo.decorators import sudo_required | ||
|
||
|
||
class SudoMixin(object): | ||
@classmethod | ||
def as_view(cls, **initkwargs): | ||
view = super(SudoMixin, cls).as_view(**initkwargs) | ||
return sudo_required(view) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from .base import BaseTestCase | ||
from django.http import HttpResponse | ||
from django.views import generic | ||
from sudo.mixins import SudoMixin | ||
|
||
|
||
class FooView(SudoMixin, generic.View): | ||
def get(self, request): | ||
return HttpResponse() | ||
|
||
foo = FooView.as_view() | ||
|
||
|
||
class SudoMixinTestCase(BaseTestCase): | ||
def test_is_sudo(self): | ||
self.request.is_sudo = lambda: True | ||
response = foo(self.request) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_is_not_sudo(self): | ||
self.request.is_sudo = lambda: False | ||
response = foo(self.request) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(response['Location'], '/sudo/?next=/foo') |