Example #1
0
 def test_empty(self):
     schema = self._make_schema()
     request = self._make_request('{}')
     validate_colander_schema(schema, request)
     self.assertEqual(request.errors, [])
     self.assertEqual(request.validated,
                      {'cell': (), 'wifi': (), 'radio': ''})
Example #2
0
        def test_colander_schema_using_defaults(self):
            """
            Schema could contains default values
            """
            schema = CorniceSchema.from_colander(DefaultSchema)

            dummy_request = MockRequest('', {'bar': 'test'})
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)
            self.assertEqual(len(qs_fields), 2)

            expected = {'foo': 'foo', 'bar': 'test'}

            self.assertEqual(expected, dummy_request.validated)

            dummy_request = MockRequest('', {'bar': 'test', 'foo': 'test'})
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)
            self.assertEqual(len(qs_fields), 2)
Example #3
0
        def test_colander_schema_using_defaults(self):
            """
            Schema could contains default values
            """
            schema = CorniceSchema.from_colander(DefaultSchema)

            dummy_request = get_mock_request("", {"bar": "test"})
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)
            self.assertEqual(len(qs_fields), 2)

            expected = {"foo": "foo", "bar": "test"}

            self.assertEqual(expected, dummy_request.validated)

            dummy_request = get_mock_request("", {"bar": "test", "foo": "test"})
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)
            self.assertEqual(len(qs_fields), 2)

            expected = {"foo": "test", "bar": "test"}

            self.assertEqual(expected, dummy_request.validated)
Example #4
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            class MockRequest(object):
                def __init__(self, body):
                    self.headers = {}
                    self.matchdict = {}
                    self.body = body
                    self.GET = {}
                    self.POST = {}
                    self.validated = {}
                    self.registry = {
                        'cornice_deserializers': {
                            'application/json': extract_json_data
                        }
                    }

            dummy_request = MockRequest('{"bar": "required_data"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
Example #5
0
 def collection_post(self):
     set_config(self.request.user)
     if self.model.is_readonly:
         raise HTTPBadRequest()
     validate_colander_schema(self._schema, self.request)
     obj = self.model(**self.request.json_body)
     Session.add(obj)
     return obj
Example #6
0
        def test_sequence_with_null(self):
            # null can be passed to a sequence field
            schema = CorniceSchema.from_colander(TestNoneSchema)

            dummy_request = get_mock_request('{"foo": "abc", "bar": null}')
            validate_colander_schema(schema, dummy_request)
            self.assertEqual(len(dummy_request.errors), 0)
            self.assertIsNone(dummy_request.validated['bar'])
Example #7
0
 def test_minimal(self):
     schema = self._make_schema()
     request = self._make_request(
         '{"items": [{"lat": 12.3456781, "lon": 23.4567892}]}')
     validate_colander_schema(schema, request)
     self.assertFalse(request.errors)
     self.assertTrue('items' in request.validated)
     self.assertEqual(len(request.validated['items']), 1)
Example #8
0
        def test_validate_colander_schema_can_preserve_unknown_fields(self):
            schema = CorniceSchema.from_colander(PreserveUnkownSchema)

            data = json.dumps({"bar": "required_data", "optional": "true"})
            dummy_request = get_mock_request(data)
            validate_colander_schema(schema, dummy_request)

            self.assertDictEqual(dummy_request.validated, {"bar": "required_data", "optional": "true"})
            self.assertEqual(len(dummy_request.errors), 0)
Example #9
0
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args and not callable(view):
            params = dict(request=request)
            if 'factory' in args and 'acl' not in args:
                params['context'] = request.context
            ob = args['klass'](**params)
            if is_string(view):
                view_ = getattr(ob, view.lower())
            elif isinstance(view, _UnboundView):
                view_ = view.make_bound_view(ob)

        # set data deserializer
        if 'deserializer' in args:
            request.deserializer = args['deserializer']

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)
        elif hasattr(ob, 'schema'):
            validate_colander_schema(ob.schema, request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if is_string(validator) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        # only call the view if we don't have validation errors
        if len(request.errors) == 0:
            # if we have an object, the request had already been passed to it
            if ob:
                response = view_()
            else:
                response = view_(request)

        # check for errors and return them if any
        if len(request.errors) > 0:
            # We already checked for CORS, but since the response is created
            # again, we want to do that again before returning the response.
            request.info['cors_checked'] = False
            return args['error_handler'](request.errors)

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response
Example #10
0
        def test_extra_params_qs(self):
            schema = CorniceSchema.from_colander(QsSchema)
            dummy_request = get_mock_request("", {"foo": "test", "bar": "test"})
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)

            expected = {"foo": "test"}
            self.assertEqual(expected, dummy_request.validated)
