Merge "Refactor extract function load_auth_method"
This commit is contained in:
commit
4af488d88d
|
@ -41,6 +41,11 @@ AUTH_METHODS = {}
|
|||
AUTH_PLUGINS_LOADED = False
|
||||
|
||||
|
||||
def load_auth_method(method):
|
||||
plugin_name = CONF.auth[method]
|
||||
return importutils.import_object(plugin_name)
|
||||
|
||||
|
||||
def load_auth_methods():
|
||||
global AUTH_PLUGINS_LOADED
|
||||
|
||||
|
@ -51,9 +56,7 @@ def load_auth_methods():
|
|||
# have setup all the appropriate configuration options we may need.
|
||||
config.setup_authentication()
|
||||
for plugin in set(CONF.auth.methods):
|
||||
plugin_class = CONF.auth[plugin]
|
||||
driver = importutils.import_object(plugin_class)
|
||||
AUTH_METHODS[plugin] = driver
|
||||
AUTH_METHODS[plugin] = load_auth_method(plugin)
|
||||
AUTH_PLUGINS_LOADED = True
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
# Copyright 2015 IBM Corp.
|
||||
|
||||
# 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.
|
||||
|
||||
import uuid
|
||||
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from oslo_config import fixture as config_fixture
|
||||
from oslo_utils import importutils
|
||||
from oslotest import mockpatch
|
||||
|
||||
from keystone.auth import controllers
|
||||
from keystone.tests import unit
|
||||
|
||||
|
||||
class TestLoadAuthMethod(unit.BaseTestCase):
|
||||
def test_import_works(self):
|
||||
method = uuid.uuid4().hex
|
||||
plugin_name = self.getUniqueString()
|
||||
|
||||
# Register the method using the given plugin
|
||||
cf = self.useFixture(config_fixture.Config())
|
||||
cf.register_opt(cfg.StrOpt(method), group='auth')
|
||||
cf.config(group='auth', **{method: plugin_name})
|
||||
|
||||
self.useFixture(mockpatch.PatchObject(
|
||||
importutils, 'import_object', return_value=mock.sentinel.driver))
|
||||
|
||||
driver = controllers.load_auth_method(method)
|
||||
self.assertIs(driver, mock.sentinel.driver)
|
||||
|
||||
def test_import_fails(self):
|
||||
method = uuid.uuid4().hex
|
||||
plugin_name = self.getUniqueString()
|
||||
|
||||
# Register the method using the given plugin
|
||||
cf = self.useFixture(config_fixture.Config())
|
||||
cf.register_opt(cfg.StrOpt(method), group='auth')
|
||||
cf.config(group='auth', **{method: plugin_name})
|
||||
|
||||
class TestException(Exception):
|
||||
pass
|
||||
|
||||
self.useFixture(mockpatch.PatchObject(
|
||||
importutils, 'import_object', side_effect=TestException))
|
||||
|
||||
self.assertRaises(TestException, controllers.load_auth_method, method)
|
Loading…
Reference in New Issue