'])
**** CubicPower OpenStack Study ****
def test_panels(self):
cats = horizon.get_dashboard("cats")
tigers = cats.get_panel("tigers")
self.assertEqual(tigers._registered_with, cats)
self.assertEqual(tigers.get_absolute_url(), "/cats/tigers/")
**** CubicPower OpenStack Study ****
def test_panel_without_slug_fails(self):
class InvalidPanel(horizon.Panel):
name = 'Invalid'
self.assertRaises(ImproperlyConfigured, InvalidPanel)
**** CubicPower OpenStack Study ****
def test_registry_without_registerable_class_attr_fails(self):
class InvalidRegistry(base.Registry):
pass
self.assertRaises(ImproperlyConfigured, InvalidRegistry)
**** CubicPower OpenStack Study ****
def test_index_url_name(self):
cats = horizon.get_dashboard("cats")
tigers = cats.get_panel("tigers")
tigers.index_url_name = "does_not_exist"
with self.assertRaises(urlresolvers.NoReverseMatch):
tigers.get_absolute_url()
tigers.index_url_name = "index"
self.assertEqual(tigers.get_absolute_url(), "/cats/tigers/")
**** CubicPower OpenStack Study ****
def test_lazy_urls(self):
urlpatterns = horizon.urls[0]
self.assertIsInstance(urlpatterns, base.LazyURLPattern)
# The following two methods simply should not raise any exceptions
iter(urlpatterns)
reversed(urlpatterns)
**** CubicPower OpenStack Study ****
def test_horizon_test_isolation_1(self):
"""Isolation Test Part 1: sets a value."""
cats = horizon.get_dashboard("cats")
cats.evil = True
**** CubicPower OpenStack Study ****
def test_horizon_test_isolation_2(self):
"""Isolation Test Part 2: The value set in part 1 should be gone."""
cats = horizon.get_dashboard("cats")
self.assertFalse(hasattr(cats, "evil"))
**** CubicPower OpenStack Study ****
def test_public(self):
dogs = horizon.get_dashboard("dogs")
# Known to have no restrictions on it other than being logged in.
puppies = dogs.get_panel("puppies")
url = puppies.get_absolute_url()
# Get a clean, logged out client instance.
self.client.logout()
resp = self.client.get(url)
redirect_url = "?".join(['http://testserver' + settings.LOGIN_URL,
"next=%s" % url])
self.assertRedirects(resp, redirect_url)
# Simulate ajax call
resp = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
# Response should be HTTP 401 with redirect header
self.assertEqual(resp.status_code, 401)
self.assertEqual(resp["X-Horizon-Location"],
redirect_url)
**** CubicPower OpenStack Study ****
def test_required_permissions(self):
dash = horizon.get_dashboard("cats")
panel = dash.get_panel('tigers')
# 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 insufficient 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)
**** CubicPower OpenStack Study ****
def test_ssl_redirect_by_proxy(self):
dogs = horizon.get_dashboard("dogs")
puppies = dogs.get_panel("puppies")
url = puppies.get_absolute_url()
redirect_url = "?".join([settings.LOGIN_URL,
"next=%s" % url])
self.client.logout()
resp = self.client.get(url)
self.assertRedirects(resp, redirect_url)
# Set SSL settings for test server
settings.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL',
'https')
resp = self.client.get(url, HTTP_X_FORWARDED_PROTOCOL="https")
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp['location'],
'https://testserver:80%s' % redirect_url)
# Restore settings
settings.SECURE_PROXY_SSL_HEADER = None
**** CubicPower OpenStack Study ****
class GetUserHomeTests(BaseHorizonTests):
"""Test get_user_home parameters."""
**** CubicPower OpenStack Study ****
def setUp(self):
self.orig_user_home = settings.HORIZON_CONFIG['user_home']
super(BaseHorizonTests, self).setUp()
self.original_username = "testname"
self.test_user = User()
self.test_user.username = self.original_username
**** CubicPower OpenStack Study ****
def tearDown(self):
settings.HORIZON_CONFIG['user_home'] = self.orig_user_home
conf.HORIZON_CONFIG._setup()
**** CubicPower OpenStack Study ****
def test_using_callable(self):
def fancy_user_fnc(user):
return user.username.upper()
settings.HORIZON_CONFIG['user_home'] = fancy_user_fnc
conf.HORIZON_CONFIG._setup()
self.assertEqual(self.test_user.username.upper(),
base.Horizon.get_user_home(self.test_user))
**** CubicPower OpenStack Study ****
def fancy_user_fnc(user):
return user.username.upper()
settings.HORIZON_CONFIG['user_home'] = fancy_user_fnc
conf.HORIZON_CONFIG._setup()
self.assertEqual(self.test_user.username.upper(),
base.Horizon.get_user_home(self.test_user))
**** CubicPower OpenStack Study ****
def test_using_module_function(self):
module_func = 'django.utils.encoding.force_unicode'
settings.HORIZON_CONFIG['user_home'] = module_func
conf.HORIZON_CONFIG._setup()
self.test_user.username = 'testname'
self.assertEqual(self.original_username,
base.Horizon.get_user_home(self.test_user))
**** CubicPower OpenStack Study ****
def test_using_url(self):
fixed_url = "/url"
settings.HORIZON_CONFIG['user_home'] = fixed_url
conf.HORIZON_CONFIG._setup()
self.assertEqual(fixed_url,
base.Horizon.get_user_home(self.test_user))
**** CubicPower OpenStack Study ****
class CustomPanelTests(BaseHorizonTests):
"""Test customization of dashboards and panels
using 'customization_module' to HORIZON_CONFIG.
"""
**** CubicPower OpenStack Study ****
def setUp(self):
settings.HORIZON_CONFIG['customization_module'] = \
'horizon.test.customization.cust_test1'
# refresh config
conf.HORIZON_CONFIG._setup()
super(CustomPanelTests, self).setUp()
**** CubicPower OpenStack Study ****
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()
**** CubicPower OpenStack Study ****
def test_customize_dashboard(self):
cats = horizon.get_dashboard("cats")
self.assertEqual(cats.name, "WildCats")
self.assertQuerysetEqual(cats.get_panels(),
['']) with self.assertRaises(base.NotRegistered):
horizon.get_dashboard("dogs")
**** CubicPower OpenStack Study ****
class CustomPermissionsTests(BaseHorizonTests):
"""Test customization of permissions on panels
using 'customization_module' to HORIZON_CONFIG.
"""
**** CubicPower OpenStack Study ****
def setUp(self):
settings.HORIZON_CONFIG['customization_module'] = \
'horizon.test.customization.cust_test2'
# refresh config
conf.HORIZON_CONFIG._setup()
super(CustomPermissionsTests, self).setUp()
**** CubicPower OpenStack Study ****
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()
**** CubicPower OpenStack Study ****
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)