| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import copy |
| 14 |
import uuid |
| 15 |
|
| 16 |
from keystoneauth1 import fixture |
| 17 |
|
| 18 |
from keystoneclient import access |
| 19 |
from keystoneclient.auth.identity import v3 |
| 20 |
from keystoneclient import session |
| 21 |
from keystoneclient.tests.unit import utils |
| 22 |
|
| 23 |
|
| 24 |
class TesterFederationPlugin(v3.FederatedBaseAuth): |
| 25 |
|
| 26 |
def get_unscoped_auth_ref(self, sess, **kwargs): |
| 27 |
|
| 28 |
resp = sess.post(self.federated_token_url, authenticated=False) |
| 29 |
return access.AccessInfo.factory(resp=resp, body=resp.json()) |
| 30 |
|
| 31 |
|
| 32 |
class V3FederatedPlugin(utils.TestCase): |
| 33 |
|
| 34 |
AUTH_URL = 'http://keystone/v3' |
| 35 |
|
| 36 |
def setUp(self): |
| 37 |
super(V3FederatedPlugin, self).setUp() |
| 38 |
|
| 39 |
self.deprecations.expect_deprecations() |
| 40 |
|
| 41 |
self.unscoped_token = fixture.V3Token() |
| 42 |
self.unscoped_token_id = uuid.uuid4().hex |
| 43 |
self.scoped_token = copy.deepcopy(self.unscoped_token) |
| 44 |
self.scoped_token.set_project_scope() |
| 45 |
self.scoped_token.methods.append('token') |
| 46 |
self.scoped_token_id = uuid.uuid4().hex |
| 47 |
|
| 48 |
s = self.scoped_token.add_service('compute', name='nova') |
| 49 |
s.add_standard_endpoints(public='http://nova/public', |
| 50 |
admin='http://nova/admin', |
| 51 |
internal='http://nova/internal') |
| 52 |
|
| 53 |
self.idp = uuid.uuid4().hex |
| 54 |
self.protocol = uuid.uuid4().hex |
| 55 |
|
| 56 |
self.token_url = ('%s/OS-FEDERATION/identity_providers/%s/protocols/%s' |
| 57 |
'/auth' % (self.AUTH_URL, self.idp, self.protocol)) |
| 58 |
|
| 59 |
headers = {'X-Subject-Token': self.unscoped_token_id} |
| 60 |
self.unscoped_mock = self.requests_mock.post(self.token_url, |
| 61 |
json=self.unscoped_token, |
| 62 |
headers=headers) |
| 63 |
|
| 64 |
headers = {'X-Subject-Token': self.scoped_token_id} |
| 65 |
auth_url = self.AUTH_URL + '/auth/tokens' |
| 66 |
self.scoped_mock = self.requests_mock.post(auth_url, |
| 67 |
json=self.scoped_token, |
| 68 |
headers=headers) |
| 69 |
|
| 70 |
def get_plugin(self, **kwargs): |
| 71 |
kwargs.setdefault('auth_url', self.AUTH_URL) |
| 72 |
kwargs.setdefault('protocol', self.protocol) |
| 73 |
kwargs.setdefault('identity_provider', self.idp) |
| 74 |
return TesterFederationPlugin(**kwargs) |
| 75 |
|
| 76 |
def test_federated_url(self): |
| 77 |
plugin = self.get_plugin() |
| 78 |
self.assertEqual(self.token_url, plugin.federated_token_url) |
| 79 |
|
| 80 |
def test_unscoped_behaviour(self): |
| 81 |
sess = session.Session(auth=self.get_plugin()) |
| 82 |
self.assertEqual(self.unscoped_token_id, sess.get_token()) |
| 83 |
|
| 84 |
self.assertTrue(self.unscoped_mock.called) |
| 85 |
self.assertFalse(self.scoped_mock.called) |
| 86 |
|
| 87 |
def test_scoped_behaviour(self): |
| 88 |
auth = self.get_plugin(project_id=self.scoped_token.project_id) |
| 89 |
sess = session.Session(auth=auth) |
| 90 |
self.assertEqual(self.scoped_token_id, sess.get_token()) |
| 91 |
|
| 92 |
self.assertTrue(self.unscoped_mock.called) |
| 93 |
self.assertTrue(self.scoped_mock.called) |
| 94 |
|
| 95 |
def test_options(self): |
| 96 |
opts = [o.name for o in v3.FederatedBaseAuth.get_options()] |
| 97 |
|
| 98 |
self.assertIn('protocol', opts) |
| 99 |
self.assertIn('identity-provider', opts) |