~ubuntu-branches/ubuntu/trusty/horizon/trusty-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/test/helpers.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-31 17:31:49 UTC
  • mfrom: (1.1.38)
  • Revision ID: package-import@ubuntu.com-20140331173149-f28yjk2s8pt15fqj
Tags: 1:2014.1~rc1-0ubuntu1
* New upstream release candidate (LP: #1288245).
  - d/static/*: Refreshed assets for new upstream release.
* d/theme/*: Refresh Ubuntu theme against Icehouse templates (LP: #1291653).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from django.contrib.auth.middleware import AuthenticationMiddleware  # noqa
26
26
from django.contrib.messages.storage import default_storage  # noqa
27
27
from django.core.handlers import wsgi
 
28
from django.core import urlresolvers
28
29
from django import http
29
30
from django.test.client import RequestFactory  # noqa
30
31
from django.utils.importlib import import_module  # noqa
47
48
from openstack_auth import user
48
49
from openstack_auth import utils
49
50
 
 
51
from horizon import base
 
52
from horizon import conf
50
53
from horizon import middleware
51
54
from horizon.test import helpers as horizon_helpers
52
55
 
106
109
 
107
110
      * A full suite of test data through various attached objects and
108
111
        managers (e.g. ``self.servers``, ``self.user``, etc.). See the
109
 
        docs for :class:`~horizon.tests.test_data.utils.TestData` for more
110
 
        information.
 
112
        docs for
 
113
        :class:`~openstack_dashboard.test.test_data.utils.TestData`
 
114
        for more information.
111
115
      * The ``mox`` mocking framework via ``self.mox``.
112
116
      * A set of request context data via ``self.context``.
113
117
      * A ``RequestFactory`` class which supports Django's ``contrib.messages``
414
418
        'm1.massive': 2,
415
419
    }
416
420
    return sort_order[flavor.name]
 
421
 
 
422
 
 
423
class PluginTestCase(TestCase):
 
424
    """The ``PluginTestCase`` class is for use with tests which deal with the
 
425
    pluggable dashboard and panel configuration, it takes care of backing up
 
426
    and restoring the Horizon configuration.
 
427
    """
 
428
    def setUp(self):
 
429
        super(PluginTestCase, self).setUp()
 
430
        self.old_horizon_config = conf.HORIZON_CONFIG
 
431
        conf.HORIZON_CONFIG = conf.LazySettings()
 
432
        base.Horizon._urls()
 
433
        # Trigger discovery, registration, and URLconf generation if it
 
434
        # hasn't happened yet.
 
435
        self.client.get("/")
 
436
        # Store our original dashboards
 
437
        self._discovered_dashboards = base.Horizon._registry.keys()
 
438
        # Gather up and store our original panels for each dashboard
 
439
        self._discovered_panels = {}
 
440
        for dash in self._discovered_dashboards:
 
441
            panels = base.Horizon._registry[dash]._registry.keys()
 
442
            self._discovered_panels[dash] = panels
 
443
 
 
444
    def tearDown(self):
 
445
        super(PluginTestCase, self).tearDown()
 
446
        conf.HORIZON_CONFIG = self.old_horizon_config
 
447
        # Destroy our singleton and re-create it.
 
448
        base.HorizonSite._instance = None
 
449
        del base.Horizon
 
450
        base.Horizon = base.HorizonSite()
 
451
        # Reload the convenience references to Horizon stored in __init__
 
452
        reload(import_module("horizon"))
 
453
        # Re-register our original dashboards and panels.
 
454
        # This is necessary because autodiscovery only works on the first
 
455
        # import, and calling reload introduces innumerable additional
 
456
        # problems. Manual re-registration is the only good way for testing.
 
457
        for dash in self._discovered_dashboards:
 
458
            base.Horizon.register(dash)
 
459
            for panel in self._discovered_panels[dash]:
 
460
                dash.register(panel)
 
461
        self._reload_urls()
 
462
 
 
463
    def _reload_urls(self):
 
464
        """Clears out the URL caches, reloads the root urls module, and
 
465
        re-triggers the autodiscovery mechanism for Horizon. Allows URLs
 
466
        to be re-calculated after registering new dashboards. Useful
 
467
        only for testing and should never be used on a live site.
 
468
        """
 
469
        urlresolvers.clear_url_caches()
 
470
        reload(import_module(settings.ROOT_URLCONF))
 
471
        base.Horizon._urls()