Example #11
0
        def test_extra_params_qs(self):
            schema = CorniceSchema.from_colander(QsSchema)
            dummy_request = get_mock_request('', {'foo': 'test', 'bar': 'test'})
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)

            expected = {'foo': 'test'}
            self.assertEqual(expected, dummy_request.validated)
Example #12
0
        def test_colander_schema_with_root_validator(self):
            """
            Test the preparer run on the root node
            """
            schema = CorniceSchema.from_colander(WithValidatorSchema)

            dummy_request = MockRequest('{"bar": "heloo"}')
            with self.assertRaises(Invalid) as e:
                validate_colander_schema(schema, dummy_request)
            self.assertEqual({'': 'Bubblicious'}, e.exception.asdict())
Example #13
0
        def test_colander_schema_with_root_preparer(self):
            """
            Test the preparer run on the root node
            """
            schema = CorniceSchema.from_colander(WithPreparerSchema)

            dummy_request = MockRequest('{"bar": "not_mama"}')
            validate_colander_schema(schema, dummy_request)

            self.assertEqual('mama', dummy_request.validated['bar'])
Example #14
0
        def test_colander_schema_using_dotted_names(self):
            """
            Schema could be passed as string in view
            """
            schema = CorniceSchema.from_colander("cornice.tests.schema.AccountSchema")

            dummy_request = get_mock_request('{"nickname": "john"}')
            validate_colander_schema(schema, dummy_request)

            self.assertIn("nickname", dummy_request.validated)
            self.assertNotIn("city", dummy_request.validated)
Example #15
0
 def put(self):
     set_config(self.request.user)
     if self.model.is_readonly:
         raise HTTPBadRequest()
     validate_colander_schema(self._schema, self.request)
     result = Session.query(self.model).get(int(self.request.matchdict['id']))
     if result:
         for k, v in self.request.json_body.items():
             setattr(result, k, v)
         return result
     raise HTTPNotFound()
Example #16
0
        def test_imperative_colander_schema(self):
            # not specifying body should act the same way as specifying it
            schema = CorniceSchema.from_colander(imperative_schema)
            body_fields = schema.get_attributes(location="body")
            qs_fields = schema.get_attributes(location="querystring")

            self.assertEqual(len(body_fields), 2)
            self.assertEqual(len(qs_fields), 1)

            dummy_request = get_mock_request('{"bar": "some data"}')
            validate_colander_schema(schema, dummy_request)
Example #17
0
        def test_extra_params_qs_strict(self):
            schema = CorniceSchema.from_colander(StrictQsSchema)
            dummy_request = get_mock_request("", {"foo": "test", "bar": "test"})
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {"description": "bar is not allowed", "location": "querystring", "name": "bar"})

            expected = {"foo": "test"}
            self.assertEqual(expected, dummy_request.validated)
Example #18
0
        def test_colander_schema_default_value(self):
            # apply default value to field if the input for them is
            # missing
            schema = CorniceSchema.from_colander(DefaultValueSchema)
            dummy_request = get_mock_request('{"foo": 5}')
            validate_colander_schema(schema, dummy_request)

            self.assertIn('bar', dummy_request.validated)
            self.assertEqual(len(dummy_request.errors), 0)
            self.assertEqual(dummy_request.validated['foo'], 5)
            # default value should be available
            self.assertEqual(dummy_request.validated['bar'], 10)
Example #19
0
        def test_validate_colander_schema_can_preserve_unknown_fields(self):
            schema = CorniceSchema.from_colander(PreserveUnkownSchema)

            data = json.dumps({"bar": "required_data", "optional": "true"})
            dummy_request = get_mock_request(data)
            validate_colander_schema(schema, dummy_request)

            self.assertDictEqual(dummy_request.validated, {
                "bar": "required_data",
                "optional": "true"
            })
            self.assertEqual(len(dummy_request.errors), 0)
Example #20
0
        def test_colander_schema_using_dotted_names(self):
            """
            Schema could be passed as string in view
            """
            schema = CorniceSchema.from_colander(
                'cornice.tests.schema.AccountSchema')

            dummy_request = get_mock_request('{"nickname": "john"}')
            validate_colander_schema(schema, dummy_request)

            self.assertIn('nickname', dummy_request.validated)
            self.assertNotIn('city', dummy_request.validated)
Example #21
0
        def test_colander_schema_default_value(self):
            # apply default value to field if the input for them is
            # missing
            schema = CorniceSchema.from_colander(DefaultValueSchema)
            dummy_request = get_mock_request('{"foo": 5}')
            validate_colander_schema(schema, dummy_request)

            self.assertIn('bar', dummy_request.validated)
            self.assertEqual(len(dummy_request.errors), 0)
            self.assertEqual(dummy_request.validated['foo'], 5)
            # default value should be available
            self.assertEqual(dummy_request.validated['bar'], 10)
