Files
connexion/tests/decorators/test_validation.py
João Santos 44ea9336fe Connexion 2.0 (#619)
- App and Api options must be provided through the "options" argument (``old_style_options`` have been removed).
- You must specify a form content-type in 'consumes' in order to consume form data.
- The `Operation` interface has been formalized in the `AbstractOperation` class.
- The `Operation` class has been renamed to `Swagger2Operation`.
- Array parameter deserialization now follows the Swagger 2.0 spec more closely.
  In situations when a query parameter is passed multiple times, and the collectionFormat is either csv or pipes, the right-most value will be used.
  For example, `?q=1,2,3&q=4,5,6` will result in `q = [4, 5, 6]`.
  The old behavior is available by setting the collectionFormat to `multi`, or by importing `decorators.uri_parsing.AlwaysMultiURIParser` and passing `parser_class=AlwaysMultiURIParser` to your Api.
- The spec validator library has changed from `swagger-spec-validator` to `openapi-spec-validator`.
- Errors that previously raised `SwaggerValidationError` now raise the `InvalidSpecification` exception.
  All spec validation errors should be wrapped with `InvalidSpecification`.
- Support for nullable/x-nullable, readOnly and writeOnly/x-writeOnly has been added to the standard json schema validator.
- Custom validators can now be specified on api level (instead of app level).
- Added support for basic authentication and apikey authentication
- If unsupported security requirements are defined or ``x-tokenInfoFunc``/``x-tokenInfoUrl`` is missing, connexion now denies requests instead of allowing access without security-check.
- Accessing ``connexion.request.user`` / ``flask.request.user`` is no longer supported, use ``connexion.context['user']`` instead
2018-11-05 14:50:42 +01:00

144 lines
4.3 KiB
Python

from jsonschema import ValidationError
import pytest
from connexion.decorators.validation import ParameterValidator
from connexion.json_schema import (Draft4RequestValidator,
Draft4ResponseValidator)
from mock import MagicMock
def test_get_valid_parameter():
result = ParameterValidator.validate_parameter('formdata', 20, {'type': 'number', 'name': 'foobar'})
assert result is None
def test_get_valid_parameter_with_required_attr():
param = {'type': 'number', 'required': True, 'name': 'foobar'}
result = ParameterValidator.validate_parameter('formdata', 20, param)
assert result is None
def test_get_missing_required_parameter():
param = {'type': 'number', 'required': True, 'name': 'foo'}
result = ParameterValidator.validate_parameter('formdata', None, param)
assert result == "Missing formdata parameter 'foo'"
def test_get_nullable_parameter():
param = {'type': 'number', 'required': True, 'name': 'foo', 'x-nullable': True}
result = ParameterValidator.validate_parameter('formdata', 'None', param)
assert result is None
def test_invalid_type(monkeypatch):
logger = MagicMock()
monkeypatch.setattr('connexion.decorators.validation.logger', logger)
result = ParameterValidator.validate_parameter('formdata', 20, {'type': 'string', 'name': 'foo'})
expected_result = """20 is not of type 'string'
Failed validating 'type' in schema:
{'name': 'foo', 'type': 'string'}
On instance:
20"""
assert result == expected_result
logger.info.assert_called_once()
def test_invalid_type_value_error(monkeypatch):
logger = MagicMock()
monkeypatch.setattr('connexion.decorators.validation.logger', logger)
value = {'test': 1, 'second': 2}
result = ParameterValidator.validate_parameter('formdata', value, {'type': 'boolean', 'name': 'foo'})
assert result == "Wrong type, expected 'boolean' for formdata parameter 'foo'"
def test_support_nullable_properties():
schema = {
"type": "object",
"properties": {"foo": {"type": "string", "x-nullable": True}},
}
try:
Draft4RequestValidator(schema).validate({"foo": None})
except ValidationError:
pytest.fail("Shouldn't raise ValidationError")
def test_support_nullable_properties_raises_validation_error():
schema = {
"type": "object",
"properties": {"foo": {"type": "string", "x-nullable": False}},
}
with pytest.raises(ValidationError):
Draft4RequestValidator(schema).validate({"foo": None})
def test_support_nullable_properties_not_iterable():
schema = {
"type": "object",
"properties": {"foo": {"type": "string", "x-nullable": True}},
}
with pytest.raises(ValidationError):
Draft4RequestValidator(schema).validate(12345)
def test_nullable_enum():
schema = {
"enum": ["foo", 7],
"nullable": True
}
try:
Draft4RequestValidator(schema).validate(None)
except ValidationError:
pytest.fail("Shouldn't raise ValidationError")
def test_nullable_enum_error():
schema = {
"enum": ["foo", 7]
}
with pytest.raises(ValidationError):
Draft4RequestValidator(schema).validate(None)
def test_writeonly_value():
schema = {
"type": "object",
"properties": {"foo": {"type": "string", "writeOnly": True}},
}
try:
Draft4RequestValidator(schema).validate({"foo": "bar"})
except ValidationError:
pytest.fail("Shouldn't raise ValidationError")
def test_writeonly_value_error():
schema = {
"type": "object",
"properties": {"foo": {"type": "string", "writeOnly": True}},
}
with pytest.raises(ValidationError):
Draft4ResponseValidator(schema).validate({"foo": "bar"})
def test_writeonly_required():
schema = {
"type": "object",
"required": ["foo"],
"properties": {"foo": {"type": "string", "writeOnly": True}},
}
try:
Draft4RequestValidator(schema).validate({"foo": "bar"})
except ValidationError:
pytest.fail("Shouldn't raise ValidationError")
def test_writeonly_required_error():
schema = {
"type": "object",
"required": ["foo"],
"properties": {"foo": {"type": "string", "writeOnly": True}},
}
with pytest.raises(ValidationError):
Draft4RequestValidator(schema).validate({"bar": "baz"})