|
4 | 4 |
|
5 | 5 | from openapi_core.exceptions import ( |
6 | 6 | MissingParameterError, InvalidContentTypeError, InvalidServerError, |
| 7 | + InvalidValueType, UndefinedSchemaProperty, MissingPropertyError, |
7 | 8 | ) |
8 | 9 | from openapi_core.media_types import MediaType |
9 | 10 | from openapi_core.operations import Operation |
@@ -139,41 +140,38 @@ def test_get_pets(self, spec): |
139 | 140 | } |
140 | 141 | assert body is None |
141 | 142 |
|
142 | | - def test_get_pets_raises_missing_required_param(self, spec): |
| 143 | + def test_get_pets_wrong_parameter_type(self, spec): |
143 | 144 | host_url = 'http://petstore.swagger.io/v1' |
144 | 145 | path_pattern = '/v1/pets' |
| 146 | + query_params = { |
| 147 | + 'limit': 'twenty', |
| 148 | + } |
| 149 | + |
145 | 150 | request = RequestMock( |
146 | 151 | host_url, 'GET', '/pets', |
147 | | - path_pattern=path_pattern, |
| 152 | + path_pattern=path_pattern, args=query_params, |
148 | 153 | ) |
149 | 154 |
|
150 | | - with pytest.raises(MissingParameterError): |
| 155 | + with pytest.raises(InvalidValueType): |
151 | 156 | request.get_parameters(spec) |
152 | 157 |
|
153 | 158 | body = request.get_body(spec) |
154 | 159 |
|
155 | 160 | assert body is None |
156 | 161 |
|
157 | | - def test_get_pets_failed_to_cast(self, spec): |
| 162 | + def test_get_pets_raises_missing_required_param(self, spec): |
158 | 163 | host_url = 'http://petstore.swagger.io/v1' |
159 | 164 | path_pattern = '/v1/pets' |
160 | | - query_params = { |
161 | | - 'limit': 'non_integer_value', |
162 | | - } |
163 | | - |
164 | 165 | request = RequestMock( |
165 | 166 | host_url, 'GET', '/pets', |
166 | | - path_pattern=path_pattern, args=query_params, |
| 167 | + path_pattern=path_pattern, |
167 | 168 | ) |
168 | 169 |
|
169 | | - parameters = request.get_parameters(spec) |
| 170 | + with pytest.raises(MissingParameterError): |
| 171 | + request.get_parameters(spec) |
| 172 | + |
170 | 173 | body = request.get_body(spec) |
171 | 174 |
|
172 | | - assert parameters == { |
173 | | - 'query': { |
174 | | - 'limit': 'non_integer_value', |
175 | | - } |
176 | | - } |
177 | 175 | assert body is None |
178 | 176 |
|
179 | 177 | def test_get_pets_empty_value(self, spec): |
@@ -260,6 +258,99 @@ def test_post_pets(self, spec, spec_dict): |
260 | 258 | assert body.address.street == pet_street |
261 | 259 | assert body.address.city == pet_city |
262 | 260 |
|
| 261 | + def test_post_pets_empty_body(self, spec, spec_dict): |
| 262 | + host_url = 'http://petstore.swagger.io/v1' |
| 263 | + path_pattern = '/v1/pets' |
| 264 | + data_json = {} |
| 265 | + data = json.dumps(data_json) |
| 266 | + |
| 267 | + request = RequestMock( |
| 268 | + host_url, 'POST', '/pets', |
| 269 | + path_pattern=path_pattern, data=data, |
| 270 | + ) |
| 271 | + |
| 272 | + parameters = request.get_parameters(spec) |
| 273 | + |
| 274 | + assert parameters == {} |
| 275 | + |
| 276 | + with pytest.raises(MissingPropertyError): |
| 277 | + request.get_body(spec) |
| 278 | + |
| 279 | + def test_post_pets_extra_body_properties(self, spec, spec_dict): |
| 280 | + host_url = 'http://petstore.swagger.io/v1' |
| 281 | + path_pattern = '/v1/pets' |
| 282 | + pet_name = 'Cat' |
| 283 | + alias = 'kitty' |
| 284 | + data_json = { |
| 285 | + 'name': pet_name, |
| 286 | + 'alias': alias, |
| 287 | + } |
| 288 | + data = json.dumps(data_json) |
| 289 | + |
| 290 | + request = RequestMock( |
| 291 | + host_url, 'POST', '/pets', |
| 292 | + path_pattern=path_pattern, data=data, |
| 293 | + ) |
| 294 | + |
| 295 | + parameters = request.get_parameters(spec) |
| 296 | + |
| 297 | + assert parameters == {} |
| 298 | + |
| 299 | + with pytest.raises(UndefinedSchemaProperty): |
| 300 | + request.get_body(spec) |
| 301 | + |
| 302 | + def test_post_pets_only_required_body(self, spec, spec_dict): |
| 303 | + host_url = 'http://petstore.swagger.io/v1' |
| 304 | + path_pattern = '/v1/pets' |
| 305 | + pet_name = 'Cat' |
| 306 | + data_json = { |
| 307 | + 'name': pet_name, |
| 308 | + } |
| 309 | + data = json.dumps(data_json) |
| 310 | + |
| 311 | + request = RequestMock( |
| 312 | + host_url, 'POST', '/pets', |
| 313 | + path_pattern=path_pattern, data=data, |
| 314 | + ) |
| 315 | + |
| 316 | + parameters = request.get_parameters(spec) |
| 317 | + |
| 318 | + assert parameters == {} |
| 319 | + |
| 320 | + body = request.get_body(spec) |
| 321 | + |
| 322 | + schemas = spec_dict['components']['schemas'] |
| 323 | + pet_model = schemas['PetCreate']['x-model'] |
| 324 | + assert body.__class__.__name__ == pet_model |
| 325 | + assert body.name == pet_name |
| 326 | + assert body.tag is None |
| 327 | + assert body.address is None |
| 328 | + |
| 329 | + def test_get_pets_wrong_body_type(self, spec): |
| 330 | + host_url = 'http://petstore.swagger.io/v1' |
| 331 | + path_pattern = '/v1/pets' |
| 332 | + pet_name = 'Cat' |
| 333 | + pet_tag = 'cats' |
| 334 | + pet_address = 'address text' |
| 335 | + data_json = { |
| 336 | + 'name': pet_name, |
| 337 | + 'tag': pet_tag, |
| 338 | + 'address': pet_address, |
| 339 | + } |
| 340 | + data = json.dumps(data_json) |
| 341 | + |
| 342 | + request = RequestMock( |
| 343 | + host_url, 'POST', '/pets', |
| 344 | + path_pattern=path_pattern, data=data, |
| 345 | + ) |
| 346 | + |
| 347 | + parameters = request.get_parameters(spec) |
| 348 | + |
| 349 | + assert parameters == {} |
| 350 | + |
| 351 | + with pytest.raises(InvalidValueType): |
| 352 | + request.get_body(spec) |
| 353 | + |
263 | 354 | def test_post_pets_raises_invalid_content_type(self, spec): |
264 | 355 | host_url = 'http://petstore.swagger.io/v1' |
265 | 356 | path_pattern = '/v1/pets' |
|
0 commit comments