Make test helpers properly inheritable/extendable

The helpers.TestCase classes didn't call their superclass' methods
in their setUp and tearDown methods, which made it impossible to
probperly inherit from them and use mixins with them.

I split the code that was differing for them into separate methods,
removed common code and made them use super() correctly.

Due to some inspect magic, the factory creation has to be duplicated.

Change-Id: I0bf909b1e4324c1e36bcdb83daece391bd0cfae7
This commit is contained in:
Radomir Dopieralski 2015-02-03 15:09:21 +01:00
parent 680488de6e
commit e0168e773a
2 changed files with 39 additions and 24 deletions

View File

@ -118,18 +118,32 @@ class TestCase(django_test.TestCase):
* A ready-to-go request object via ``self.request``. * A ready-to-go request object via ``self.request``.
""" """
def setUp(self): def setUp(self):
super(TestCase, self).setUp()
self.mox = mox.Mox() self.mox = mox.Mox()
self.factory = RequestFactoryWithMessages() self._setup_test_data()
self.user = User.objects.create_user(username='test', password='test') self._setup_factory()
self.assertTrue(self.client.login(username="test", password="test")) self._setup_user()
self._setup_request()
self.request = http.HttpRequest()
self.request.session = self.client._session()
middleware.HorizonMiddleware().process_request(self.request) middleware.HorizonMiddleware().process_request(self.request)
AuthenticationMiddleware().process_request(self.request) AuthenticationMiddleware().process_request(self.request)
os.environ["HORIZON_TEST_RUN"] = "True" os.environ["HORIZON_TEST_RUN"] = "True"
def _setup_test_data(self):
pass
def _setup_factory(self):
self.factory = RequestFactoryWithMessages()
def _setup_user(self):
self.user = User.objects.create_user(username='test', password='test')
self.assertTrue(self.client.login(username="test", password="test"))
def _setup_request(self):
self.request = http.HttpRequest()
self.request.session = self.client._session()
def tearDown(self): def tearDown(self):
super(TestCase, self).tearDown()
self.mox.UnsetStubs() self.mox.UnsetStubs()
self.mox.VerifyAll() self.mox.VerifyAll()
del os.environ["HORIZON_TEST_RUN"] del os.environ["HORIZON_TEST_RUN"]

View File

@ -24,11 +24,9 @@ import os
from ceilometerclient.v2 import client as ceilometer_client from ceilometerclient.v2 import client as ceilometer_client
from cinderclient import client as cinder_client from cinderclient import client as cinder_client
from django.conf import settings from django.conf import settings
from django.contrib.auth.middleware import AuthenticationMiddleware # noqa
from django.contrib.messages.storage import default_storage # noqa from django.contrib.messages.storage import default_storage # noqa
from django.core.handlers import wsgi from django.core.handlers import wsgi
from django.core import urlresolvers from django.core import urlresolvers
from django import http
from django.test.client import RequestFactory # noqa from django.test.client import RequestFactory # noqa
from django.test import utils as django_test_utils from django.test import utils as django_test_utils
from django.utils.importlib import import_module # noqa from django.utils.importlib import import_module # noqa
@ -49,7 +47,6 @@ from troveclient import client as trove_client
from horizon import base from horizon import base
from horizon import conf from horizon import conf
from horizon import middleware
from horizon.test import helpers as horizon_helpers from horizon.test import helpers as horizon_helpers
from openstack_dashboard import api from openstack_dashboard import api
from openstack_dashboard import context_processors from openstack_dashboard import context_processors
@ -151,11 +148,6 @@ class TestCase(horizon_helpers.TestCase):
* Several handy additional assertion methods. * Several handy additional assertion methods.
""" """
def setUp(self): def setUp(self):
test_utils.load_test_data(self)
self.mox = mox.Mox()
self.factory = RequestFactoryWithMessages()
self.context = {'authorized_tenants': self.tenants.list()}
def fake_conn_request(*args, **kwargs): def fake_conn_request(*args, **kwargs):
raise Exception("An external URI request tried to escape through " raise Exception("An external URI request tried to escape through "
"an httplib2 client. Args: %s, kwargs: %s" "an httplib2 client. Args: %s, kwargs: %s"
@ -167,6 +159,21 @@ class TestCase(horizon_helpers.TestCase):
self._real_context_processor = context_processors.openstack self._real_context_processor = context_processors.openstack
context_processors.openstack = lambda request: self.context context_processors.openstack = lambda request: self.context
self.patchers = {}
self.add_panel_mocks()
super(TestCase, self).setUp()
def _setup_test_data(self):
super(TestCase, self)._setup_test_data()
test_utils.load_test_data(self)
self.context = {'authorized_tenants': self.tenants.list()}
def _setup_factory(self):
# For some magical reason we need a copy of this here.
self.factory = RequestFactoryWithMessages()
def _setup_user(self):
self._real_get_user = utils.get_user self._real_get_user = utils.get_user
tenants = self.context['authorized_tenants'] tenants = self.context['authorized_tenants']
self.setActiveUser(id=self.user.id, self.setActiveUser(id=self.user.id,
@ -176,14 +183,10 @@ class TestCase(horizon_helpers.TestCase):
tenant_id=self.tenant.id, tenant_id=self.tenant.id,
service_catalog=self.service_catalog, service_catalog=self.service_catalog,
authorized_tenants=tenants) authorized_tenants=tenants)
self.request = http.HttpRequest()
self.request.session = self.client._session() def _setup_request(self):
super(TestCase, self)._setup_request()
self.request.session['token'] = self.token.id self.request.session['token'] = self.token.id
middleware.HorizonMiddleware().process_request(self.request)
AuthenticationMiddleware().process_request(self.request)
self.patchers = {}
self.add_panel_mocks()
os.environ["HORIZON_TEST_RUN"] = "True"
def add_panel_mocks(self): def add_panel_mocks(self):
"""Global mocks on panels that get called on all views.""" """Global mocks on panels that get called on all views."""
@ -194,13 +197,11 @@ class TestCase(horizon_helpers.TestCase):
self.patchers['aggregates'].start() self.patchers['aggregates'].start()
def tearDown(self): def tearDown(self):
self.mox.UnsetStubs()
httplib2.Http._conn_request = self._real_conn_request httplib2.Http._conn_request = self._real_conn_request
context_processors.openstack = self._real_context_processor context_processors.openstack = self._real_context_processor
utils.get_user = self._real_get_user utils.get_user = self._real_get_user
mock.patch.stopall() mock.patch.stopall()
self.mox.VerifyAll() super(TestCase, self).tearDown()
del os.environ["HORIZON_TEST_RUN"]
def setActiveUser(self, id=None, token=None, username=None, tenant_id=None, def setActiveUser(self, id=None, token=None, username=None, tenant_id=None,
service_catalog=None, tenant_name=None, roles=None, service_catalog=None, tenant_name=None, roles=None,