2015-03-30 18:54:25 +08:00
|
|
|
from django.contrib import admin
|
2017-06-20 02:50:12 +08:00
|
|
|
from django.contrib.admin.actions import delete_selected
|
2015-03-30 18:54:25 +08:00
|
|
|
from django.contrib.auth.models import User
|
2017-06-20 02:50:12 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings
|
2015-03-30 18:54:25 +08:00
|
|
|
from django.test.client import RequestFactory
|
2018-12-08 06:52:28 +08:00
|
|
|
from django.urls import path, reverse
|
2015-03-30 18:54:25 +08:00
|
|
|
|
|
|
|
from .models import Article
|
|
|
|
|
|
|
|
site = admin.AdminSite(name="test_adminsite")
|
|
|
|
site.register(User)
|
|
|
|
site.register(Article)
|
|
|
|
|
|
|
|
urlpatterns = [
|
2018-12-08 06:52:28 +08:00
|
|
|
path('test_admin/admin/', site.urls),
|
2015-03-30 18:54:25 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_views.test_adminsite')
|
2015-03-30 18:54:25 +08:00
|
|
|
class SiteEachContextTest(TestCase):
|
|
|
|
"""
|
|
|
|
Check each_context contains the documented variables and that available_apps context
|
|
|
|
variable structure is the expected one.
|
|
|
|
"""
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
|
|
|
|
2015-03-30 18:54:25 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-06 04:56:52 +08:00
|
|
|
cls.u1 = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2015-03-30 18:54:25 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.get(reverse('test_adminsite:index'))
|
2015-03-30 18:54:25 +08:00
|
|
|
request.user = self.u1
|
|
|
|
self.ctx = site.each_context(request)
|
|
|
|
|
|
|
|
def test_each_context(self):
|
|
|
|
ctx = self.ctx
|
|
|
|
self.assertEqual(ctx['site_header'], 'Django administration')
|
|
|
|
self.assertEqual(ctx['site_title'], 'Django site admin')
|
|
|
|
self.assertEqual(ctx['site_url'], '/')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(ctx['has_permission'], True)
|
2015-03-30 18:54:25 +08:00
|
|
|
|
2015-10-13 14:52:49 +08:00
|
|
|
def test_each_context_site_url_with_script_name(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.get(reverse('test_adminsite:index'), SCRIPT_NAME='/my-script-name/')
|
2015-10-13 14:52:49 +08:00
|
|
|
request.user = self.u1
|
|
|
|
self.assertEqual(site.each_context(request)['site_url'], '/my-script-name/')
|
|
|
|
|
2015-03-30 18:54:25 +08:00
|
|
|
def test_available_apps(self):
|
|
|
|
ctx = self.ctx
|
|
|
|
apps = ctx['available_apps']
|
|
|
|
# we have registered two models from two different apps
|
|
|
|
self.assertEqual(len(apps), 2)
|
|
|
|
|
|
|
|
# admin_views.Article
|
|
|
|
admin_views = apps[0]
|
|
|
|
self.assertEqual(admin_views['app_label'], 'admin_views')
|
|
|
|
self.assertEqual(len(admin_views['models']), 1)
|
2020-09-22 16:46:27 +08:00
|
|
|
article = admin_views['models'][0]
|
|
|
|
self.assertEqual(article['object_name'], 'Article')
|
|
|
|
self.assertEqual(article['model'], Article)
|
2015-03-30 18:54:25 +08:00
|
|
|
|
|
|
|
# auth.User
|
|
|
|
auth = apps[1]
|
|
|
|
self.assertEqual(auth['app_label'], 'auth')
|
|
|
|
self.assertEqual(len(auth['models']), 1)
|
|
|
|
user = auth['models'][0]
|
|
|
|
self.assertEqual(user['object_name'], 'User')
|
2020-09-22 16:46:27 +08:00
|
|
|
self.assertEqual(user['model'], User)
|
2015-03-30 18:54:25 +08:00
|
|
|
|
|
|
|
self.assertEqual(auth['app_url'], '/test_admin/admin/auth/')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(auth['has_module_perms'], True)
|
2015-03-30 18:54:25 +08:00
|
|
|
|
|
|
|
self.assertIn('perms', user)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(user['perms']['add'], True)
|
|
|
|
self.assertIs(user['perms']['change'], True)
|
|
|
|
self.assertIs(user['perms']['delete'], True)
|
2015-03-30 18:54:25 +08:00
|
|
|
self.assertEqual(user['admin_url'], '/test_admin/admin/auth/user/')
|
|
|
|
self.assertEqual(user['add_url'], '/test_admin/admin/auth/user/add/')
|
|
|
|
self.assertEqual(user['name'], 'Users')
|
2017-06-20 02:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SiteActionsTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.site = admin.AdminSite()
|
|
|
|
|
|
|
|
def test_add_action(self):
|
|
|
|
def test_action():
|
|
|
|
pass
|
|
|
|
self.site.add_action(test_action)
|
|
|
|
self.assertEqual(self.site.get_action('test_action'), test_action)
|
|
|
|
|
|
|
|
def test_disable_action(self):
|
|
|
|
action_name = 'delete_selected'
|
|
|
|
self.assertEqual(self.site._actions[action_name], delete_selected)
|
|
|
|
self.site.disable_action(action_name)
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
self.site._actions[action_name]
|
|
|
|
|
|
|
|
def test_get_action(self):
|
|
|
|
"""AdminSite.get_action() returns an action even if it's disabled."""
|
|
|
|
action_name = 'delete_selected'
|
|
|
|
self.assertEqual(self.site.get_action(action_name), delete_selected)
|
|
|
|
self.site.disable_action(action_name)
|
|
|
|
self.assertEqual(self.site.get_action(action_name), delete_selected)
|