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)
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._assert_notify_sent(policy_ref['id'], CREATED_OPERATION,
'policy')
@ -529,7 +529,7 @@ class NotificationsForEntities(BaseNotificationTest):
'policy', cadftaxonomy.SECURITY_POLICY)
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.update_policy(policy_ref['id'], policy_ref)
self._assert_notify_sent(policy_ref['id'], UPDATED_OPERATION,
@ -538,7 +538,7 @@ class NotificationsForEntities(BaseNotificationTest):
'policy', cadftaxonomy.SECURITY_POLICY)
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.delete_policy(policy_ref['id'])
self._assert_notify_sent(policy_ref['id'], DELETED_OPERATION,

View File

@ -16,6 +16,7 @@ from __future__ import absolute_import
import atexit
import datetime
import functools
import json
import logging
import os
import re
@ -374,16 +375,20 @@ def new_role_ref(**kwargs):
return ref
def new_policy_ref():
return {
def new_policy_ref(**kwargs):
ref = {
'id': uuid.uuid4().hex,
'name': uuid.uuid4().hex,
'description': uuid.uuid4().hex,
'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,
}
ref.update(kwargs)
return ref
def new_trust_ref(trustor_user_id, trustee_user_id, project_id=None,
impersonation=None, expires=None, role_ids=None,

View File

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

View File

@ -67,8 +67,7 @@ class PolicyAssociationTests(object):
parent_region_id = None
for i in range(3):
policy = {'id': uuid.uuid4().hex, 'type': uuid.uuid4().hex,
'blob': {'data': uuid.uuid4().hex}}
policy = unit.new_policy_ref()
self.policy.append(self.policy_api.create_policy(policy['id'],
policy))
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,
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,
enable_project=True):
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):
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.service = unit.new_service_ref()
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
# under the License.
import uuid
from oslo_config import cfg
from oslo_serialization import jsonutils
from six.moves import range
@ -342,8 +340,7 @@ class IdentityTestListLimitCase(IdentityTestFilteredCase):
self.policy_list = []
self.addCleanup(self.clean_up_policy)
for _ in range(10):
new_entity = {'id': uuid.uuid4().hex, 'type': uuid.uuid4().hex,
'blob': uuid.uuid4().hex}
new_entity = unit.new_policy_ref()
policy = self.policy_api.create_policy(new_entity['id'],
new_entity)
self.policy_list.append(policy)

View File

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