openapi-core/tests/integration/test_validators.py

490 lines
15 KiB
Python
Raw Normal View History

2019-02-26 16:49:25 +00:00
from base64 import b64encode
2017-11-02 16:05:25 +00:00
import json
import pytest
2019-03-07 21:55:27 +00:00
from six import text_type
2017-11-02 16:05:25 +00:00
2018-04-18 10:39:03 +00:00
from openapi_core.schema.media_types.exceptions import (
InvalidContentType, InvalidMediaTypeValue,
2017-11-02 16:05:25 +00:00
)
2018-08-21 17:33:24 +00:00
from openapi_core.extensions.models.models import BaseModel
2018-04-18 10:39:03 +00:00
from openapi_core.schema.operations.exceptions import InvalidOperation
from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
from openapi_core.schema.parameters.exceptions import InvalidParameterValue
2019-06-18 15:13:44 +00:00
from openapi_core.schema.paths.exceptions import InvalidPath
2018-04-18 10:39:03 +00:00
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
from openapi_core.schema.responses.exceptions import (
MissingResponseContent, InvalidResponse,
)
from openapi_core.schema.servers.exceptions import InvalidServer
2017-11-02 16:05:25 +00:00
from openapi_core.shortcuts import create_spec
2018-04-17 12:38:23 +00:00
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator
2018-04-17 14:37:52 +00:00
from openapi_core.wrappers.mock import MockRequest, MockResponse
2017-11-02 16:05:25 +00:00
class TestRequestValidator(object):
host_url = 'http://petstore.swagger.io'
2019-03-07 21:55:27 +00:00
api_key = '12345'
2019-02-26 16:49:25 +00:00
@property
def api_key_encoded(self):
2019-03-07 21:55:27 +00:00
api_key_bytes = self.api_key.encode('utf8')
api_key_bytes_enc = b64encode(api_key_bytes)
return text_type(api_key_bytes_enc, 'utf8')
2019-02-26 16:49:25 +00:00
2017-11-02 16:05:25 +00:00
@pytest.fixture
def spec_dict(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")
@pytest.fixture
def spec(self, spec_dict):
return create_spec(spec_dict)
@pytest.fixture
def validator(self, spec):
return RequestValidator(spec)
def test_request_server_error(self, validator):
2017-11-03 11:18:48 +00:00
request = MockRequest('http://petstore.invalid.net/v1', 'get', '/')
2017-11-02 16:05:25 +00:00
result = validator.validate(request)
assert len(result.errors) == 1
2017-11-03 11:18:48 +00:00
assert type(result.errors[0]) == InvalidServer
2017-11-02 16:05:25 +00:00
assert result.body is None
assert result.parameters == {}
2019-06-18 15:13:44 +00:00
def test_invalid_path(self, validator):
2017-11-03 11:18:48 +00:00
request = MockRequest(self.host_url, 'get', '/v1')
2017-11-02 16:05:25 +00:00
result = validator.validate(request)
2019-06-18 15:13:44 +00:00
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidPath
assert result.body is None
assert result.parameters == {}
def test_invalid_operation(self, validator):
request = MockRequest(self.host_url, 'patch', '/v1/pets')
result = validator.validate(request)
2017-11-02 16:05:25 +00:00
assert len(result.errors) == 1
2017-11-03 11:18:48 +00:00
assert type(result.errors[0]) == InvalidOperation
2017-11-02 16:05:25 +00:00
assert result.body is None
assert result.parameters == {}
def test_missing_parameter(self, validator):
2017-11-03 11:18:48 +00:00
request = MockRequest(self.host_url, 'get', '/v1/pets')
2017-11-02 16:05:25 +00:00
result = validator.validate(request)
2018-04-18 10:39:03 +00:00
assert type(result.errors[0]) == MissingRequiredParameter
2017-11-02 16:05:25 +00:00
assert result.body is None
assert result.parameters == {
'query': {
'page': 1,
'search': '',
},
}
def test_get_pets(self, validator):
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-11-02 16:05:25 +00:00
self.host_url, 'get', '/v1/pets',
path_pattern='/v1/pets', args={'limit': '10'},
)
result = validator.validate(request)
assert result.errors == []
assert result.body is None
assert result.parameters == {
'query': {
'limit': 10,
'page': 1,
'search': '',
},
}
def test_missing_body(self, validator):
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-11-02 16:05:25 +00:00
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets',
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-11-02 16:05:25 +00:00
)
result = validator.validate(request)
assert len(result.errors) == 1
2018-04-18 10:39:03 +00:00
assert type(result.errors[0]) == MissingRequestBody
2017-11-02 16:05:25 +00:00
assert result.body is None
2018-07-09 11:10:05 +00:00
assert result.parameters == {
'header': {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
'cookie': {
'user': 123,
},
}
2017-11-02 16:05:25 +00:00
def test_invalid_content_type(self, validator):
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-11-02 16:05:25 +00:00
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', mimetype='text/csv',
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-11-02 16:05:25 +00:00
)
result = validator.validate(request)
assert len(result.errors) == 1
2017-11-03 11:18:48 +00:00
assert type(result.errors[0]) == InvalidContentType
2017-11-02 16:05:25 +00:00
assert result.body is None
2018-07-09 11:10:05 +00:00
assert result.parameters == {
'header': {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
'cookie': {
'user': 123,
},
}
2017-11-02 16:05:25 +00:00
def test_post_pets(self, validator, spec_dict):
pet_name = 'Cat'
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
data_json = {
'name': pet_name,
'tag': pet_tag,
2019-03-22 01:51:47 +00:00
'position': 2,
2017-11-02 16:05:25 +00:00
'address': {
'street': pet_street,
'city': pet_city,
2018-05-25 15:32:09 +00:00
},
'ears': {
'healthy': True,
2017-11-02 16:05:25 +00:00
}
}
data = json.dumps(data_json)
2018-07-09 11:10:05 +00:00
headers = {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key_encoded,
2018-07-09 11:10:05 +00:00
}
cookies = {
'user': '123',
}
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-11-02 16:05:25 +00:00
self.host_url, 'post', '/v1/pets',
path_pattern='/v1/pets', data=data,
2018-07-09 11:10:05 +00:00
headers=headers, cookies=cookies,
2017-11-02 16:05:25 +00:00
)
result = validator.validate(request)
assert result.errors == []
2018-07-09 11:10:05 +00:00
assert result.parameters == {
'header': {
2019-02-26 16:49:25 +00:00
'api_key': self.api_key,
2018-07-09 11:10:05 +00:00
},
'cookie': {
'user': 123,
},
}
2017-11-02 16:05:25 +00:00
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
address_model = schemas['Address']['x-model']
assert result.body.__class__.__name__ == pet_model
assert result.body.name == pet_name
assert result.body.tag == pet_tag
assert result.body.position == 2
assert result.body.address.__class__.__name__ == address_model
assert result.body.address.street == pet_street
assert result.body.address.city == pet_city
def test_get_pet(self, validator):
2017-11-03 11:18:48 +00:00
request = MockRequest(
2017-11-02 16:05:25 +00:00
self.host_url, 'get', '/v1/pets/1',
path_pattern='/v1/pets/{petId}', view_args={'petId': '1'},
)
result = validator.validate(request)
assert result.errors == []
assert result.body is None
assert result.parameters == {
'path': {
'petId': 1,
},
}
2017-11-06 13:32:31 +00:00
class TestPathItemParamsValidator(object):
@pytest.fixture
def spec_dict(self):
return {
"openapi": "3.0.0",
"info": {
"title": "Test path item parameter validation",
"version": "0.1",
},
"paths": {
"/resource": {
"parameters": [
{
"name": "resId",
"in": "query",
"required": True,
"schema": {
"type": "integer",
},
},
],
"get": {
"responses": {
"default": {
"description": "Return the resource."
}
}
}
}
}
}
@pytest.fixture
def spec(self, spec_dict):
return create_spec(spec_dict)
@pytest.fixture
def validator(self, spec):
return RequestValidator(spec)
def test_request_missing_param(self, validator):
request = MockRequest('http://example.com', 'get', '/resource')
result = validator.validate(request)
assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None
assert result.parameters == {}
def test_request_invalid_param(self, validator):
request = MockRequest(
'http://example.com', 'get', '/resource',
args={'resId': 'invalid'},
)
result = validator.validate(request)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidParameterValue
assert result.body is None
assert result.parameters == {}
def test_request_valid_param(self, validator):
request = MockRequest(
'http://example.com', 'get', '/resource',
args={'resId': '10'},
)
result = validator.validate(request)
assert len(result.errors) == 0
assert result.body is None
assert result.parameters == {'query': {'resId': 10}}
def test_request_override_param(self, spec_dict):
# override path parameter on operation
spec_dict["paths"]["/resource"]["get"]["parameters"] = [
{
# full valid parameter object required
"name": "resId",
"in": "query",
"required": False,
"schema": {
"type": "integer",
},
}
]
validator = RequestValidator(create_spec(spec_dict))
request = MockRequest('http://example.com', 'get', '/resource')
result = validator.validate(request)
assert len(result.errors) == 0
assert result.body is None
assert result.parameters == {}
def test_request_override_param_uniqueness(self, spec_dict):
# add parameter on operation with same name as on path but
# different location
spec_dict["paths"]["/resource"]["get"]["parameters"] = [
{
# full valid parameter object required
"name": "resId",
"in": "header",
"required": False,
"schema": {
"type": "integer",
},
}
]
validator = RequestValidator(create_spec(spec_dict))
request = MockRequest('http://example.com', 'get', '/resource')
result = validator.validate(request)
assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None
assert result.parameters == {}
2017-11-06 13:32:31 +00:00
class TestResponseValidator(object):
host_url = 'http://petstore.swagger.io'
@pytest.fixture
def spec_dict(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")
@pytest.fixture
def spec(self, spec_dict):
return create_spec(spec_dict)
@pytest.fixture
def validator(self, spec):
return ResponseValidator(spec)
def test_invalid_server(self, validator):
request = MockRequest('http://petstore.invalid.net/v1', 'get', '/')
2017-11-06 15:08:21 +00:00
response = MockResponse('Not Found', status_code=404)
2017-11-06 13:32:31 +00:00
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidServer
2017-11-06 14:05:06 +00:00
assert result.data is None
2018-04-18 10:39:03 +00:00
assert result.headers is None
2017-11-06 13:32:31 +00:00
def test_invalid_operation(self, validator):
request = MockRequest(self.host_url, 'get', '/v1')
2017-11-06 15:08:21 +00:00
response = MockResponse('Not Found', status_code=404)
2017-11-06 13:32:31 +00:00
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidOperation
2017-11-06 14:05:06 +00:00
assert result.data is None
2018-04-18 10:39:03 +00:00
assert result.headers is None
2017-11-06 13:32:31 +00:00
def test_invalid_response(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
2017-11-06 15:08:21 +00:00
response = MockResponse('Not Found', status_code=409)
2017-11-06 13:32:31 +00:00
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidResponse
2017-11-06 14:05:06 +00:00
assert result.data is None
2018-04-18 10:39:03 +00:00
assert result.headers is None
2017-11-06 13:32:31 +00:00
2017-11-06 13:53:49 +00:00
def test_invalid_content_type(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response = MockResponse('Not Found', mimetype='text/csv')
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidContentType
2017-11-06 14:05:06 +00:00
assert result.data is None
2017-11-06 13:53:49 +00:00
assert result.headers == {}
2017-11-06 13:32:31 +00:00
def test_missing_body(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response = MockResponse(None)
result = validator.validate(request, response)
assert len(result.errors) == 1
2018-04-18 10:39:03 +00:00
assert type(result.errors[0]) == MissingResponseContent
2017-11-06 14:05:06 +00:00
assert result.data is None
2017-11-06 13:32:31 +00:00
assert result.headers == {}
2019-09-03 00:38:19 +00:00
def test_invalid_media_type(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response = MockResponse("abcde")
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidMediaTypeValue
assert result.data is None
assert result.headers == {}
2017-11-06 13:32:31 +00:00
def test_invalid_media_type_value(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
2019-02-24 02:28:45 +00:00
response = MockResponse("{}")
2017-11-06 13:32:31 +00:00
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidMediaTypeValue
2017-11-06 14:05:06 +00:00
assert result.data is None
2017-11-06 13:32:31 +00:00
assert result.headers == {}
2018-04-23 18:50:29 +00:00
def test_invalid_value(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/tags')
response_json = {
'data': [
{
'id': 1,
'name': 'Sparky'
},
],
}
response_data = json.dumps(response_json)
response = MockResponse(response_data)
result = validator.validate(request, response)
assert len(result.errors) == 1
2018-04-18 10:39:03 +00:00
assert type(result.errors[0]) == InvalidMediaTypeValue
2018-04-23 18:50:29 +00:00
assert result.data is None
assert result.headers == {}
2017-11-06 13:32:31 +00:00
def test_get_pets(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response_json = {
'data': [
{
'id': 1,
2019-09-03 00:38:19 +00:00
'name': 'Sparky',
'ears': {
'healthy': True,
},
2017-11-06 13:32:31 +00:00
},
],
}
response_data = json.dumps(response_json)
response = MockResponse(response_data)
result = validator.validate(request, response)
assert result.errors == []
2018-08-21 17:33:24 +00:00
assert isinstance(result.data, BaseModel)
assert len(result.data.data) == 1
assert result.data.data[0].id == 1
assert result.data.data[0].name == 'Sparky'
2017-11-06 13:32:31 +00:00
assert result.headers == {}