예제 #1
0
    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'
예제 #2
0
    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ß'
예제 #3
0
    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ß'
예제 #4
0
    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
예제 #5
0
    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', '<=')
예제 #6
0
    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']
예제 #7
0
    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'
예제 #8
0
 def test_location_form(self):
     parser = RequestParser()
     parser.add_argument('in_form', type=int, location='form')
     assert parser.__schema__ == [{
         'name': 'in_form',
         'type': 'integer',
         'in': 'formData',
     }]
예제 #9
0
    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)
예제 #10
0
    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)
예제 #11
0
    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
예제 #12
0
    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'
예제 #13
0
    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
예제 #14
0
 def test_location_files(self):
     parser = RequestParser()
     parser.add_argument('in_files', type=FileStorage, location='files')
     assert parser.__schema__ == [{
         'name': 'in_files',
         'type': 'file',
         'in': 'formData',
     }]
예제 #15
0
    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
예제 #16
0
 def test_location_json(self):
     parser = RequestParser()
     parser.add_argument('in_json', type=str, location='json')
     assert parser.__schema__ == [{
         'name': 'in_json',
         'type': 'string',
         'in': 'body',
     }]
예제 #17
0
 def test_unknown_type(self):
     parser = RequestParser()
     parser.add_argument('unknown', type=lambda v: v)
     assert parser.__schema__ == [{
         'name': 'unknown',
         'type': 'string',
         'in': 'query',
     }]
예제 #18
0
 def test_default_as_false(self):
     parser = RequestParser()
     parser.add_argument('bool', type=inputs.boolean, default=False)
     assert parser.__schema__ == [{
         'name': 'bool',
         'type': 'boolean',
         'in': 'query',
         'default': False,
     }]
예제 #19
0
 def test_default(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, default=5)
     assert parser.__schema__ == [{
         'name': 'int',
         'type': 'integer',
         'in': 'query',
         'default': 5,
     }]
예제 #20
0
 def test_required(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, required=True)
     assert parser.__schema__ == [{
         'name': 'int',
         'type': 'integer',
         'in': 'query',
         'required': True,
     }]
예제 #21
0
    def test_request_parser_remove_argument(self):
        req = Request.from_values('/bubble?foo=baz')
        parser = RequestParser()
        parser.add_argument('foo', type=int)
        parser_copy = parser.copy()
        parser_copy.remove_argument('foo')

        args = parser_copy.parse_args(req)
        assert args == {}
예제 #22
0
    def test_form_and_body_location(self):
        parser = RequestParser()
        parser.add_argument('default', type=int)
        parser.add_argument('in_form', type=int, location='form')
        parser.add_argument('in_json', type=str, location='json')
        with pytest.raises(SpecsError) as cm:
            parser.__schema__

        assert cm.value.msg == "Can't use formData and body at the same time"
예제 #23
0
 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
예제 #24
0
    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'
예제 #25
0
 def test_choices(self):
     parser = RequestParser()
     parser.add_argument('string', type=str, choices=['a', 'b'])
     assert parser.__schema__ == [{
         'name': 'string',
         'type': 'string',
         'in': 'query',
         'enum': ['a', 'b'],
         'collectionFormat': 'multi',
     }]
예제 #26
0
    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
예제 #27
0
    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')
예제 #28
0
 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
예제 #29
0
 def test_models(self):
     todo_fields = Model('Todo', {
         'task':
         fields.String(required=True, description='The task details')
     })
     parser = RequestParser()
     parser.add_argument('todo', type=todo_fields)
     assert parser.__schema__ == [{
         'name': 'todo',
         'type': 'Todo',
         'in': 'body',
     }]
예제 #30
0
 def test_split_lists(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, action='split')
     assert parser.__schema__ == [{
         'name': 'int',
         'in': 'query',
         'type': 'array',
         'collectionFormat': 'csv',
         'items': {
             'type': 'integer'
         }
     }]