2010-10-12 15:15:47 +08:00
|
|
|
from django.conf import settings
|
2010-04-09 19:07:17 +08:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2010-09-11 03:38:57 +08:00
|
|
|
from django.contrib.auth.tests.views import AuthViewsTestCase
|
2010-04-09 19:07:17 +08:00
|
|
|
|
2010-09-11 03:38:57 +08:00
|
|
|
class LoginRequiredTestCase(AuthViewsTestCase):
|
2010-04-09 19:07:17 +08:00
|
|
|
"""
|
|
|
|
Tests the login_required decorators
|
|
|
|
"""
|
2010-09-11 03:38:57 +08:00
|
|
|
urls = 'django.contrib.auth.tests.urls'
|
|
|
|
|
2010-04-09 19:07:17 +08:00
|
|
|
def testCallable(self):
|
|
|
|
"""
|
|
|
|
Check that login_required is assignable to callable objects.
|
|
|
|
"""
|
|
|
|
class CallableView(object):
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
login_required(CallableView())
|
2010-10-12 15:15:47 +08:00
|
|
|
|
2010-04-09 19:07:17 +08:00
|
|
|
def testView(self):
|
|
|
|
"""
|
|
|
|
Check that login_required is assignable to normal views.
|
|
|
|
"""
|
|
|
|
def normal_view(request):
|
|
|
|
pass
|
2010-09-11 03:38:57 +08:00
|
|
|
login_required(normal_view)
|
|
|
|
|
2010-10-12 15:15:47 +08:00
|
|
|
def testLoginRequired(self, view_url='/login_required/', login_url=settings.LOGIN_URL):
|
2010-09-11 03:38:57 +08:00
|
|
|
"""
|
|
|
|
Check that login_required works on a simple view wrapped in a
|
|
|
|
login_required decorator.
|
|
|
|
"""
|
|
|
|
response = self.client.get(view_url)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(login_url in response['Location'])
|
2010-09-11 03:38:57 +08:00
|
|
|
self.login()
|
|
|
|
response = self.client.get(view_url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def testLoginRequiredNextUrl(self):
|
|
|
|
"""
|
|
|
|
Check that login_required works on a simple view wrapped in a
|
|
|
|
login_required decorator with a login_url set.
|
|
|
|
"""
|
|
|
|
self.testLoginRequired(view_url='/login_required_login_url/',
|
2010-10-12 15:15:47 +08:00
|
|
|
login_url='/somewhere/')
|