diff options
| author | Ziad Sawalha <github@highbridgellc.com> | 2012-01-24 20:52:46 -0600 |
|---|---|---|
| committer | Ziad Sawalha <github@highbridgellc.com> | 2012-01-24 20:53:45 -0600 |
| commit | b207a49cf9cf8f091fdadc0da06970e016101dc3 (patch) | |
| tree | 3b2064e1ddfc8f29d829b783aa168ea91d6627ab | |
| parent | 23c396df0b42bda0fb373fb4b93d0b66e3069e87 (diff) | |
Return Version and Tenant in Endpoints
- fixes bug 920817
- adds basic tests for this case
Change-Id: If32884ba149b089f93dc4278271d85c84f0f738c
| -rwxr-xr-x | keystone/logic/types/auth.py | 18 | ||||
| -rw-r--r-- | keystone/models.py | 10 | ||||
| -rw-r--r-- | keystone/test/unit/test_logic_auth.py | 132 |
3 files changed, 153 insertions, 7 deletions
diff --git a/keystone/logic/types/auth.py b/keystone/logic/types/auth.py index b59d2d7e..ace6aa6b 100755 --- a/keystone/logic/types/auth.py +++ b/keystone/logic/types/auth.py @@ -479,14 +479,14 @@ class AuthData(object): dom.append(token) user = etree.Element("user", - id=unicode(self.user.id), - name=unicode(self.user.username)) + id=unicode(self.user.id), + name=unicode(self.user.username)) dom.append(user) if self.user.rolegrants is not None: user.append(self.user.rolegrants.to_dom()) - if self.base_urls is not None or len(self.base_urls) > 0: + if self.base_urls is not None and len(self.base_urls) > 0: service_catalog = etree.Element("serviceCatalog") for key, key_base_urls in self.d.items(): dservice = db_api.SERVICE.get(key) @@ -510,12 +510,18 @@ class AuthData(object): endpoint.set(url_kind + "URL", base_url_item.replace('%tenant_id%', str(self.token.tenant.id))) + endpoint.set('tenantId', + str(self.token.tenant.id)) include_this_endpoint = True else: endpoint.set(url_kind + "URL", base_url_item) include_this_endpoint = True if include_this_endpoint: endpoint.set("id", str(base_url.id)) + if hasattr(base_url, "version_id"): + if base_url.version_id: + endpoint.set("versionId", + str(base_url.version_id)) service.append(endpoint) if service.find("endpoint") is not None: service_catalog.append(service) @@ -567,12 +573,18 @@ class AuthData(object): endpoint[url_kind + "URL"] = \ base_url_item.replace('%tenant_id%', str(self.token.tenant.id)) + endpoint['tenantId'] = \ + str(self.token.tenant.id) include_this_endpoint = True else: endpoint[url_kind + "URL"] = base_url_item include_this_endpoint = True if include_this_endpoint: endpoint['id'] = str(base_url.id) + if hasattr(base_url, 'version_id'): + if base_url.version_id: + endpoint['versionId'] = \ + str(base_url.version_id) endpoints.append(endpoint) dservice = db_api.SERVICE.get(key) if not dservice: diff --git a/keystone/models.py b/keystone/models.py index eb30d082..0f8006ce 100644 --- a/keystone/models.py +++ b/keystone/models.py @@ -737,11 +737,13 @@ class Roles(object): dom = etree.Element("roles") dom.set(u"xmlns", "http://docs.openstack.org/identity/api/v2.0") - for t in self.values: - dom.append(t.to_dom()) + if self.values: + for t in self.values: + dom.append(t.to_dom()) - for t in self.links: - dom.append(t.to_dom()) + if self.links: + for t in self.links: + dom.append(t.to_dom()) return dom diff --git a/keystone/test/unit/test_logic_auth.py b/keystone/test/unit/test_logic_auth.py new file mode 100644 index 00000000..3b61debf --- /dev/null +++ b/keystone/test/unit/test_logic_auth.py @@ -0,0 +1,132 @@ +# Copyright (c) 2011 OpenStack, LLC. +# +# 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 json +import datetime +from lxml import etree +import unittest2 as unittest + +import base +from keystone.logic.types import auth as logic_auth +from keystone import models +from keystone.test import utils as test_utils + + +class LogicTypesAuthTestCase(base.ServiceAPITest): + """ + Base class to test keystone/logic/types/auth.py + """ + def __init__(self, *args, **kwargs): + super(LogicTypesAuthTestCase, self).__init__(*args, **kwargs) + + self.user = models.User(id='u1', name='john', username='john') + self.role = models.Role(id=1, name='Admin') + self.user.rolegrants = models.Roles([self.role], links=None) + self.token = models.Token(id='abc123T', user_id=self.user.id, + expires=datetime.date(2000, 1, 31)) + self.tenant = models.Tenant(id='ten8', name='The Tenant') + self.token.tenant = self.tenant + self.base_urls = [models.EndpointTemplate( + id="1", + internal_url="http://127.0.0.1/v1/%tenant_id%", + public_url="http://internet.com/v1/%tenant_id%", + admin_url="http://private.net/v1/", + version_id="v1", + version_url="http://127.0.0.1/v1/", + version_info="http://127.0.0.1/", + region="RegionOne", + service_id="0" + ), + models.EndpointTemplate( + id="2", + internal_url="http://127.0.0.1/v1/%tenant_id%", + public_url="http://internet.com/v1/%tenant_id%", + service_id="0" + )] + self.url_types = ["internal", "public", "admin"] + + def test_AuthData_json_serialization(self): + auth = logic_auth.AuthData(self.token, self.user) + data = json.loads(auth.to_json()) + expected = { + 'access': { + 'token': { + 'expires': '2000-01-31', + 'tenants': [{ + 'id': 'ten8', + 'name': 'The Tenant' + }], + 'id': 'abc123T', + 'tenant': { + 'id': 'ten8', + 'name': 'The Tenant' + } + }, + 'user': { + 'id': 'u1', + 'roles': [{ + 'name': 'Admin', + 'id': '1' + }], + 'name': 'john' + } + } + } + self.assertDictEqual(data, expected) + + def test_AuthData_xml_serialization(self): + auth = logic_auth.AuthData(self.token, self.user) + xml_str = auth.to_xml() + expected = ('<access xmlns=' + '"http://docs.openstack.org/identity/api/v2.0"><token expires=' + '"2000-01-31" id="abc123T"><tenant name="The Tenant" ' + 'id="ten8"/></token><user name="john" id="u1"><roles ' + 'xmlns="http://docs.openstack.org/identity/api/v2.0"><role ' + 'xmlns="http://docs.openstack.org/identity/api/v2.0" id="1" ' + 'name="Admin"/></roles></user></access>') + self.assertTrue(test_utils.XMLTools.xmlEqual(xml_str, expected)) + + def test_AuthData_json_catalog(self): + auth = logic_auth.AuthData(self.token, self.user, self.base_urls) + data = json.loads(auth.to_json()) + self.assertIn("access", data) + self.assertIn("serviceCatalog", data['access']) + catalog = data['access']['serviceCatalog'] + self.assertTrue(len(catalog) > 0) + endpoints = catalog[0]['endpoints'] + self.assertTrue(len(endpoints) > 1) + endpoint = endpoints[0] + self.assertIn("publicURL", endpoint) + self.assertIn("versionId", endpoint) + self.assertIn("tenantId", endpoint) + + endpoint = endpoints[1] + self.assertNotIn("versionId", endpoint) + + def test_AuthData_xml_catalog(self): + auth = logic_auth.AuthData(self.token, self.user, self.base_urls) + xml_str = auth.to_xml() + dom = etree.fromstring(xml_str) + xmlns = "http://docs.openstack.org/identity/api/v2.0" + catalog = dom.find("{%s}serviceCatalog" % xmlns) + service = catalog.find("{%s}service" % xmlns) + endpoint = service.find("{%s}endpoint" % xmlns) + self.assertIsNotNone("publicURL", endpoint.attrib) + self.assertIn("versionId", endpoint.attrib) + self.assertIn("tenantId", endpoint.attrib) + + +if __name__ == '__main__': + unittest.main() |
