Use new_policy_ref consistently

Do not manually create policy refs. Use new_policy_ref instead.

Change-Id: I7b6ec22430fcffae69cab1b276e41390314be5cc
This commit is contained in:
Sean Perry 2015-11-18 17:36:39 -08:00
parent 3ac286897f
commit 73317d9b63
8 changed files with 34 additions and 52 deletions

View File

@ -521,7 +521,7 @@ class NotificationsForEntities(BaseNotificationTest):
'region', cadftaxonomy.SECURITY_REGION) 'region', cadftaxonomy.SECURITY_REGION)
def test_create_policy(self): def test_create_policy(self):
policy_ref = self.new_policy_ref() policy_ref = unit.new_policy_ref()
self.policy_api.create_policy(policy_ref['id'], policy_ref) self.policy_api.create_policy(policy_ref['id'], policy_ref)
self._assert_notify_sent(policy_ref['id'], CREATED_OPERATION, self._assert_notify_sent(policy_ref['id'], CREATED_OPERATION,
'policy') 'policy')
@ -529,7 +529,7 @@ class NotificationsForEntities(BaseNotificationTest):
'policy', cadftaxonomy.SECURITY_POLICY) 'policy', cadftaxonomy.SECURITY_POLICY)
def test_update_policy(self): def test_update_policy(self):
policy_ref = self.new_policy_ref() policy_ref = unit.new_policy_ref()
self.policy_api.create_policy(policy_ref['id'], policy_ref) self.policy_api.create_policy(policy_ref['id'], policy_ref)
self.policy_api.update_policy(policy_ref['id'], policy_ref) self.policy_api.update_policy(policy_ref['id'], policy_ref)
self._assert_notify_sent(policy_ref['id'], UPDATED_OPERATION, self._assert_notify_sent(policy_ref['id'], UPDATED_OPERATION,
@ -538,7 +538,7 @@ class NotificationsForEntities(BaseNotificationTest):
'policy', cadftaxonomy.SECURITY_POLICY) 'policy', cadftaxonomy.SECURITY_POLICY)
def test_delete_policy(self): def test_delete_policy(self):
policy_ref = self.new_policy_ref() policy_ref = unit.new_policy_ref()
self.policy_api.create_policy(policy_ref['id'], policy_ref) self.policy_api.create_policy(policy_ref['id'], policy_ref)
self.policy_api.delete_policy(policy_ref['id']) self.policy_api.delete_policy(policy_ref['id'])
self._assert_notify_sent(policy_ref['id'], DELETED_OPERATION, self._assert_notify_sent(policy_ref['id'], DELETED_OPERATION,

View File

@ -16,6 +16,7 @@ from __future__ import absolute_import
import atexit import atexit
import datetime import datetime
import functools import functools
import json
import logging import logging
import os import os
import re import re
@ -374,16 +375,20 @@ def new_role_ref(**kwargs):
return ref return ref
def new_policy_ref(): def new_policy_ref(**kwargs):
return { ref = {
'id': uuid.uuid4().hex, 'id': uuid.uuid4().hex,
'name': uuid.uuid4().hex, 'name': uuid.uuid4().hex,
'description': uuid.uuid4().hex, 'description': uuid.uuid4().hex,
'enabled': True, 'enabled': True,
'blob': uuid.uuid4().hex, # Store serialized JSON data as the blob to mimic real world usage.
'blob': json.dumps({'data': uuid.uuid4().hex, }),
'type': uuid.uuid4().hex, 'type': uuid.uuid4().hex,
} }
ref.update(kwargs)
return ref
def new_trust_ref(trustor_user_id, trustee_user_id, project_id=None, def new_trust_ref(trustor_user_id, trustee_user_id, project_id=None,
impersonation=None, expires=None, role_ids=None, impersonation=None, expires=None, role_ids=None,

View File

@ -5306,34 +5306,23 @@ class CatalogTests(object):
class PolicyTests(object): class PolicyTests(object):
def _new_policy_ref(self):
return {
'id': uuid.uuid4().hex,
'policy': uuid.uuid4().hex,
'type': uuid.uuid4().hex,
'endpoint_id': uuid.uuid4().hex,
}
def assertEqualPolicies(self, a, b): def assertEqualPolicies(self, a, b):
self.assertEqual(a['id'], b['id']) self.assertDictEqual(a, b)
self.assertEqual(a['endpoint_id'], b['endpoint_id'])
self.assertEqual(a['policy'], b['policy'])
self.assertEqual(a['type'], b['type'])
def test_create(self): def test_create(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
res = self.policy_api.create_policy(ref['id'], ref) res = self.policy_api.create_policy(ref['id'], ref)
self.assertEqualPolicies(ref, res) self.assertEqualPolicies(ref, res)
def test_get(self): def test_get(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
res = self.policy_api.create_policy(ref['id'], ref) res = self.policy_api.create_policy(ref['id'], ref)
res = self.policy_api.get_policy(ref['id']) res = self.policy_api.get_policy(ref['id'])
self.assertEqualPolicies(ref, res) self.assertEqualPolicies(ref, res)
def test_list(self): def test_list(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
self.policy_api.create_policy(ref['id'], ref) self.policy_api.create_policy(ref['id'], ref)
res = self.policy_api.list_policies() res = self.policy_api.list_policies()
@ -5341,11 +5330,11 @@ class PolicyTests(object):
self.assertEqualPolicies(ref, res) self.assertEqualPolicies(ref, res)
def test_update(self): def test_update(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
self.policy_api.create_policy(ref['id'], ref) self.policy_api.create_policy(ref['id'], ref)
orig = ref orig = ref
ref = self._new_policy_ref() ref = unit.new_policy_ref()
# (cannot change policy ID) # (cannot change policy ID)
self.assertRaises(exception.ValidationError, self.assertRaises(exception.ValidationError,
@ -5358,7 +5347,7 @@ class PolicyTests(object):
self.assertEqualPolicies(ref, res) self.assertEqualPolicies(ref, res)
def test_delete(self): def test_delete(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
self.policy_api.create_policy(ref['id'], ref) self.policy_api.create_policy(ref['id'], ref)
self.policy_api.delete_policy(ref['id']) self.policy_api.delete_policy(ref['id'])
@ -5377,7 +5366,7 @@ class PolicyTests(object):
uuid.uuid4().hex) uuid.uuid4().hex)
def test_update_policy_returns_not_found(self): def test_update_policy_returns_not_found(self):
ref = self._new_policy_ref() ref = unit.new_policy_ref()
self.assertRaises(exception.PolicyNotFound, self.assertRaises(exception.PolicyNotFound,
self.policy_api.update_policy, self.policy_api.update_policy,
ref['id'], ref['id'],

View File

@ -67,8 +67,7 @@ class PolicyAssociationTests(object):
parent_region_id = None parent_region_id = None
for i in range(3): for i in range(3):
policy = {'id': uuid.uuid4().hex, 'type': uuid.uuid4().hex, policy = unit.new_policy_ref()
'blob': {'data': uuid.uuid4().hex}}
self.policy.append(self.policy_api.create_policy(policy['id'], self.policy.append(self.policy_api.create_policy(policy['id'],
policy)) policy))
service = unit.new_service_ref() service = unit.new_service_ref()

View File

@ -250,9 +250,6 @@ class RestfulTestCase(unit.SQLDriverOverrides, rest.RestfulTestCase,
return unit.new_credential_ref(user_id, project_id=project_id, return unit.new_credential_ref(user_id, project_id=project_id,
cred_type=cred_type) cred_type=cred_type)
def new_policy_ref(self):
return unit.new_policy_ref()
def create_new_default_project_for_user(self, user_id, domain_id, def create_new_default_project_for_user(self, user_id, domain_id,
enable_project=True): enable_project=True):
ref = unit.new_project_ref(domain_id=domain_id, enabled=enable_project) ref = unit.new_project_ref(domain_id=domain_id, enabled=enable_project)

View File

@ -32,7 +32,7 @@ class EndpointPolicyTestCase(test_v3.RestfulTestCase):
def setUp(self): def setUp(self):
super(EndpointPolicyTestCase, self).setUp() super(EndpointPolicyTestCase, self).setUp()
self.policy = self.new_policy_ref() self.policy = unit.new_policy_ref()
self.policy_api.create_policy(self.policy['id'], self.policy) self.policy_api.create_policy(self.policy['id'], self.policy)
self.service = unit.new_service_ref() self.service = unit.new_service_ref()
self.catalog_api.create_service(self.service['id'], self.service) self.catalog_api.create_service(self.service['id'], self.service)

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import uuid
from oslo_config import cfg from oslo_config import cfg
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from six.moves import range from six.moves import range
@ -342,8 +340,7 @@ class IdentityTestListLimitCase(IdentityTestFilteredCase):
self.policy_list = [] self.policy_list = []
self.addCleanup(self.clean_up_policy) self.addCleanup(self.clean_up_policy)
for _ in range(10): for _ in range(10):
new_entity = {'id': uuid.uuid4().hex, 'type': uuid.uuid4().hex, new_entity = unit.new_policy_ref()
'blob': uuid.uuid4().hex}
policy = self.policy_api.create_policy(new_entity['id'], policy = self.policy_api.create_policy(new_entity['id'],
new_entity) new_entity)
self.policy_list.append(policy) self.policy_list.append(policy)

View File

@ -12,8 +12,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import uuid import uuid
from keystone.tests import unit
from keystone.tests.unit import test_v3 from keystone.tests.unit import test_v3
@ -22,9 +24,8 @@ class PolicyTestCase(test_v3.RestfulTestCase):
def setUp(self): def setUp(self):
super(PolicyTestCase, self).setUp() super(PolicyTestCase, self).setUp()
self.policy_id = uuid.uuid4().hex self.policy = unit.new_policy_ref()
self.policy = self.new_policy_ref() self.policy_id = self.policy['id']
self.policy['id'] = self.policy_id
self.policy_api.create_policy( self.policy_api.create_policy(
self.policy_id, self.policy_id,
self.policy.copy()) self.policy.copy())
@ -33,10 +34,8 @@ class PolicyTestCase(test_v3.RestfulTestCase):
def test_create_policy(self): def test_create_policy(self):
"""Call ``POST /policies``.""" """Call ``POST /policies``."""
ref = self.new_policy_ref() ref = unit.new_policy_ref()
r = self.post( r = self.post('/policies', body={'policy': ref})
'/policies',
body={'policy': ref})
return self.assertValidPolicyResponse(r, ref) return self.assertValidPolicyResponse(r, ref)
def test_list_policies(self): def test_list_policies(self):
@ -47,22 +46,18 @@ class PolicyTestCase(test_v3.RestfulTestCase):
def test_get_policy(self): def test_get_policy(self):
"""Call ``GET /policies/{policy_id}``.""" """Call ``GET /policies/{policy_id}``."""
r = self.get( r = self.get(
'/policies/%(policy_id)s' % { '/policies/%(policy_id)s' % {'policy_id': self.policy_id})
'policy_id': self.policy_id})
self.assertValidPolicyResponse(r, self.policy) self.assertValidPolicyResponse(r, self.policy)
def test_update_policy(self): def test_update_policy(self):
"""Call ``PATCH /policies/{policy_id}``.""" """Call ``PATCH /policies/{policy_id}``."""
policy = self.new_policy_ref() self.policy['blob'] = json.dumps({'data': uuid.uuid4().hex, })
policy['id'] = self.policy_id
r = self.patch( r = self.patch(
'/policies/%(policy_id)s' % { '/policies/%(policy_id)s' % {'policy_id': self.policy_id},
'policy_id': self.policy_id}, body={'policy': self.policy})
body={'policy': policy}) self.assertValidPolicyResponse(r, self.policy)
self.assertValidPolicyResponse(r, policy)
def test_delete_policy(self): def test_delete_policy(self):
"""Call ``DELETE /policies/{policy_id}``.""" """Call ``DELETE /policies/{policy_id}``."""
self.delete( self.delete(
'/policies/%(policy_id)s' % { '/policies/%(policy_id)s' % {'policy_id': self.policy_id})
'policy_id': self.policy_id})