Example #1
0
 def test_location(self):
     parser = RequestParser()
     parser.add_argument("default", type=int)
     parser.add_argument("in_values", type=int, location="values")
     parser.add_argument("in_query", type=int, location="args")
     parser.add_argument("in_headers", type=int, location="headers")
     parser.add_argument("in_cookie", type=int, location="cookie")
     assert parser.__schema__ == [
         {
             "name": "default",
             "type": "integer",
             "in": "query",
         },
         {
             "name": "in_values",
             "type": "integer",
             "in": "query",
         },
         {
             "name": "in_query",
             "type": "integer",
             "in": "query",
         },
         {
             "name": "in_headers",
             "type": "integer",
             "in": "header",
         },
     ]
Example #2
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"
Example #3
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
Example #4
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ß"
Example #5
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"
Example #6
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ß"
Example #7
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ß'
Example #8
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ß'
Example #9
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'
Example #10
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
Example #11
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)
Example #12
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
Example #13
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)
Example #14
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',
     }]
Example #15
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',
     }]
Example #16
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
Example #17
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'
Example #18
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
Example #19
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"
Example #20
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',
     }]
Example #21
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)
Example #22
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",
     }]
Example #23
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"
Example #24
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",
     }]
Example #25
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
Example #26
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",
     }]
Example #27
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",
     }]
Example #28
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", "<=")
Example #29
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"]
Example #30
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'