def validate(self, value):
     errors_iter = self.validator.iter_errors(value)
     errors = tuple(errors_iter)
     if errors:
         raise InvalidSchemaValue(value,
                                  self.schema.type,
                                  schema_errors=errors)
Esempio n. 2
0
    def test_get_pets_invalid_response(self, spec, response_validator):
        host_url = "http://petstore.swagger.io/v1"
        path_pattern = "/v1/pets"
        query_params = {
            "limit": "20",
        }

        request = MockRequest(
            host_url,
            "GET",
            "/pets",
            path_pattern=path_pattern,
            args=query_params,
        )

        with pytest.warns(DeprecationWarning):
            parameters = spec_validate_parameters(spec, request)
        body = spec_validate_body(spec, request)

        assert parameters == Parameters(
            query={
                "limit": 20,
                "page": 1,
                "search": "",
            }
        )
        assert body is None

        response_data_json = {
            "data": [
                {
                    "id": 1,
                    "name": {
                        "first_name": "Cat",
                    },
                }
            ],
        }
        response_data = json.dumps(response_data_json)
        response = MockResponse(response_data)

        with pytest.raises(InvalidSchemaValue):
            spec_validate_data(spec, request, response)

        response_result = response_validator.validate(request, response)

        schema_errors = response_result.errors[0].schema_errors
        assert response_result.errors == [
            InvalidSchemaValue(
                type="object",
                value=response_data_json,
                schema_errors=schema_errors,
            ),
        ]
        assert response_result.data is None
Esempio n. 3
0
    def test_get_pets_invalid_response(self, spec, response_validator):
        host_url = 'http://petstore.swagger.io/v1'
        path_pattern = '/v1/pets'
        query_params = {
            'limit': '20',
        }

        request = MockRequest(
            host_url, 'GET', '/pets',
            path_pattern=path_pattern, args=query_params,
        )

        parameters = validate_parameters(spec, request)
        body = validate_body(spec, request)

        assert parameters == RequestParameters(
            query={
                'limit': 20,
                'page': 1,
                'search': '',
            }
        )
        assert body is None

        response_data_json = {
            'data': [
                {
                    'id': 1,
                    'name': {
                        'first_name': 'Cat',
                    },
                }
            ],
        }
        response_data = json.dumps(response_data_json)
        response = MockResponse(response_data)

        with pytest.raises(InvalidSchemaValue):
            validate_data(spec, request, response)

        response_result = response_validator.validate(request, response)

        schema_errors = response_result.errors[0].schema_errors
        assert response_result.errors == [
            InvalidSchemaValue(
                type='object',
                value=response_data_json,
                schema_errors=schema_errors,
            ),
        ]
        assert response_result.data is None
Esempio n. 4
0
 def validate(self, value):
     errors_iter = self.validator.iter_errors(value)
     errors = tuple(errors_iter)
     if errors:
         schema_type = self.schema.getkey("type", "any")
         raise InvalidSchemaValue(value, schema_type, schema_errors=errors)
Esempio n. 5
0
 def _formatter_validate(self, value):
     result = self.formatter.validate(value)
     if not result:
         schema_type = self.schema.getkey("type", "any")
         raise InvalidSchemaValue(value, schema_type)
 def _formatter_validate(self, value):
     result = self.formatter.validate(value)
     if not result:
         raise InvalidSchemaValue(value, self.schema.type)