kopia lustrzana https://github.com/wagtail/wagtail
Add test for user_passes_test decorator
rodzic
63cd666fd4
commit
eccb0990bb
|
@ -0,0 +1,6 @@
|
|||
from django.conf.urls import url
|
||||
from wagtail.tests.testapp.views import bob_only_zone
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^bob-only-zone$', bob_only_zone, name='testapp_bob_only_zone'),
|
||||
]
|
|
@ -0,0 +1,12 @@
|
|||
from django.http import HttpResponse
|
||||
|
||||
from wagtail.wagtailadmin.utils import user_passes_test
|
||||
|
||||
|
||||
def user_is_called_bob(user):
|
||||
return user.first_name == 'Bob'
|
||||
|
||||
|
||||
@user_passes_test(user_is_called_bob)
|
||||
def bob_only_zone(request):
|
||||
return HttpResponse("Bobs of the world unite!")
|
|
@ -8,6 +8,8 @@ from wagtail.wagtailsearch import urls as wagtailsearch_urls
|
|||
from wagtail.contrib.wagtailsitemaps.views import sitemap
|
||||
from wagtail.contrib.wagtailapi import urls as wagtailapi_urls
|
||||
|
||||
from wagtail.tests.testapp import urls as testapp_urls
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^admin/', include(wagtailadmin_urls)),
|
||||
|
@ -18,6 +20,8 @@ urlpatterns = [
|
|||
url(r'^api/', include(wagtailapi_urls)),
|
||||
url(r'^sitemap\.xml$', sitemap),
|
||||
|
||||
url(r'^testapp/', include(testapp_urls)),
|
||||
|
||||
# For anything not caught by a more specific rule above, hand over to
|
||||
# Wagtail's serving mechanism
|
||||
url(r'', include(wagtail_urls)),
|
||||
|
|
|
@ -243,3 +243,25 @@ class TestMenuItem(TestCase, WagtailTestUtils):
|
|||
def test_menuitem_reverse_lazy_url_pass(self):
|
||||
menuitem = MenuItem(_('Test'), reverse_lazy('wagtailadmin_home'))
|
||||
self.assertEqual(menuitem.is_active(self.request), True)
|
||||
|
||||
|
||||
class TestUserPassesTestPermissionDecorator(TestCase):
|
||||
"""
|
||||
Test for custom user_passes_test permission decorators.
|
||||
testapp_bob_only_zone is a view configured to only grant access to users with a first_name of Bob
|
||||
"""
|
||||
def test_user_passes_test(self):
|
||||
# create and log in as a user called Bob
|
||||
get_user_model().objects.create_superuser(first_name='Bob', last_name='Mortimer', username='test', email='test@email.com', password='password')
|
||||
self.client.login(username='test', password='password')
|
||||
|
||||
response = self.client.get(reverse('testapp_bob_only_zone'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_user_fails_test(self):
|
||||
# create and log in as a user not called Bob
|
||||
get_user_model().objects.create_superuser(first_name='Vic', last_name='Reeves', username='test', email='test@email.com', password='password')
|
||||
self.client.login(username='test', password='password')
|
||||
|
||||
response = self.client.get(reverse('testapp_bob_only_zone'))
|
||||
self.assertRedirects(response, reverse('wagtailadmin_home'))
|
||||
|
|
Ładowanie…
Reference in New Issue