2016-09-28 03:37:32 +08:00
|
|
|
from django.contrib.auth.models import User
|
2021-03-10 21:08:37 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2019-10-23 14:04:14 +08:00
|
|
|
from django.test import override_settings
|
2016-09-28 03:37:32 +08:00
|
|
|
|
|
|
|
from .tests import AdminDocsTestCase, TestDataMixin
|
|
|
|
|
|
|
|
|
|
|
|
class XViewMiddlewareTest(TestDataMixin, AdminDocsTestCase):
|
|
|
|
def test_xview_func(self):
|
|
|
|
user = User.objects.get(username="super")
|
|
|
|
response = self.client.head("/xview/func/")
|
|
|
|
self.assertNotIn("X-View", response)
|
|
|
|
self.client.force_login(self.superuser)
|
|
|
|
response = self.client.head("/xview/func/")
|
|
|
|
self.assertIn("X-View", response)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["X-View"], "admin_docs.views.xview")
|
2016-09-28 03:37:32 +08:00
|
|
|
user.is_staff = False
|
|
|
|
user.save()
|
|
|
|
response = self.client.head("/xview/func/")
|
|
|
|
self.assertNotIn("X-View", response)
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_active = False
|
|
|
|
user.save()
|
|
|
|
response = self.client.head("/xview/func/")
|
|
|
|
self.assertNotIn("X-View", response)
|
|
|
|
|
|
|
|
def test_xview_class(self):
|
|
|
|
user = User.objects.get(username="super")
|
|
|
|
response = self.client.head("/xview/class/")
|
|
|
|
self.assertNotIn("X-View", response)
|
|
|
|
self.client.force_login(self.superuser)
|
|
|
|
response = self.client.head("/xview/class/")
|
|
|
|
self.assertIn("X-View", response)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["X-View"], "admin_docs.views.XViewClass")
|
2016-09-28 03:37:32 +08:00
|
|
|
user.is_staff = False
|
|
|
|
user.save()
|
|
|
|
response = self.client.head("/xview/class/")
|
|
|
|
self.assertNotIn("X-View", response)
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_active = False
|
|
|
|
user.save()
|
|
|
|
response = self.client.head("/xview/class/")
|
|
|
|
self.assertNotIn("X-View", response)
|
2018-04-09 01:35:24 +08:00
|
|
|
|
|
|
|
def test_callable_object_view(self):
|
|
|
|
self.client.force_login(self.superuser)
|
|
|
|
response = self.client.head("/xview/callable_object/")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["X-View"], "admin_docs.views.XViewCallableObject"
|
|
|
|
)
|
2019-10-23 14:04:14 +08:00
|
|
|
|
|
|
|
@override_settings(MIDDLEWARE=[])
|
|
|
|
def test_no_auth_middleware(self):
|
|
|
|
msg = (
|
|
|
|
"The XView middleware requires authentication middleware to be "
|
|
|
|
"installed. Edit your MIDDLEWARE setting to insert "
|
|
|
|
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
|
|
|
|
)
|
2021-03-10 21:08:37 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2019-10-23 14:04:14 +08:00
|
|
|
self.client.head("/xview/func/")
|