Example #22
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            dummy_request = MockRequest('{"bar": "required_data"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
Example #23
0
        def test_extra_params_qs_strict(self):
            schema = CorniceSchema.from_colander(StrictQsSchema)
            dummy_request = get_mock_request('', {'foo': 'test', 'bar': 'test'})
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {'description': 'bar is not allowed',
                                         'location': 'querystring',
                                         'name': 'bar'})

            expected = {'foo': 'test'}
            self.assertEqual(expected, dummy_request.validated)
Example #24
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            dummy_request = get_mock_request('{"bar": "required_data"}')
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
            self.assertEqual(len(dummy_request.errors), 0)
Example #25
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            dummy_request = MockRequest('{"bar": "required_data"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
Example #26
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            dummy_request = get_mock_request('{"bar": "required_data"}')
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
            self.assertEqual(len(dummy_request.errors), 0)
Example #27
0
        def test_colander_schema_using_dotted_names(self):
            """
            Schema could be passed as string in view
            """
            schema = CorniceSchema.from_colander(
                'cornice.tests.schema.AccountSchema')

            dummy_request = MockRequest('{"nickname": "john"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertIn('nickname', dummy_request.validated)
            self.assertNotIn('city', dummy_request.validated)
Example #28
0
        def test_extra_params_qs(self):
            schema = CorniceSchema.from_colander(QsSchema)
            dummy_request = get_mock_request('', {
                'foo': 'test',
                'bar': 'test'
            })
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0)

            expected = {'foo': 'test'}
            self.assertEqual(expected, dummy_request.validated)
Example #29
0
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args:
            params = dict(request=request)
            if 'factory' in args and 'acl' not in args:
                params['context'] = args['factory'](request)
            ob = args['klass'](**params)
            if is_string(view):
                view_ = getattr(ob, view.lower())

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)
        elif hasattr(ob, 'schema'):
            validate_colander_schema(ob.schema, request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if is_string(validator) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        # only call the view if we don't have validation errors
        if len(request.errors) == 0:
            # if we have an object, the request had already been passed to it
            if ob:
                response = view_()
            else:
                response = view_(request)

        # check for errors and return them if any
        if len(request.errors) > 0:
            # We already checked for CORS, but since the response is created
            # again, we want to do that again before returning the response.
            request.info['cors_checked'] = False
            return args['error_handler'](request.errors)

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response
Example #30
0
        def test_extra_params_qs_strict(self):
            schema = CorniceSchema.from_colander(StrictQsSchema)
            dummy_request = MockRequest('', {'foo': 'test', 'bar': 'test'})
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {'description': 'bar is not allowed',
                                         'location': 'querystring',
                                         'name': 'bar'})

            expected = {'foo': 'test'}
            self.assertEqual(expected, dummy_request.validated)
Example #31
0
        def test_colander_nested_schema(self):
            schema = CorniceSchema.from_colander(NestedSchema)

            dummy_request = get_mock_request('{"ham": {"bar": "POST"}}', {"egg.bar": "GET"})
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0, errors)
            self.assertEqual(len(qs_fields), 1)

            expected = {"egg": {"bar": "GET"}, "ham": {"bar": "POST"}}

            self.assertEqual(expected, dummy_request.validated)
Example #32
0
        def test_colander_strict_schema(self):
            schema = CorniceSchema.from_colander(StrictSchema)

            dummy_request = get_mock_request(
                """
                {"bar": "required_data", "foo": "optional_data",
                "other": "not_wanted_data"}
                """
            )
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {"description": "other is not allowed", "location": "body", "name": "other"})
            self.assertIn("foo", dummy_request.validated)
            self.assertIn("bar", dummy_request.validated)
Example #33
0
        def test_colander_strict_schema(self):
            schema = CorniceSchema.from_colander(StrictSchema)

            dummy_request = MockRequest('''{"bar": "required_data",
                                            "foo": "optional_data",
                                            "other": "not_wanted_data"}''')

            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {'description': 'other is not allowed',
                                         'location': 'body',
                                         'name': 'other'})
            self.assertIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
Example #34
0
        def test_colander_strict_schema(self):
            schema = CorniceSchema.from_colander(StrictSchema)

            dummy_request = MockRequest('''{"bar": "required_data",
                                            "foo": "optional_data",
                                            "other": "not_wanted_data"}''')

            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            errors = dummy_request.errors
            self.assertEqual(len(errors), 1)
            self.assertEqual(errors[0], {'description': 'other is not allowed',
                                         'location': 'body',
                                         'name': 'other'})
            self.assertIn('foo', dummy_request.validated)
            self.assertIn('bar', dummy_request.validated)
