Add jsonschema validation for karbor verification API
Change-Id: I85d4c249b03e4d80b946f7b8c2a3af689c9577d3 Partial-Implements: bp karbor-json-schema-validation
This commit is contained in:
parent
95db269d14
commit
4ad7447e43
|
@ -0,0 +1,38 @@
|
|||
# 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.
|
||||
|
||||
"""
|
||||
Schema for Karbor V1 Verifications API.
|
||||
|
||||
"""
|
||||
|
||||
from karbor.api.validation import parameter_types
|
||||
|
||||
|
||||
create = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'type': 'object',
|
||||
'verification': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'provider_id': parameter_types.uuid,
|
||||
'checkpoint_id': parameter_types.uuid,
|
||||
'parameters': parameter_types.parameters,
|
||||
},
|
||||
'required': ['provider_id', 'checkpoint_id'],
|
||||
'additionalProperties': False,
|
||||
},
|
||||
},
|
||||
'required': ['verification'],
|
||||
'additionalProperties': False,
|
||||
}
|
|
@ -20,6 +20,8 @@ from webob import exc
|
|||
|
||||
from karbor.api import common
|
||||
from karbor.api.openstack import wsgi
|
||||
from karbor.api.schemas import verifications as verification_schema
|
||||
from karbor.api import validation
|
||||
from karbor.common import constants
|
||||
from karbor import exception
|
||||
from karbor.i18n import _
|
||||
|
@ -195,6 +197,7 @@ class VerificationsController(wsgi.Controller):
|
|||
LOG.info("Get all verifications completed successfully.")
|
||||
return verifications
|
||||
|
||||
@validation.schema(verification_schema.create)
|
||||
def create(self, req, body):
|
||||
"""Creates a new verification."""
|
||||
if not self.is_valid_body(body, 'verification'):
|
||||
|
|
|
@ -16,7 +16,6 @@ from oslo_config import cfg
|
|||
from webob import exc
|
||||
|
||||
from karbor.api.v1 import verifications
|
||||
from karbor.common import constants
|
||||
from karbor import context
|
||||
from karbor import exception
|
||||
from karbor.tests import base
|
||||
|
@ -46,7 +45,7 @@ class VerificationApiTest(base.TestCase):
|
|||
verification = self._verification_in_request_body()
|
||||
body = {"verification": verification}
|
||||
req = fakes.HTTPRequest.blank('/v1/verifications')
|
||||
self.controller.create(req, body)
|
||||
self.controller.create(req, body=body)
|
||||
self.assertTrue(mock_verification_create.called)
|
||||
self.assertTrue(mock_rpc_verification.called)
|
||||
|
||||
|
@ -54,25 +53,25 @@ class VerificationApiTest(base.TestCase):
|
|||
verification = self._verification_in_request_body()
|
||||
body = {"verificationxx": verification}
|
||||
req = fakes.HTTPRequest.blank('/v1/verifications')
|
||||
self.assertRaises(exc.HTTPUnprocessableEntity,
|
||||
self.assertRaises(exception.ValidationError,
|
||||
self.controller.create,
|
||||
req, body)
|
||||
req, body=body)
|
||||
|
||||
def test_verification_create_InvalidProviderId(self):
|
||||
verification = self._verification_in_request_body(
|
||||
provider_id="")
|
||||
body = {"verification": verification}
|
||||
req = fakes.HTTPRequest.blank('/v1/verifications')
|
||||
self.assertRaises(exception.InvalidInput, self.controller.create,
|
||||
req, body)
|
||||
self.assertRaises(exception.ValidationError, self.controller.create,
|
||||
req, body=body)
|
||||
|
||||
def test_verification_create_Invalidcheckpoint_id(self):
|
||||
verification = self._verification_in_request_body(
|
||||
checkpoint_id="")
|
||||
body = {"verification": verification}
|
||||
req = fakes.HTTPRequest.blank('/v1/verifications')
|
||||
self.assertRaises(exception.InvalidInput, self.controller.create,
|
||||
req, body)
|
||||
self.assertRaises(exception.ValidationError, self.controller.create,
|
||||
req, body=body)
|
||||
|
||||
@mock.patch(
|
||||
'karbor.api.v1.verifications.'
|
||||
|
@ -98,17 +97,13 @@ class VerificationApiTest(base.TestCase):
|
|||
req, "1")
|
||||
|
||||
def _verification_in_request_body(
|
||||
self, project_id=DEFAULT_PROJECT_ID,
|
||||
provider_id=DEFAULT_PROVIDER_ID,
|
||||
self, provider_id=DEFAULT_PROVIDER_ID,
|
||||
checkpoint_id=DEFAULT_CHECKPOINT_ID,
|
||||
parameters=DEFAULT_PARAMETERS,
|
||||
status=constants.RESOURCE_STATUS_STARTED):
|
||||
parameters=DEFAULT_PARAMETERS):
|
||||
verification_req = {
|
||||
'project_id': project_id,
|
||||
'provider_id': provider_id,
|
||||
'checkpoint_id': checkpoint_id,
|
||||
'parameters': parameters,
|
||||
'status': status,
|
||||
}
|
||||
|
||||
return verification_req
|
||||
|
|
Loading…
Reference in New Issue