2015-03-30 10:54:25 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-05-28 15:25:52 +00:00
|
|
|
from django.conf.urls import url
|
2015-03-30 10:54:25 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
from django.test.client import RequestFactory
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import reverse
|
2015-03-30 10:54:25 +00:00
|
|
|
|
|
|
|
from .models import Article
|
|
|
|
|
|
|
|
site = admin.AdminSite(name="test_adminsite")
|
|
|
|
site.register(User)
|
|
|
|
site.register(Article)
|
|
|
|
|
|
|
|
urlpatterns = [
|
2015-05-28 15:25:52 +00:00
|
|
|
url(r'^test_admin/admin/', site.urls),
|
2015-03-30 10:54:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-02-05 20:56:52 +00:00
|
|
|
@override_settings(ROOT_URLCONF='admin_views.test_adminsite')
|
2015-03-30 10:54:25 +00:00
|
|
|
class SiteEachContextTest(TestCase):
|
|
|
|
"""
|
|
|
|
Check each_context contains the documented variables and that available_apps context
|
|
|
|
variable structure is the expected one.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-05 20:56:52 +00:00
|
|
|
cls.u1 = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2015-03-30 10:54:25 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
factory = RequestFactory()
|
|
|
|
request = factory.get(reverse('test_adminsite:index'))
|
|
|
|
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'], '/')
|
|
|
|
self.assertEqual(ctx['has_permission'], True)
|
|
|
|
|
2015-10-13 06:52:49 +00:00
|
|
|
def test_each_context_site_url_with_script_name(self):
|
|
|
|
request = RequestFactory().get(reverse('test_adminsite:index'), SCRIPT_NAME='/my-script-name/')
|
|
|
|
request.user = self.u1
|
|
|
|
self.assertEqual(site.each_context(request)['site_url'], '/my-script-name/')
|
|
|
|
|
2015-03-30 10:54:25 +00: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)
|
|
|
|
self.assertEqual(admin_views['models'][0]['object_name'], 'Article')
|
|
|
|
|
|
|
|
# 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')
|
|
|
|
|
|
|
|
self.assertEqual(auth['app_url'], '/test_admin/admin/auth/')
|
|
|
|
self.assertEqual(auth['has_module_perms'], True)
|
|
|
|
|
|
|
|
self.assertIn('perms', user)
|
|
|
|
self.assertEqual(user['perms']['add'], True)
|
|
|
|
self.assertEqual(user['perms']['change'], True)
|
|
|
|
self.assertEqual(user['perms']['delete'], True)
|
|
|
|
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')
|