From 4046ef661666794d196ee6d93cf0c6595b6830ea Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sun, 13 Sep 2020 08:32:36 +0900 Subject: [PATCH] Recover scenario settings in openstack_auth test_auth The scenario configuration in openstack_auth test_auth was ignored somehow. Perhaps it happened when openstack_auth was merged into the horizon repo as test runners used in the horizon repo so far (django test runner, nose and pytest) do not support testscenarios. This commit tries to recover the original intention of the scenario. pytest supports several ways to parametrize tests [1] but there seems no way without changing each test functions. A quick port of "testscenarios" is explained [2], but it is just a way to generate tests based on scenarios and we still need to add scenario parameters to each test function, so we cannot refer scenario parameters in setUp(). As a result, I chose a way to inherit the original class and pass different attributes per scenario. This is not ideal and I hope pytest lovers can improve the situation. The test classes in test_auth are renamed to more meaningful ones. Direct overrides of settings in test_auth.py are improved too. [1] https://docs.pytest.org/en/stable/example/parametrize.html [2] https://docs.pytest.org/en/stable/example/parametrize.html#a-quick-port-of-testscenarios Change-Id: I1538ffbc853a2c9328c364f462a27be36c85cc2f --- openstack_auth/tests/unit/test_auth.py | 115 +++++++++++++++---------- 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/openstack_auth/tests/unit/test_auth.py b/openstack_auth/tests/unit/test_auth.py index f4e7117aa0..ac162a9fc3 100644 --- a/openstack_auth/tests/unit/test_auth.py +++ b/openstack_auth/tests/unit/test_auth.py @@ -45,15 +45,26 @@ class IsA(object): return isinstance(other, self.cls) -class OpenStackAuthTestsMixin(object): - """Common functions for version specific tests.""" +class SwitchProviderTests(test.TestCase): - scenarios = [ - ('pure', {'interface': None}), - ('public', {'interface': 'publicURL'}), - ('internal', {'interface': 'internalURL'}), - ('admin', {'interface': 'adminURL'}) - ] + interface = None + + def setUp(self): + super().setUp() + + params = { + 'OPENSTACK_API_VERSIONS': {'identity': 3}, + 'OPENSTACK_KEYSTONE_URL': "http://localhost/identity/v3", + } + if self.interface: + params['OPENSTACK_ENDPOINT_TYPE'] = self.interface + + override = self.settings(**params) + override.enable() + self.addCleanup(override.disable) + + self.data = data_v3.generate_test_data() + self.ks_client_module = client_v3 def get_form_data(self, user): return {'region': "default", @@ -61,22 +72,6 @@ class OpenStackAuthTestsMixin(object): 'password': user.password, 'username': user.name} - -class OpenStackAuthTestsV3Base(OpenStackAuthTestsMixin, test.TestCase): - - def setUp(self): - super().setUp() - - if getattr(self, 'interface', None): - override = self.settings(OPENSTACK_ENDPOINT_TYPE=self.interface) - override.enable() - self.addCleanup(override.disable) - - self.data = data_v3.generate_test_data() - self.ks_client_module = client_v3 - settings.OPENSTACK_API_VERSIONS['identity'] = 3 - settings.OPENSTACK_KEYSTONE_URL = "http://localhost/identity/v3" - @mock.patch.object(v3_auth, 'Keystone2Keystone') @mock.patch.object(client_v3, 'Client') @mock.patch.object(v3_auth, 'Token') @@ -709,7 +704,19 @@ class OpenStackAuthTestsV3Base(OpenStackAuthTestsMixin, test.TestCase): IsA(session.Session)) -class OpenStackAuthTestsWebSSO(OpenStackAuthTestsMixin, test.TestCase): +class SwitchProviderTestsPublicURL(SwitchProviderTests): + interface = 'publicURL' + + +class SwitchProviderTestsInternalURL(SwitchProviderTests): + interface = 'internalURL' + + +class SwitchProviderTestsAdminURL(SwitchProviderTests): + interface = 'adminURL' + + +class OpenStackAuthTestsWebSSO(test.TestCase): def setUp(self): super().setUp() @@ -986,7 +993,25 @@ class OpenStackAuthTestsWebSSO(OpenStackAuthTestsMixin, test.TestCase): status_code=302, target_status_code=301) -class OpenStackAuthTestsV3WithMock(test.TestCase): +class OpenStackAuthTests(test.TestCase): + + interface = None + + def setUp(self): + super().setUp() + + params = { + 'OPENSTACK_API_VERSIONS': {'identity': 3}, + 'OPENSTACK_KEYSTONE_URL': "http://localhost/identity/v3", + } + if self.interface: + params['OPENSTACK_ENDPOINT_TYPE'] = self.interface + + override = self.settings(**params) + override.enable() + self.addCleanup(override.disable) + + self.data = data_v3.generate_test_data() def get_form_data(self, user): return {'region': "default", @@ -994,18 +1019,6 @@ class OpenStackAuthTestsV3WithMock(test.TestCase): 'password': user.password, 'username': user.name} - def setUp(self): - super().setUp() - - if getattr(self, 'interface', None): - override = self.settings(OPENSTACK_ENDPOINT_TYPE=self.interface) - override.enable() - self.addCleanup(override.disable) - - self.data = data_v3.generate_test_data() - settings.OPENSTACK_API_VERSIONS['identity'] = 3 - settings.OPENSTACK_KEYSTONE_URL = "http://localhost:5000/v3" - @mock.patch('keystoneauth1.identity.v3.Token.get_access') @mock.patch('keystoneauth1.identity.v3.Password.get_access') @mock.patch('keystoneclient.v3.client.Client') @@ -1109,22 +1122,18 @@ class OpenStackAuthTestsV3WithMock(test.TestCase): self.assertContains(response, 'id="id_domain"') self.assertContains(response, 'name="domain"') + @override_settings( + OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT=True, + OPENSTACK_KEYSTONE_DOMAIN_DROPDOWN=True, + OPENSTACK_KEYSTONE_DOMAIN_CHOICES=(('Default', 'Default'),) + ) def test_login_form_multidomain_dropdown(self): - override = self.settings(OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT=True, - OPENSTACK_KEYSTONE_DOMAIN_DROPDOWN=True, - OPENSTACK_KEYSTONE_DOMAIN_CHOICES=( - ('Default', 'Default'),) - ) - override.enable() - self.addCleanup(override.disable) - url = reverse('login') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertContains(response, 'id="id_domain"') self.assertContains(response, 'name="domain"') self.assertContains(response, 'option value="Default"') - settings.OPENSTACK_KEYSTONE_DOMAIN_DROPDOWN = False @mock.patch.object(projects.ProjectManager, 'list') def test_tenant_sorting(self, mock_project_list): @@ -1372,3 +1381,15 @@ class OpenStackAuthTestsV3WithMock(test.TestCase): def test_switch_region_with_next(self, next=None): self.test_switch_region(next='/next_url') + + +class OpenStackAuthTestsPublicURL(OpenStackAuthTests): + interface = 'publicURL' + + +class OpenStackAuthTestsInternalURL(OpenStackAuthTests): + interface = 'internalURL' + + +class OpenStackAuthTestsAdminURL(OpenStackAuthTests): + interface = 'adminURL'