Merge "Starting to split out orders from test_resources.py"
This commit is contained in:
commit
a21db6d52a
144
barbican/tests/api/controllers/test_orders.py
Normal file
144
barbican/tests/api/controllers/test_orders.py
Normal file
@ -0,0 +1,144 @@
|
||||
# Copyright (c) 2015 Rackspace, Inc.
|
||||
#
|
||||
# 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 os
|
||||
import uuid
|
||||
|
||||
from barbican.model import models
|
||||
from barbican.model import repositories
|
||||
from barbican.tests import utils
|
||||
|
||||
order_repo = repositories.get_order_repository()
|
||||
|
||||
|
||||
class WhenCreatingOrdersUsingOrdersResource(utils.BarbicanAPIBaseTestCase):
|
||||
generic_key_meta = {
|
||||
'name': 'secretname',
|
||||
'algorithm': 'AES',
|
||||
'bit_length': 256,
|
||||
'mode': 'cbc',
|
||||
'payload_content_type': 'application/octet-stream'
|
||||
}
|
||||
|
||||
def test_can_create_a_new_order(self):
|
||||
resp, order_uuid = create_order(
|
||||
self.app,
|
||||
order_type='key',
|
||||
meta=self.generic_key_meta
|
||||
)
|
||||
self.assertEqual(resp.status_int, 202)
|
||||
|
||||
# Make sure we get a valid uuid for the order
|
||||
uuid.UUID(order_uuid)
|
||||
|
||||
order = order_repo.get(order_uuid, self.project_id)
|
||||
|
||||
self.assertIsInstance(order, models.Order)
|
||||
|
||||
def test_order_creation_should_allow_unknown_algorithm(self):
|
||||
meta = {
|
||||
'bit_length': 128,
|
||||
'algorithm': 'unknown'
|
||||
}
|
||||
resp, _ = create_order(
|
||||
self.app,
|
||||
order_type='key',
|
||||
meta=meta
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_int, 202)
|
||||
|
||||
def test_order_creation_should_fail_without_a_type(self):
|
||||
resp, _ = create_order(
|
||||
self.app,
|
||||
meta=self.generic_key_meta,
|
||||
expect_errors=True
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_order_creation_should_fail_without_metadata(self):
|
||||
resp, _ = create_order(
|
||||
self.app,
|
||||
order_type='key',
|
||||
expect_errors=True
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_order_create_should_fail_w_unsupported_payload_content_type(self):
|
||||
meta = {
|
||||
'bit_length': 128,
|
||||
'algorithm': 'aes',
|
||||
'payload_content_type': 'something_unsupported'
|
||||
}
|
||||
resp, _ = create_order(
|
||||
self.app,
|
||||
order_type='key',
|
||||
meta=meta,
|
||||
expect_errors=True
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_order_creation_should_fail_with_bogus_content(self):
|
||||
resp = self.app.post(
|
||||
'/orders/',
|
||||
'random_stuff',
|
||||
headers={'Content-Type': 'application/json'},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_order_creation_should_fail_with_empty_dict(self):
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
{},
|
||||
headers={'Content-Type': 'application/json'},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_order_creation_should_fail_without_content_type_header(self):
|
||||
resp = self.app.post(
|
||||
'/orders/',
|
||||
'doesnt matter. headers are validated first',
|
||||
expect_errors=True,
|
||||
)
|
||||
self.assertEqual(resp.status_int, 415)
|
||||
|
||||
|
||||
def create_order(app, order_type=None, meta=None, expect_errors=False):
|
||||
# TODO(jvrbanac): Once test resources is split out, refactor this
|
||||
# and similar functions into a generalized helper module and reduce
|
||||
# duplication.
|
||||
request = {
|
||||
'type': order_type,
|
||||
'meta': meta
|
||||
}
|
||||
cleaned_request = {key: val for key, val in request.items()
|
||||
if val is not None}
|
||||
|
||||
resp = app.post_json(
|
||||
'/orders/',
|
||||
cleaned_request,
|
||||
expect_errors=expect_errors
|
||||
)
|
||||
|
||||
created_uuid = None
|
||||
if resp.status_int == 202:
|
||||
order_ref = resp.json.get('order_ref', '')
|
||||
_, created_uuid = os.path.split(order_ref)
|
||||
|
||||
return (resp, created_uuid)
|
@ -982,161 +982,6 @@ class WhenPerformingUnallowedOperationsOnSecrets(BaseSecretsResource):
|
||||
self.assertEqual(resp.status_int, 405)
|
||||
|
||||
|
||||
class WhenCreatingOrdersUsingOrdersResource(FunctionalTest):
|
||||
|
||||
def setUp(self):
|
||||
super(
|
||||
WhenCreatingOrdersUsingOrdersResource, self
|
||||
).setUp()
|
||||
self.app = webtest.TestApp(app.build_wsgi_app(self.root))
|
||||
self.app.extra_environ = get_barbican_env(self.external_project_id)
|
||||
|
||||
database_utils.setup_in_memory_db()
|
||||
self.addCleanup(database_utils.in_memory_cleanup)
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
self._init()
|
||||
|
||||
class RootController(object):
|
||||
orders = controllers.orders.OrdersController(self.queue_resource)
|
||||
|
||||
return RootController()
|
||||
|
||||
def _init(self):
|
||||
self.secret_name = 'name'
|
||||
self.secret_payload_content_type = 'application/octet-stream'
|
||||
self.secret_algorithm = "aes"
|
||||
self.secret_bit_length = 128
|
||||
self.secret_mode = "cbc"
|
||||
|
||||
self.project_internal_id = 'projectid1234'
|
||||
self.external_project_id = 'keystoneid1234'
|
||||
|
||||
self.project = models.Project()
|
||||
self.project.id = self.project_internal_id
|
||||
self.project.external_id = self.external_project_id
|
||||
|
||||
self.project_repo = mock.MagicMock()
|
||||
self.project_repo.get.return_value = self.project
|
||||
|
||||
self.order_repo = mock.MagicMock()
|
||||
self.order_repo.create_from.return_value = None
|
||||
|
||||
self.setup_order_repository_mock(self.order_repo)
|
||||
self.setup_project_repository_mock(self.project_repo)
|
||||
|
||||
self.queue_resource = mock.MagicMock()
|
||||
self.queue_resource.process_order.return_value = None
|
||||
|
||||
self.type = 'key'
|
||||
self.meta = {"name": "secretname",
|
||||
"algorithm": "AES",
|
||||
"bit_length": 256,
|
||||
"mode": "cbc",
|
||||
'payload_content_type': 'application/octet-stream'}
|
||||
|
||||
self.key_order_req = {'type': self.type,
|
||||
'meta': self.meta}
|
||||
|
||||
def test_should_add_new_order(self):
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
self.key_order_req
|
||||
)
|
||||
self.assertEqual(resp.status_int, 202)
|
||||
|
||||
self.queue_resource.process_type_order.assert_called_once_with(
|
||||
order_id=None, project_id=self.external_project_id)
|
||||
|
||||
args, kwargs = self.order_repo.create_from.call_args
|
||||
order = args[0]
|
||||
self.assertIsInstance(order, models.Order)
|
||||
|
||||
def test_should_fail_creating_order_with_bogus_content(self):
|
||||
resp = self.app.post(
|
||||
'/orders/',
|
||||
'bogus',
|
||||
headers={
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_should_allow_add_new_order_unsupported_algorithm(self):
|
||||
# TODO(john-wood-w) Allow until plugin validation is added.
|
||||
|
||||
# Using unsupported algorithm field for this test
|
||||
self.unsupported_req = {
|
||||
'type': 'key',
|
||||
'meta': {
|
||||
'name': self.secret_name,
|
||||
'payload_content_type': self.secret_payload_content_type,
|
||||
'algorithm': "not supported",
|
||||
'bit_length': self.secret_bit_length,
|
||||
'mode': self.secret_mode
|
||||
}
|
||||
}
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
self.unsupported_req,
|
||||
expect_errors=True
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_int, 202)
|
||||
|
||||
def test_should_raise_add_new_order_no_secret_info(self):
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
{},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_should_raise_add_new_order_no_type(self):
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
{'meta': self.meta},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_should_raise_add_new_order_no_meta(self):
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
{'type': self.type},
|
||||
expect_errors=True
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_should_raise_add_new_order_bad_json(self):
|
||||
resp = self.app.post(
|
||||
'/orders/',
|
||||
'',
|
||||
expect_errors=True,
|
||||
headers={'Content-Type': 'application/json'},
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
def test_should_raise_add_new_order_no_content_type_header(self):
|
||||
resp = self.app.post(
|
||||
'/orders/',
|
||||
self.key_order_req,
|
||||
expect_errors=True,
|
||||
)
|
||||
self.assertEqual(resp.status_int, 415)
|
||||
|
||||
def test_should_raise_add_new_order_with_unsupported_content_type(self):
|
||||
self.meta["payload_content_type"] = 'unsupported type'
|
||||
resp = self.app.post_json(
|
||||
'/orders/',
|
||||
self.key_order_req,
|
||||
expect_errors=True,
|
||||
)
|
||||
self.assertEqual(resp.status_int, 400)
|
||||
|
||||
|
||||
class WhenGettingOrdersListUsingOrdersResource(FunctionalTest):
|
||||
def setUp(self):
|
||||
super(
|
||||
|
Loading…
x
Reference in New Issue
Block a user