def test_parse_required(self, app): parser = RequestParser() parser.add_argument('foo', required=True, location='values') expected = { 'foo': 'Missing required parameter in the post body or the query string' } with pytest.raises(BadRequest) as cm: parser.parse_args() assert cm.value.data['message'] == 'Input payload validation failed' assert cm.value.data['errors'] == expected parser = RequestParser() parser.add_argument('bar', required=True, location=['values', 'cookies']) expected = { 'bar': ("Missing required parameter in the post body or the query " "string or the request's cookies") } with pytest.raises(BadRequest) as cm: parser.parse_args() assert cm.value.data['message'] == 'Input payload validation failed' assert cm.value.data['errors'] == expected
def test_parse_choices_sensitive(self, app): req = Request.from_values('/bubble?foo=BAT') parser = RequestParser() parser.add_argument('foo', choices=['bat'], case_sensitive=True), with pytest.raises(BadRequest): parser.parse_args(req)
def test_not_json_location_and_content_type_json(self, app): parser = RequestParser() parser.add_argument('foo', location='args') with app.test_request_context('/bubble', method='get', content_type='application/json'): parser.parse_args() # Should not raise a 400: BadRequest
def test_parse_lte_gte_mock(self, mocker): mock_type = mocker.Mock() parser = RequestParser() parser.add_argument('foo', type=mock_type, operators=['<=']) parser.parse_args() mock_type.assert_called_with('bar', 'foo', '<=')
def test_parse_choices(self, app): req = Request.from_values('/bubble?foo=bar') parser = RequestParser() parser.add_argument('foo', choices=['bat']), with pytest.raises(BadRequest): parser.parse_args(req)
def test_int_range_choice_types(self, app): parser = RequestParser() parser.add_argument('foo', type=int, choices=range(100), location='json') with app.test_request_context('/bubble', method='post', data=json.dumps({'foo': 101}), content_type='application/json'): with pytest.raises(BadRequest): parser.parse_args()
def test_no_help(self, app, mocker): abort = mocker.patch('flask_restplus.reqparse.abort', side_effect=BadRequest('Bad Request')) parser = RequestParser() parser.add_argument('foo', choices=['one', 'two']) req = mocker.Mock(['values']) req.values = MultiDict([('foo', 'three')]) with pytest.raises(BadRequest): parser.parse_args(req) expected = { 'foo': 'The value \'three\' is not a valid choice for \'foo\'.' } abort.assert_called_with(400, 'Input payload validation failed', errors=expected)
def test_parse_unicode(self, app): req = Request.from_values('/bubble?foo=barß') parser = RequestParser() parser.add_argument('foo') args = parser.parse_args(req) assert args['foo'] == 'barß'
def test_parse_unicode_app(self, app): parser = RequestParser() parser.add_argument('foo') with app.test_request_context('/bubble?foo=barß'): args = parser.parse_args() assert args['foo'] == 'barß'
def test_passing_arguments_object(self, app): req = Request.from_values('/bubble?foo=bar') parser = RequestParser() parser.add_argument(Argument('foo')) args = parser.parse_args(req) assert args['foo'] == 'bar'
def test_trim_request_parser(self, app): req = Request.from_values('/bubble?foo= 1 &bar=bees&n=22') parser = RequestParser(trim=False) parser.add_argument('foo') args = parser.parse_args(req) assert args['foo'] == ' 1 ' parser = RequestParser(trim=True) parser.add_argument('foo') args = parser.parse_args(req) assert args['foo'] == '1' parser = RequestParser(trim=True) parser.add_argument('foo', type=int) args = parser.parse_args(req) assert args['foo'] == 1
def test_viewargs(self, mocker): req = Request.from_values() req.view_args = {'foo': 'bar'} parser = RequestParser() parser.add_argument('foo', location=['view_args']) args = parser.parse_args(req) assert args['foo'] == 'bar' req = mocker.Mock() req.values = () req.json = None req.view_args = {'foo': 'bar'} parser = RequestParser() parser.add_argument('foo', store_missing=True) args = parser.parse_args(req) assert args['foo'] is None
def test_parse_choices_insensitive(self, app): req = Request.from_values('/bubble?foo=BAT') parser = RequestParser() parser.add_argument('foo', choices=['bat'], case_sensitive=False), args = parser.parse_args(req) assert 'bat' == args.get('foo') # both choices and args are case_insensitive req = Request.from_values('/bubble?foo=bat') parser = RequestParser() parser.add_argument('foo', choices=['BAT'], case_sensitive=False), args = parser.parse_args(req) assert 'bat' == args.get('foo')
def test_type_callable(self, app): req = Request.from_values('/bubble?foo=1') parser = RequestParser() parser.add_argument('foo', type=lambda x: x, required=False), args = parser.parse_args(req) assert args['foo'] == '1'
def test_parse_gte_lte_eq(self): parser = RequestParser() parser.add_argument('foo', operators=['>=', '<=', '='], action='append'), args = parser.parse_args() assert args['foo'] == ['bar', 'bat', 'foo']
def test_parse_ignore(self, app): req = Request.from_values('/bubble?foo=bar') parser = RequestParser() parser.add_argument('foo', type=int, ignore=True, store_missing=True), args = parser.parse_args(req) assert args['foo'] is None
def test_parse_store_missing(self, app): req = Request.from_values('/bubble') parser = RequestParser() parser.add_argument('foo', store_missing=False) args = parser.parse_args(req) assert 'foo' not in args
def test_parse_choices_correct(self, app): req = Request.from_values('/bubble?foo=bat') parser = RequestParser() parser.add_argument('foo', choices=['bat']), args = parser.parse_args(req) assert args['foo'] == 'bat'
def test_parse_error_bundling_w_parser_arg(self, app): parser = RequestParser(bundle_errors=True) parser.add_argument('foo', required=True, location='values') parser.add_argument('bar', required=True, location=['values', 'cookies']) with pytest.raises(BadRequest) as cm: parser.parse_args() assert cm.value.data['message'] == 'Input payload validation failed' assert cm.value.data['errors'] == { 'foo': 'Missing required parameter in the post body or the query string', 'bar': ("Missing required parameter in the post body or the query string " "or the request's cookies") }
def test_none_argument(self, app): parser = RequestParser() parser.add_argument('foo', location='json') with app.test_request_context('/bubble', method='post', data=json.dumps({'foo': None}), content_type='application/json'): args = parser.parse_args() assert args['foo'] is None
def test_type_decimal(self, app): parser = RequestParser() parser.add_argument('foo', type=decimal.Decimal, location='json') with app.test_request_context('/bubble', method='post', data=json.dumps({'foo': '1.0025'}), content_type='application/json'): args = parser.parse_args() assert args['foo'] == decimal.Decimal('1.0025')
def test_parse_default_append(self): parser = RequestParser() parser.add_argument('foo', default='bar', action='append', store_missing=True) args = parser.parse_args() assert args['foo'] == 'bar'
def test_parse_append_ignore(self, app): parser = RequestParser() parser.add_argument('foo', ignore=True, type=int, action='append', store_missing=True), args = parser.parse_args() assert args['foo'] is None
def test_both_json_and_values_location(self, app): parser = RequestParser() parser.add_argument('foo', type=int) parser.add_argument('baz', type=int) with app.test_request_context('/bubble?foo=1', method='post', data=json.dumps({'baz': 2}), content_type='application/json'): args = parser.parse_args() assert args['foo'] == 1 assert args['baz'] == 2
def test_type_callable_none(self, app): parser = RequestParser() parser.add_argument('foo', type=lambda x: x, location='json', required=False), with app.test_request_context('/bubble', method='post', data=json.dumps({'foo': None}), content_type='application/json'): args = parser.parse_args() assert args['foo'] is None
def test_parse_model(self, app): model = Model('Todo', {'task': fields.String(required=True)}) parser = RequestParser() parser.add_argument('todo', type=model, required=True) data = {'todo': {'task': 'aaa'}} with app.test_request_context('/', method='post', data=json.dumps(data), content_type='application/json'): args = parser.parse_args() assert args['todo'] == {'task': 'aaa'}
def test_type_filestorage(self, app): parser = RequestParser() parser.add_argument('foo', type=FileStorage, location='files') fdata = 'foo bar baz qux'.encode('utf-8') with app.test_request_context( '/bubble', method='POST', data={'foo': (six.BytesIO(fdata), 'baz.txt')}): args = parser.parse_args() assert args['foo'].name == 'foo' assert args['foo'].filename == 'baz.txt' assert args['foo'].read() == fdata
def test_trim_request_parser_json(self, app): parser = RequestParser(trim=True) parser.add_argument('foo', location='json') parser.add_argument('int1', location='json', type=int) parser.add_argument('int2', location='json', type=int) with app.test_request_context('/bubble', method='post', data=json.dumps({ 'foo': ' bar ', 'int1': 1, 'int2': ' 2 ' }), content_type='application/json'): args = parser.parse_args() assert args['foo'] == 'bar' assert args['int1'] == 1 assert args['int2'] == 2
def test_filestorage_custom_type(self, app): def _custom_type(f): return FileStorage(stream=f.stream, filename='{0}aaaa'.format(f.filename), name='{0}aaaa'.format(f.name)) parser = RequestParser() parser.add_argument('foo', type=_custom_type, location='files') fdata = 'foo bar baz qux'.encode('utf-8') with app.test_request_context( '/bubble', method='POST', data={'foo': (six.BytesIO(fdata), 'baz.txt')}): args = parser.parse_args() assert args['foo'].name == 'fooaaaa' assert args['foo'].filename == 'baz.txtaaaa' assert args['foo'].read() == fdata
def test_strict_parsing_on_partial_hit(self, app): req = Request.from_values('/bubble?foo=1&bar=bees&n=22') parser = RequestParser() parser.add_argument('foo', type=int) with pytest.raises(BadRequest): parser.parse_args(req, strict=True)