Example #35
0
        def test_colander_nested_schema(self):
            schema = CorniceSchema.from_colander(NestedSchema)

            dummy_request = MockRequest('{"ham": {"bar": "POST"}}',
                                        {'egg.bar': 'GET'})
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0, errors)
            self.assertEqual(len(qs_fields), 1)

            expected = {'egg': {'bar': 'GET'},
                        'ham': {'bar': 'POST'},
                        }

            self.assertEqual(expected, dummy_request.validated)
Example #36
0
        def test_colander_nested_schema(self):
            schema = CorniceSchema.from_colander(NestedSchema)

            dummy_request = MockRequest('{"ham": {"bar": "POST"}}',
                                        {'egg.bar': 'GET'})
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            qs_fields = schema.get_attributes(location="querystring")

            errors = dummy_request.errors
            self.assertEqual(len(errors), 0, errors)
            self.assertEqual(len(qs_fields), 1)

            expected = {'egg': {'bar': 'GET'},
                        'ham': {'bar': 'POST'},
                        }

            self.assertEqual(expected, dummy_request.validated)
Example #37
0
        def test_post_data_fails_validation_on_error(self):
            schema = CorniceSchema.from_colander(PostDataSchema)

            class MockRequest(object):
                def __init__(self, body):
                    self.headers = {}
                    self.matchdict = {}
                    self.body = body
                    self.GET = {}
                    self.POST = {'foo' : ''}
                    self.validated = {}

            dummy_request = MockRequest('{"bar": "irrelevant data"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)
            missing_foo = filter(lambda x: x['name'] == 'foo', dummy_request.errors)

            assert missing_foo
            self.assertNotIn('foo', dummy_request.validated)
Example #38
0
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args:
            ob = args['klass'](request)
            if isinstance(view, str):
                view_ = getattr(ob, view.lower())

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if isinstance(validator, str) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        # only call the view if we don't have validation errors
        if len(request.errors) == 0:
            # if we have an object, the request had already been passed to it
            if ob:
                response = view_()
            else:
                response = view_(request)

        # check for errors and return them if any
        if len(request.errors) > 0:
            return args['error_handler'](request.errors)

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response
Example #39
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            class MockRequest(object):
                def __init__(self, body):
                    self.headers = {}
                    self.matchdict = {}
                    self.body = body
                    self.GET = {}
                    self.POST = {}
                    self.validated = {}

            dummy_request = MockRequest('{"bar": "required_data"}')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
Example #40
0
        def test_colander_schema_using_drop(self):
            """
            remove fields from validated data if they deserialize to colander's
            `drop` object.
            """
            schema = CorniceSchema.from_colander(DropSchema)

            class MockRequest(object):
                def __init__(self, body):
                    self.headers = {}
                    self.matchdict = {}
                    self.body = body
                    self.GET = {}
                    self.POST = {}
                    self.validated = {}

            dummy_request = MockRequest('')
            setattr(dummy_request, 'errors', Errors(dummy_request))
            validate_colander_schema(schema, dummy_request)

            self.assertNotIn('foo', dummy_request.validated)
Example #41
0
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args:
            ob = args['klass'](request)
            if isinstance(view, basestring):
                view_ = getattr(ob, view.lower())

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if isinstance(validator, basestring) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        if len(request.errors) > 0:
            return args['error_handler'](request.errors)

        # if we have an object, the request had already been passed to it
        if ob:
            response = view_()
        else:
            response = view_(request)

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response
Example #42
0
 def test_wrong_cell_data(self):
     schema = self._make_schema()
     request = self._make_request(
         '{"cell": [{"mcc": "a", "mnc": 2, "lac": 3, "cid": 4}]}')
     validate_colander_schema(schema, request)
     self.assertTrue(request.errors)
Example #43
0
 def test_wrong_cell_data(self):
     schema = self._make_schema()
     request = self._make_request(
         '{"cell": [{"mcc": "a", "mnc": 2, "lac": 3, "cid": 4}]}')
     validate_colander_schema(schema, request)
     self.assertTrue(request.errors)
Example #44
0
 def validate_req(self, request):
     validate_colander_schema(validator_from_model(self.cls), request)
Example #45
0
 def test_empty(self):
     schema = self._make_schema()
     request = self._make_request('{}')
     validate_colander_schema(schema, request)
     self.assertTrue(request.errors)
Example #46
0
 def test_empty_wifi_entry(self):
     schema = self._make_schema()
     request = self._make_request(
         '{"lat": 12.3456781, "lon": 23.4567892, "wifi": [{}]}')
     validate_colander_schema(schema, request)
     self.assertTrue(request.errors)
Example #47
0
 def test_empty_cell_entry(self):
     schema = self._make_schema()
     request = self._make_request('{"cell": [{}]}')
     validate_colander_schema(schema, request)
     self.assertTrue('cell' in request.validated)