From eccb0990bb98d7a69441d450f937ac67882a5c92 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 3 Nov 2015 15:26:55 +0000 Subject: [PATCH] Add test for user_passes_test decorator --- wagtail/tests/testapp/urls.py | 6 ++++++ wagtail/tests/testapp/views.py | 12 ++++++++++++ wagtail/tests/urls.py | 4 ++++ wagtail/wagtailadmin/tests/tests.py | 22 ++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 wagtail/tests/testapp/urls.py create mode 100644 wagtail/tests/testapp/views.py diff --git a/wagtail/tests/testapp/urls.py b/wagtail/tests/testapp/urls.py new file mode 100644 index 0000000000..6c2c2e8b0b --- /dev/null +++ b/wagtail/tests/testapp/urls.py @@ -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'), +] diff --git a/wagtail/tests/testapp/views.py b/wagtail/tests/testapp/views.py new file mode 100644 index 0000000000..5bc8dc4b33 --- /dev/null +++ b/wagtail/tests/testapp/views.py @@ -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!") diff --git a/wagtail/tests/urls.py b/wagtail/tests/urls.py index 53ef781a5a..667734f906 100644 --- a/wagtail/tests/urls.py +++ b/wagtail/tests/urls.py @@ -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)), diff --git a/wagtail/wagtailadmin/tests/tests.py b/wagtail/wagtailadmin/tests/tests.py index 7526e2b9d1..35e21f5331 100644 --- a/wagtail/wagtailadmin/tests/tests.py +++ b/wagtail/wagtailadmin/tests/tests.py @@ -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'))