def test_get_pets_wrong_body_type(self, spec): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' pet_name = 'Cat' pet_tag = 'cats' pet_address = 'address text' data_json = { 'name': pet_name, 'tag': pet_tag, 'address': pet_address, } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, ) parameters = request.get_parameters(spec) assert parameters == {} with pytest.raises(InvalidMediaTypeValue): request.get_body(spec)
def test_post_pets_extra_body_properties(self, spec, spec_dict): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' pet_name = 'Cat' alias = 'kitty' data_json = { 'name': pet_name, 'alias': alias, } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, ) parameters = request.get_parameters(spec) assert parameters == {} with pytest.raises(UndefinedSchemaProperty): request.get_body(spec)
def test_post_pets_only_required_body(self, spec, spec_dict): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' pet_name = 'Cat' data_json = { 'name': pet_name, } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, ) parameters = request.get_parameters(spec) assert parameters == {} body = request.get_body(spec) schemas = spec_dict['components']['schemas'] pet_model = schemas['PetCreate']['x-model'] assert body.__class__.__name__ == pet_model assert body.name == pet_name assert not hasattr(body, 'tag') assert not hasattr(body, 'address')
def test_get_pets_none_value(self, spec): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' query_params = { 'limit': None, } request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, args=query_params, ) parameters = request.get_parameters(spec) assert parameters == { 'query': { 'limit': None, 'page': 1, 'search': '', } } body = request.get_body(spec) assert body is None
def test_get_pets(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' query_params = { 'limit': '20', } request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, args=query_params, ) parameters = request.get_parameters(spec) body = request.get_body(spec) assert parameters == { 'query': { 'limit': 20, 'page': 1, 'search': '', } } assert body is None data_json = { 'data': [], } data = json.dumps(data_json) response = MockResponse(data) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json
def test_get_pet_not_found(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets/{petId}' view_args = { 'petId': '1', } request = MockRequest( host_url, 'GET', '/pets/1', path_pattern=path_pattern, view_args=view_args, ) parameters = request.get_parameters(spec) assert parameters == { 'path': { 'petId': 1, } } body = request.get_body(spec) assert body is None data_json = { 'code': 404, 'message': 'Not found', 'rootCause': 'Pet not found', } data = json.dumps(data_json) response = MockResponse(data, status_code=404) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json
def test_post_pets_empty_body(self, spec, spec_dict): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' data_json = {} data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, ) parameters = request.get_parameters(spec) assert parameters == {} with pytest.raises(MissingProperty): request.get_body(spec)
def test_post_pets_raises_invalid_server_error(self, spec): host_url = 'http://flowerstore.swagger.io/v1' path_pattern = '/v1/pets' data_json = { 'name': 'Cat', 'tag': 'cats', } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, mimetype='text/html', ) with pytest.raises(InvalidServer): request.get_parameters(spec) with pytest.raises(InvalidServer): request.get_body(spec)
def test_post_pets_raises_invalid_mimetype(self, spec): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' data_json = { 'name': 'Cat', 'tag': 'cats', } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, mimetype='text/html', ) parameters = request.get_parameters(spec) assert parameters == {} with pytest.raises(InvalidContentType): request.get_body(spec)
def test_get_pets_raises_missing_required_param(self, spec): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, ) with pytest.raises(MissingParameter): request.get_parameters(spec) body = request.get_body(spec) assert body is None
def test_get_pets_empty_value(self, spec): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' query_params = { 'limit': '', } request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, args=query_params, ) with pytest.raises(EmptyValue): request.get_parameters(spec) body = request.get_body(spec) assert body is None
def test_post_pets_boolean_string(self, spec, spec_dict): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' pet_name = 'Cat' pet_tag = 'cats' pet_street = 'Piekna' pet_city = 'Warsaw' pet_healthy = 'false' data_json = { 'name': pet_name, 'tag': pet_tag, 'position': '2', 'address': { 'street': pet_street, 'city': pet_city, }, 'healthy': pet_healthy, } data = json.dumps(data_json) request = MockRequest( host_url, 'POST', '/pets', path_pattern=path_pattern, data=data, ) parameters = request.get_parameters(spec) assert parameters == {} body = request.get_body(spec) schemas = spec_dict['components']['schemas'] pet_model = schemas['PetCreate']['x-model'] address_model = schemas['Address']['x-model'] assert body.__class__.__name__ == pet_model assert body.name == pet_name assert body.tag == pet_tag assert body.position == 2 assert body.address.__class__.__name__ == address_model assert body.address.street == pet_street assert body.address.city == pet_city assert body.healthy is False
def test_get_pet(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets/{petId}' view_args = { 'petId': '1', } request = MockRequest( host_url, 'GET', '/pets/1', path_pattern=path_pattern, view_args=view_args, ) parameters = request.get_parameters(spec) assert parameters == { 'path': { 'petId': 1, } } body = request.get_body(spec) assert body is None data_json = { 'data': { 'id': 1, 'name': 'test', }, } data = json.dumps(data_json) response = MockResponse(data) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json