| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import functools |
| 14 |
from unittest import mock |
| 15 |
import uuid |
| 16 |
|
| 17 |
from keystoneauth1 import fixture |
| 18 |
from oslo_config import cfg |
| 19 |
|
| 20 |
from keystoneclient import access |
| 21 |
from keystoneclient.auth import base |
| 22 |
from keystoneclient import exceptions |
| 23 |
from keystoneclient import session |
| 24 |
from keystoneclient.tests.unit import utils |
| 25 |
|
| 26 |
|
| 27 |
class MockPlugin(base.BaseAuthPlugin): |
| 28 |
|
| 29 |
INT_DESC = 'test int' |
| 30 |
FLOAT_DESC = 'test float' |
| 31 |
BOOL_DESC = 'test bool' |
| 32 |
STR_DESC = 'test str' |
| 33 |
STR_DEFAULT = uuid.uuid4().hex |
| 34 |
|
| 35 |
def __init__(self, **kwargs): |
| 36 |
self._data = kwargs |
| 37 |
|
| 38 |
def __getitem__(self, key): |
| 39 |
"""Get the data of the key.""" |
| 40 |
return self._data[key] |
| 41 |
|
| 42 |
def get_token(self, *args, **kwargs): |
| 43 |
return 'aToken' |
| 44 |
|
| 45 |
def get_endpoint(self, *args, **kwargs): |
| 46 |
return 'http://test' |
| 47 |
|
| 48 |
@classmethod |
| 49 |
def get_options(cls): |
| 50 |
return [ |
| 51 |
cfg.IntOpt('a-int', default='3', help=cls.INT_DESC), |
| 52 |
cfg.BoolOpt('a-bool', help=cls.BOOL_DESC), |
| 53 |
cfg.FloatOpt('a-float', help=cls.FLOAT_DESC), |
| 54 |
cfg.StrOpt('a-str', help=cls.STR_DESC, default=cls.STR_DEFAULT), |
| 55 |
] |
| 56 |
|
| 57 |
|
| 58 |
class MockManager(object): |
| 59 |
|
| 60 |
def __init__(self, driver): |
| 61 |
self.driver = driver |
| 62 |
|
| 63 |
|
| 64 |
def mock_plugin(f): |
| 65 |
@functools.wraps(f) |
| 66 |
def inner(*args, **kwargs): |
| 67 |
with mock.patch.object(base, 'get_plugin_class') as m: |
| 68 |
m.return_value = MockPlugin |
| 69 |
args = list(args) + [m] |
| 70 |
return f(*args, **kwargs) |
| 71 |
|
| 72 |
return inner |
| 73 |
|
| 74 |
|
| 75 |
class TestCase(utils.TestCase): |
| 76 |
|
| 77 |
GROUP = 'auth' |
| 78 |
V2PASS = 'v2password' |
| 79 |
V3TOKEN = 'v3token' |
| 80 |
|
| 81 |
a_int = 88 |
| 82 |
a_float = 88.8 |
| 83 |
a_bool = False |
| 84 |
|
| 85 |
TEST_VALS = {'a_int': a_int, |
| 86 |
'a_float': a_float, |
| 87 |
'a_bool': a_bool} |
| 88 |
|
| 89 |
def assertTestVals(self, plugin, vals=TEST_VALS): |
| 90 |
for k, v in vals.items(): |
| 91 |
self.assertEqual(v, plugin[k]) |
| 92 |
|
| 93 |
|
| 94 |
class GenericPluginTestCase(utils.TestCase): |
| 95 |
|
| 96 |
TEST_URL = 'http://keystone.host:5000/' |
| 97 |
|
| 98 |
|
| 99 |
PLUGIN_CLASS = None |
| 100 |
V2_PLUGIN_CLASS = None |
| 101 |
V3_PLUGIN_CLASS = None |
| 102 |
|
| 103 |
def setUp(self): |
| 104 |
super(GenericPluginTestCase, self).setUp() |
| 105 |
|
| 106 |
self.token_v2 = fixture.V2Token() |
| 107 |
self.token_v3 = fixture.V3Token() |
| 108 |
self.token_v3_id = uuid.uuid4().hex |
| 109 |
|
| 110 |
self.deprecations.expect_deprecations() |
| 111 |
self.session = session.Session() |
| 112 |
|
| 113 |
self.stub_url('POST', ['v2.0', 'tokens'], json=self.token_v2) |
| 114 |
self.stub_url('POST', ['v3', 'auth', 'tokens'], |
| 115 |
headers={'X-Subject-Token': self.token_v3_id}, |
| 116 |
json=self.token_v3) |
| 117 |
|
| 118 |
def new_plugin(self, **kwargs): |
| 119 |
kwargs.setdefault('auth_url', self.TEST_URL) |
| 120 |
return self.PLUGIN_CLASS(**kwargs) |
| 121 |
|
| 122 |
def stub_discovery(self, base_url=None, **kwargs): |
| 123 |
kwargs.setdefault('href', self.TEST_URL) |
| 124 |
disc = fixture.DiscoveryList(**kwargs) |
| 125 |
self.stub_url('GET', json=disc, base_url=base_url, status_code=300) |
| 126 |
return disc |
| 127 |
|
| 128 |
def assertCreateV3(self, **kwargs): |
| 129 |
auth = self.new_plugin(**kwargs) |
| 130 |
auth_ref = auth.get_auth_ref(self.session) |
| 131 |
self.assertIsInstance(auth_ref, access.AccessInfoV3) |
| 132 |
self.assertEqual(self.TEST_URL + 'v3/auth/tokens', |
| 133 |
self.requests_mock.last_request.url) |
| 134 |
self.assertIsInstance(auth._plugin, self.V3_PLUGIN_CLASS) |
| 135 |
return auth |
| 136 |
|
| 137 |
def assertCreateV2(self, **kwargs): |
| 138 |
auth = self.new_plugin(**kwargs) |
| 139 |
auth_ref = auth.get_auth_ref(self.session) |
| 140 |
self.assertIsInstance(auth_ref, access.AccessInfoV2) |
| 141 |
self.assertEqual(self.TEST_URL + 'v2.0/tokens', |
| 142 |
self.requests_mock.last_request.url) |
| 143 |
self.assertIsInstance(auth._plugin, self.V2_PLUGIN_CLASS) |
| 144 |
return auth |
| 145 |
|
| 146 |
def assertDiscoveryFailure(self, **kwargs): |
| 147 |
plugin = self.new_plugin(**kwargs) |
| 148 |
self.assertRaises(exceptions.DiscoveryFailure, |
| 149 |
plugin.get_auth_ref, |
| 150 |
self.session) |
| 151 |
|
| 152 |
def test_create_v3_if_domain_params(self): |
| 153 |
self.stub_discovery() |
| 154 |
|
| 155 |
self.assertCreateV3(domain_id=uuid.uuid4().hex) |
| 156 |
self.assertCreateV3(domain_name=uuid.uuid4().hex) |
| 157 |
self.assertCreateV3(project_name=uuid.uuid4().hex, |
| 158 |
project_domain_name=uuid.uuid4().hex) |
| 159 |
self.assertCreateV3(project_name=uuid.uuid4().hex, |
| 160 |
project_domain_id=uuid.uuid4().hex) |
| 161 |
|
| 162 |
def test_create_v2_if_no_domain_params(self): |
| 163 |
self.stub_discovery() |
| 164 |
self.assertCreateV2() |
| 165 |
self.assertCreateV2(project_id=uuid.uuid4().hex) |
| 166 |
self.assertCreateV2(project_name=uuid.uuid4().hex) |
| 167 |
self.assertCreateV2(tenant_id=uuid.uuid4().hex) |
| 168 |
self.assertCreateV2(tenant_name=uuid.uuid4().hex) |
| 169 |
|
| 170 |
def test_v3_params_v2_url(self): |
| 171 |
self.stub_discovery(v3=False) |
| 172 |
self.assertDiscoveryFailure(domain_name=uuid.uuid4().hex) |
| 173 |
|
| 174 |
def test_v2_params_v3_url(self): |
| 175 |
self.stub_discovery(v2=False) |
| 176 |
self.assertCreateV3() |
| 177 |
|
| 178 |
def test_no_urls(self): |
| 179 |
self.stub_discovery(v2=False, v3=False) |
| 180 |
self.assertDiscoveryFailure() |
| 181 |
|
| 182 |
def test_path_based_url_v2(self): |
| 183 |
self.stub_url('GET', ['v2.0'], status_code=403) |
| 184 |
self.assertCreateV2(auth_url=self.TEST_URL + 'v2.0') |
| 185 |
|
| 186 |
def test_path_based_url_v3(self): |
| 187 |
self.stub_url('GET', ['v3'], status_code=403) |
| 188 |
self.assertCreateV3(auth_url=self.TEST_URL + 'v3') |
| 189 |
|
| 190 |
def test_disc_error_for_failure(self): |
| 191 |
self.stub_url('GET', [], status_code=403) |
| 192 |
self.assertDiscoveryFailure() |
| 193 |
|
| 194 |
def test_v3_plugin_from_failure(self): |
| 195 |
url = self.TEST_URL + 'v3' |
| 196 |
self.stub_url('GET', [], base_url=url, status_code=403) |
| 197 |
self.assertCreateV3(auth_url=url) |
| 198 |
|
| 199 |
def test_unknown_discovery_version(self): |
| 200 |
|
| 201 |
self.stub_discovery(v2=False, v3_id='v4.0') |
| 202 |
self.assertDiscoveryFailure() |