use NoDBTestCase for KeypairPolicyTest

mock and return a dummy object for keypair so
NoDBTestCase can be uesd as parent class

Change-Id: I598db39a9adc10411b82d2f9e1d97a10431a07af
This commit is contained in:
jichenjc
2015-10-06 04:20:36 +08:00
parent 0f44add33f
commit 5e7f584d79

View File

@@ -345,7 +345,7 @@ class KeypairsTestV21(test.TestCase):
self.assertEqual(202, res.status_code)
class KeypairPolicyTestV21(test.TestCase):
class KeypairPolicyTestV21(test.NoDBTestCase):
KeyPairController = keypairs_v21.KeypairController()
policy_path = 'os_compute_api:os-keypairs'
@@ -361,8 +361,6 @@ class KeypairPolicyTestV21(test.TestCase):
_db_key_pair_get)
self.stubs.Set(db, "key_pair_get_all_by_user",
db_key_pair_get_all_by_user)
self.stubs.Set(db, "key_pair_create",
db_key_pair_create)
self.stubs.Set(db, "key_pair_destroy",
db_key_pair_destroy)
@@ -407,13 +405,23 @@ class KeypairPolicyTestV21(test.TestCase):
self.KeyPairController.create,
self.req, body=body)
def test_keypair_create_pass_policy(self):
def _assert_keypair_create(self, mock_create, req):
mock_create.assert_called_with(req, 'fake_user', 'create_test', 'ssh')
@mock.patch.object(compute_api.KeypairAPI, 'create_key_pair')
def test_keypair_create_pass_policy(self, mock_create):
keypair_obj = objects.KeyPair(name='', public_key='',
fingerprint='', user_id='')
mock_create.return_value = (keypair_obj, 'dummy')
body = {'keypair': {'name': 'create_test'}}
rules = {self.policy_path + ':create':
common_policy.parse_rule('')}
policy.set_rules(rules)
res = self.KeyPairController.create(self.req, body=body)
self.assertIn('keypair', res)
req = self.req.environ['nova.context']
self._assert_keypair_create(mock_create, req)
def test_keypair_delete_fail_policy(self):
rules = {self.policy_path + ':delete':
@@ -600,3 +608,6 @@ class KeypairsTestV210(KeypairsTestV22):
class KeypairPolicyTestV2(KeypairPolicyTestV21):
KeyPairController = keypairs_v2.KeypairController()
policy_path = 'compute_extension:keypairs'
def _assert_keypair_create(self, mock_create, req):
mock_create.assert_called_with(req, 'fake_user', 'create_test')