diff --git a/horizon/test/settings.py b/horizon/test/settings.py index f3e432638e..3945321bb2 100644 --- a/horizon/test/settings.py +++ b/horizon/test/settings.py @@ -60,6 +60,7 @@ INSTALLED_APPS = ( ) MIDDLEWARE = ( + 'openstack_auth.middleware.OpenstackAuthMonkeyPatchMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/openstack_auth/middleware.py b/openstack_auth/middleware.py new file mode 100644 index 0000000000..b67a54e5e2 --- /dev/null +++ b/openstack_auth/middleware.py @@ -0,0 +1,25 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from openstack_auth import utils + +# NOTE: The main role of this middleware is to call this. +utils.patch_middleware_get_user() + + +class OpenstackAuthMonkeyPatchMiddleware(object): + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + # Do nothing actually + return self.get_response(request) diff --git a/openstack_auth/tests/settings.py b/openstack_auth/tests/settings.py index 41c9041944..22e272e274 100644 --- a/openstack_auth/tests/settings.py +++ b/openstack_auth/tests/settings.py @@ -28,6 +28,7 @@ INSTALLED_APPS = [ ] MIDDLEWARE = [ + 'openstack_auth.middleware.OpenstackAuthMonkeyPatchMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/openstack_auth/tests/urls.py b/openstack_auth/tests/urls.py index b93725a2a2..0a32b08418 100644 --- a/openstack_auth/tests/urls.py +++ b/openstack_auth/tests/urls.py @@ -15,13 +15,9 @@ from django.conf.urls import include from django.conf.urls import url from django.views import generic -from openstack_auth import utils from openstack_auth import views -utils.patch_middleware_get_user() - - urlpatterns = [ url(r"", include('openstack_auth.urls')), url(r"^websso/$", views.websso, name='websso'), diff --git a/openstack_auth/urls.py b/openstack_auth/urls.py index 3673f7ecca..27efdac18d 100644 --- a/openstack_auth/urls.py +++ b/openstack_auth/urls.py @@ -17,8 +17,6 @@ from django.views import generic from openstack_auth import utils from openstack_auth import views -utils.patch_middleware_get_user() - urlpatterns = [ url(r"^login/$", views.login, name='login'), diff --git a/openstack_auth/utils.py b/openstack_auth/utils.py index 70ccfdd5ec..58ff5cc77b 100644 --- a/openstack_auth/utils.py +++ b/openstack_auth/utils.py @@ -37,8 +37,8 @@ We need the request object to get the user, so we'll slightly modify the existing django.contrib.auth.get_user method. To do so we update the auth middleware to point to our overridden method. -Calling the "patch_middleware_get_user" method somewhere like our urls.py -file takes care of hooking it in appropriately. +Calling "patch_middleware_get_user" is done in our custom middleware at +"openstack_auth.middleware" to monkeypatch the code in before it is needed. """ diff --git a/openstack_dashboard/settings.py b/openstack_dashboard/settings.py index 197be7c3c9..6f1cf68a9e 100644 --- a/openstack_dashboard/settings.py +++ b/openstack_dashboard/settings.py @@ -110,6 +110,7 @@ OPENSTACK_IMAGE_BACKEND = { } MIDDLEWARE = ( + 'openstack_auth.middleware.OpenstackAuthMonkeyPatchMiddleware', 'debreach.middleware.RandomCommentMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index 5a83e6616d..83ba3089ba 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -408,10 +408,11 @@ class BaseAdminViewTests(TestCase): self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key -class APITestCase(TestCase): - def setUp(self): - super(APITestCase, self).setUp() - utils.patch_middleware_get_user() +# NOTE(adriant): APITestCase was only needed for some openstack_auth +# monkeypatching. With the new monkeypatch middleware from openstack_auth this +# is not needed. +# TODO(adriant): Clean up APITestCase usage in horizon plugins. +APITestCase = TestCase # APIMockTestCase was introduced to support mox to mock migration smoothly