|
| 1 | +import json |
| 2 | +import pytest |
| 3 | + |
| 4 | +from openapi_core.exceptions import ( |
| 5 | + InvalidServerError, InvalidOperationError, MissingParameterError, |
| 6 | + MissingBodyError, InvalidContentTypeError, |
| 7 | +) |
| 8 | +from openapi_core.shortcuts import create_spec |
| 9 | +from openapi_core.validators import RequestValidator |
| 10 | +from openapi_core.wrappers import BaseOpenAPIRequest |
| 11 | + |
| 12 | + |
| 13 | +class RequestMock(BaseOpenAPIRequest): |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, host_url, method, path, path_pattern=None, args=None, |
| 17 | + view_args=None, headers=None, cookies=None, data=None, |
| 18 | + mimetype='application/json'): |
| 19 | + self.host_url = host_url |
| 20 | + self.path = path |
| 21 | + self.path_pattern = path_pattern or path |
| 22 | + self.method = method |
| 23 | + |
| 24 | + self.args = args or {} |
| 25 | + self.view_args = view_args or {} |
| 26 | + self.headers = headers or {} |
| 27 | + self.cookies = cookies or {} |
| 28 | + self.data = data or '' |
| 29 | + |
| 30 | + self.mimetype = mimetype |
| 31 | + |
| 32 | + |
| 33 | +class TestRequestValidator(object): |
| 34 | + |
| 35 | + host_url = 'http://petstore.swagger.io' |
| 36 | + |
| 37 | + @pytest.fixture |
| 38 | + def spec_dict(self, factory): |
| 39 | + return factory.spec_from_file("data/v3.0/petstore.yaml") |
| 40 | + |
| 41 | + @pytest.fixture |
| 42 | + def spec(self, spec_dict): |
| 43 | + return create_spec(spec_dict) |
| 44 | + |
| 45 | + @pytest.fixture |
| 46 | + def validator(self, spec): |
| 47 | + return RequestValidator(spec) |
| 48 | + |
| 49 | + def test_request_server_error(self, validator): |
| 50 | + request = RequestMock('http://petstore.invalid.net/v1', 'get', '/') |
| 51 | + |
| 52 | + result = validator.validate(request) |
| 53 | + |
| 54 | + assert len(result.errors) == 1 |
| 55 | + assert type(result.errors[0]) == InvalidServerError |
| 56 | + assert result.body is None |
| 57 | + assert result.parameters == {} |
| 58 | + |
| 59 | + def test_invalid_operation(self, validator): |
| 60 | + request = RequestMock(self.host_url, 'get', '/v1') |
| 61 | + |
| 62 | + result = validator.validate(request) |
| 63 | + |
| 64 | + assert len(result.errors) == 1 |
| 65 | + assert type(result.errors[0]) == InvalidOperationError |
| 66 | + assert result.body is None |
| 67 | + assert result.parameters == {} |
| 68 | + |
| 69 | + def test_missing_parameter(self, validator): |
| 70 | + request = RequestMock(self.host_url, 'get', '/v1/pets') |
| 71 | + |
| 72 | + result = validator.validate(request) |
| 73 | + |
| 74 | + assert type(result.errors[0]) == MissingParameterError |
| 75 | + assert result.body is None |
| 76 | + assert result.parameters == { |
| 77 | + 'query': { |
| 78 | + 'page': 1, |
| 79 | + 'search': '', |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + def test_get_pets(self, validator): |
| 84 | + request = RequestMock( |
| 85 | + self.host_url, 'get', '/v1/pets', |
| 86 | + path_pattern='/v1/pets', args={'limit': '10'}, |
| 87 | + ) |
| 88 | + |
| 89 | + result = validator.validate(request) |
| 90 | + |
| 91 | + assert result.errors == [] |
| 92 | + assert result.body is None |
| 93 | + assert result.parameters == { |
| 94 | + 'query': { |
| 95 | + 'limit': 10, |
| 96 | + 'page': 1, |
| 97 | + 'search': '', |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + def test_missing_body(self, validator): |
| 102 | + request = RequestMock( |
| 103 | + self.host_url, 'post', '/v1/pets', |
| 104 | + path_pattern='/v1/pets', |
| 105 | + ) |
| 106 | + |
| 107 | + result = validator.validate(request) |
| 108 | + |
| 109 | + assert len(result.errors) == 1 |
| 110 | + assert type(result.errors[0]) == MissingBodyError |
| 111 | + assert result.body is None |
| 112 | + assert result.parameters == {} |
| 113 | + |
| 114 | + def test_invalid_content_type(self, validator): |
| 115 | + request = RequestMock( |
| 116 | + self.host_url, 'post', '/v1/pets', |
| 117 | + path_pattern='/v1/pets', mimetype='text/csv', |
| 118 | + ) |
| 119 | + |
| 120 | + result = validator.validate(request) |
| 121 | + |
| 122 | + assert len(result.errors) == 1 |
| 123 | + assert type(result.errors[0]) == InvalidContentTypeError |
| 124 | + assert result.body is None |
| 125 | + assert result.parameters == {} |
| 126 | + |
| 127 | + def test_post_pets(self, validator, spec_dict): |
| 128 | + pet_name = 'Cat' |
| 129 | + pet_tag = 'cats' |
| 130 | + pet_street = 'Piekna' |
| 131 | + pet_city = 'Warsaw' |
| 132 | + data_json = { |
| 133 | + 'name': pet_name, |
| 134 | + 'tag': pet_tag, |
| 135 | + 'position': '2', |
| 136 | + 'address': { |
| 137 | + 'street': pet_street, |
| 138 | + 'city': pet_city, |
| 139 | + } |
| 140 | + } |
| 141 | + data = json.dumps(data_json) |
| 142 | + request = RequestMock( |
| 143 | + self.host_url, 'post', '/v1/pets', |
| 144 | + path_pattern='/v1/pets', data=data, |
| 145 | + ) |
| 146 | + |
| 147 | + result = validator.validate(request) |
| 148 | + |
| 149 | + assert result.errors == [] |
| 150 | + assert result.parameters == {} |
| 151 | + |
| 152 | + schemas = spec_dict['components']['schemas'] |
| 153 | + pet_model = schemas['PetCreate']['x-model'] |
| 154 | + address_model = schemas['Address']['x-model'] |
| 155 | + assert result.body.__class__.__name__ == pet_model |
| 156 | + assert result.body.name == pet_name |
| 157 | + assert result.body.tag == pet_tag |
| 158 | + assert result.body.position == 2 |
| 159 | + assert result.body.address.__class__.__name__ == address_model |
| 160 | + assert result.body.address.street == pet_street |
| 161 | + assert result.body.address.city == pet_city |
| 162 | + |
| 163 | + def test_get_pet(self, validator): |
| 164 | + request = RequestMock( |
| 165 | + self.host_url, 'get', '/v1/pets/1', |
| 166 | + path_pattern='/v1/pets/{petId}', view_args={'petId': '1'}, |
| 167 | + ) |
| 168 | + |
| 169 | + result = validator.validate(request) |
| 170 | + |
| 171 | + assert result.errors == [] |
| 172 | + assert result.body is None |
| 173 | + assert result.parameters == { |
| 174 | + 'path': { |
| 175 | + 'petId': 1, |
| 176 | + }, |
| 177 | + } |
0 commit comments