Add unit tests for customization module

Fixes bug #1077744

Change-Id: I06776131f746e0c3b5f95fed629cb3a60eb1f3d5
This commit is contained in:
Tihomir Trifonov 2013-05-09 22:33:45 +03:00
parent ff573dae88
commit c52edd39bf
5 changed files with 121 additions and 0 deletions

View File

View File

@ -0,0 +1,15 @@
from django.utils.translation import ugettext_lazy as _
import horizon
# Rename "cats" to "wildcats"
cats = horizon.get_dashboard("cats")
cats.name = _("WildCats")
# Disable tigers panel
tigers = cats.get_panel("tigers")
cats.unregister(tigers.__class__)
# Remove dogs dashboard
dogs = horizon.get_dashboard("dogs")
horizon.unregister(dogs.__class__)

View File

@ -0,0 +1,9 @@
import horizon
dogs = horizon.get_dashboard("dogs")
puppies = dogs.get_panel("puppies")
permissions = list(getattr(puppies, 'permissions', []))
permissions.append('horizon.test')
puppies.permissions = tuple(permissions)

View File

@ -98,6 +98,7 @@ TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--nocapture', NOSE_ARGS = ['--nocapture',
'--nologcapture', '--nologcapture',
'--exclude-dir=horizon/conf/', '--exclude-dir=horizon/conf/',
'--exclude-dir=horizon/test/customization',
'--cover-package=horizon', '--cover-package=horizon',
'--cover-inclusive', '--cover-inclusive',
'--all-modules'] '--all-modules']

View File

@ -27,6 +27,7 @@ from django.utils.translation import ugettext_lazy as _
import horizon import horizon
from horizon import base from horizon import base
from horizon import conf
from horizon.test import helpers as test from horizon.test import helpers as test
from horizon.test.test_dashboards.cats.dashboard import Cats from horizon.test.test_dashboards.cats.dashboard import Cats
from horizon.test.test_dashboards.cats.kittens.panel import Kittens from horizon.test.test_dashboards.cats.kittens.panel import Kittens
@ -55,6 +56,7 @@ class AdminPanel(horizon.Panel):
class BaseHorizonTests(test.TestCase): class BaseHorizonTests(test.TestCase):
def setUp(self): def setUp(self):
super(BaseHorizonTests, self).setUp() super(BaseHorizonTests, self).setUp()
# Adjust our horizon config and register our custom dashboards/panels. # Adjust our horizon config and register our custom dashboards/panels.
@ -113,6 +115,7 @@ class BaseHorizonTests(test.TestCase):
class HorizonTests(BaseHorizonTests): class HorizonTests(BaseHorizonTests):
def test_registry(self): def test_registry(self):
""" Verify registration and autodiscovery work correctly. """ Verify registration and autodiscovery work correctly.
@ -164,6 +167,7 @@ class HorizonTests(BaseHorizonTests):
['<Panel: kittens>', ['<Panel: kittens>',
'<Panel: tigers>']) '<Panel: tigers>'])
self.assertEqual(cats.get_absolute_url(), "/cats/") self.assertEqual(cats.get_absolute_url(), "/cats/")
self.assertEqual(cats.name, "Cats")
# Test registering a module with a dashboard that defines panels # Test registering a module with a dashboard that defines panels
# as a panel group. # as a panel group.
@ -292,3 +296,95 @@ class HorizonTests(BaseHorizonTests):
# Restore settings # Restore settings
settings.SECURE_PROXY_SSL_HEADER = None settings.SECURE_PROXY_SSL_HEADER = None
class CustomPanelTests(BaseHorizonTests):
""" Test customization of dashboards and panels
using 'customization_module' to HORIZON_CONFIG.
"""
def setUp(self):
settings.HORIZON_CONFIG['customization_module'] = \
'horizon.test.customization.cust_test1'
# refresh config
conf.HORIZON_CONFIG._setup()
super(CustomPanelTests, self).setUp()
def tearDown(self):
# Restore dash
cats = horizon.get_dashboard("cats")
cats.name = _("Cats")
horizon.register(Dogs)
self._discovered_dashboards.append(Dogs)
Dogs.register(Puppies)
Cats.register(Tigers)
super(CustomPanelTests, self).tearDown()
settings.HORIZON_CONFIG.pop('customization_module')
# refresh config
conf.HORIZON_CONFIG._setup()
def test_customize_dashboard(self):
cats = horizon.get_dashboard("cats")
self.assertEqual(cats.name, "WildCats")
self.assertQuerysetEqual(cats.get_panels(),
['<Panel: kittens>'])
with self.assertRaises(base.NotRegistered):
horizon.get_dashboard("dogs")
class CustomPermissionsTests(BaseHorizonTests):
""" Test customization of permissions on panels
using 'customization_module' to HORIZON_CONFIG.
"""
def setUp(self):
settings.HORIZON_CONFIG['customization_module'] = \
'horizon.test.customization.cust_test2'
# refresh config
conf.HORIZON_CONFIG._setup()
super(CustomPermissionsTests, self).setUp()
def tearDown(self):
# Restore permissions
dogs = horizon.get_dashboard("dogs")
puppies = dogs.get_panel("puppies")
puppies.permissions = tuple([])
super(CustomPermissionsTests, self).tearDown()
settings.HORIZON_CONFIG.pop('customization_module')
# refresh config
conf.HORIZON_CONFIG._setup()
def test_customized_permissions(self):
dogs = horizon.get_dashboard("dogs")
panel = dogs.get_panel('puppies')
# Non-admin user
self.assertQuerysetEqual(self.user.get_all_permissions(), [])
resp = self.client.get(panel.get_absolute_url())
self.assertEqual(resp.status_code, 302)
resp = self.client.get(panel.get_absolute_url(),
follow=False,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(resp.status_code, 401)
# Test customized permissions for logged-in user
resp = self.client.get(panel.get_absolute_url(), follow=True)
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed(resp, "auth/login.html")
self.assertContains(resp, "Login as different user", 1, 200)
# Set roles for admin user
self.set_permissions(permissions=['test'])
resp = self.client.get(panel.get_absolute_url())
self.assertEqual(resp.status_code, 200)
# Test modal form
resp = self.client.get(panel.get_absolute_url(),
follow=False,
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(resp.status_code, 200)