def test_location_invalid(empty_swagger_spec, body_param_spec): body_param_spec['in'] = 'foo' param = Param(empty_swagger_spec, Mock(spec=Operation), body_param_spec) with pytest.raises(SwaggerMappingError) as excinfo: get_param_type_spec(param) assert 'location foo' in str(excinfo)
def test_location_unknown(empty_swagger_spec): param_spec = { 'in': 'foo', } param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) with pytest.raises(SwaggerMappingError) as excinfo: get_param_type_spec(param) assert 'location foo' in str(excinfo)
def swagger_validated_function(*args, **kwargs): converted_uri = request.path # convert /pet/mypetsid to /pet/{petId} for key, value in request.view_args.items(): target = '{{{0}}}'.format(key) converted_uri = converted_uri.replace(str(value), target) # Grab the swagger spec for this specific uri and request type request_spec = spec.get_op_for_request( request.method.lower(), converted_uri) # cycle through the params and check any params that are set or required # by the schema for param in request_spec.params.values(): param_spec = get_param_type_spec(param) # TODO - grab out other request types that we care about param_value = None if param.location == 'formData': param_value = request.form.get(param.name) elif param.location == 'path': param_value = request.view_args.get(param.name) if param_value or param.required: try: validate_schema_object(spec, param_spec, param_value) except Exception as e: abort(400, str(e)) return f(*args, **kwargs)
def test_ref(minimal_swagger_dict, body_param_spec): minimal_swagger_dict['parameters'] = { 'PetIdParam': body_param_spec } param_ref_spec = {'$ref': '#/parameters/PetIdParam'} swagger_spec = Spec(minimal_swagger_dict) param = Param(swagger_spec, Mock(spec=Operation), param_ref_spec) assert {'type': 'string'} == get_param_type_spec(param)
def test_ref(minimal_swagger_dict, body_param_spec): minimal_swagger_dict['parameters'] = { 'PetIdParam': body_param_spec, } param_ref_spec = {'$ref': '#/parameters/PetIdParam'} swagger_spec = Spec(minimal_swagger_dict) param = Param(swagger_spec, Mock(spec=Operation), param_ref_spec) assert {'type': 'string'} == get_param_type_spec(param)
def test_location_is_not_body(empty_swagger_spec): for location in ('path', 'query', 'header', 'formData',): param_spec = { 'name': 'petId', 'in': location, 'description': 'ID of pet that needs to be updated', 'required': True, 'type': 'string', } param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) assert param_spec == get_param_type_spec(param)
def test_request_body_name_synchronization_required_and_declared_keys(resource, operation, param): """Check that each parameter transmitted in the body is similarly named to the resource it is declared for and has a wrapper key marked as required and declared in properties """ # TODO: Use a real stemmer assert resource.name.rstrip('s') == param.name spec = get_param_type_spec(param) assert 'required' in spec assert param.name in spec['required'] assert param.name in spec['properties'].keys()
def test_location_is_body(empty_swagger_spec): param_spec = { 'name': 'body', 'in': 'body', 'description': 'ID of pet that needs to be updated', 'required': True, 'schema': { 'type': 'string' } } param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) assert param_spec['schema'] == get_param_type_spec(param)
async def unmarshal_param(param, request): """Unmarshal the given parameter from the passed in request like object. :type param: :class:`bravado_core.param.Param` :type request: :class:`bravado_core.request.IncomingRequest` :return: value of parameter """ swagger_spec = param.swagger_spec deref = swagger_spec.deref param_spec = deref(get_param_type_spec(param)) location = param.location param_type = deref(param_spec.get('type')) cast_param = partial(cast_request_param, param_type, param.name) default_value = schema.get_default(swagger_spec, param_spec) if location == Location.path: raw_value = cast_param(request.match_info.get(param.name, None)) elif location == Location.query: raw_value = cast_param(request.query.get(param.name, default_value)) elif location == Location.header: raw_value = cast_param(request.headers.get(param.name, default_value)) elif location == Location.form_data: if param_type == 'file': raw_value = request.files.get(param.name, None) else: raw_value = cast_param(request.form.get(param.name, default_value)) elif location == Location.body: # TODO: verify content-type header try: raw_value = request.json() except ValueError as json_error: raise SwaggerMappingError( "Error reading request body JSON: {0}".format(str(json_error))) else: raise SwaggerMappingError( "Don't know how to unmarshal_param with location {0}".format( location)) if raw_value is None and not schema.is_required(swagger_spec, param_spec): return None if param_type == 'array' and location != Location.body: raw_value = unmarshal_collection_format(swagger_spec, param_spec, raw_value) if swagger_spec.config['validate_requests']: validate_schema_object(swagger_spec, param_spec, raw_value) value = unmarshal_schema_object(swagger_spec, param_spec, raw_value) return value
def fuzz(self, existing_data: Dict[str, Any]) -> Dict[str, Any]: """Returns a dictionary of values which can be used to call the operation being fuzzed. """ if not self._fuzzed_input_factory: parameters = [] for name, param in self._swagger_operation.params.items(): specification = get_param_type_spec(param).copy() if param.location == 'body': # For 'body' parameters, bravado discards information from the # param spec itself. We pass in the 'required' parameter in this # case. # For the 'name' argument (seeing that body parameters can be # named differently), we pass it in separately as it breaks the # swagger specification if we group it together. specification['required'] = param.required parameters.append(( name, specification, )) self._fuzzed_input_factory = fuzz_parameters( parameters, self.operation_id) # NOTE: If we were really worried about performance later on, # we might be able to address this. Specifically, we don't # *need* to generate examples, just to throw it away later # if the key is already in data. # However, this involves parameter modification, which may # require a more involved change. fuzzed_input = {} for key, value in self._fuzzed_input_factory.example().items(): if key in existing_data: fuzzed_input[key] = existing_data[key] continue if value is not None: fuzzed_input[key] = value return fuzzed_input
def test_location_is_body(empty_swagger_spec, body_param_spec): param = Param(empty_swagger_spec, Mock(spec=Operation), body_param_spec) assert body_param_spec['schema'] == get_param_type_spec(param)