2017-10-18 13:42:10 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
from openapi_core.schema.parameters.exceptions import (
|
|
|
|
EmptyParameterValue, InvalidParameterValue,
|
|
|
|
)
|
2018-04-17 12:18:40 +00:00
|
|
|
from openapi_core.schema.parameters.enums import ParameterStyle
|
|
|
|
from openapi_core.schema.parameters.models import Parameter
|
2019-09-03 00:38:19 +00:00
|
|
|
from openapi_core.schema.schemas.models import Schema
|
2020-01-28 15:40:45 +00:00
|
|
|
from openapi_core.unmarshalling.schemas.formatters import Formatter
|
2017-10-18 13:42:10 +00:00
|
|
|
|
|
|
|
|
2017-11-14 11:28:45 +00:00
|
|
|
class TestParameterInit(object):
|
|
|
|
|
|
|
|
def test_path(self):
|
|
|
|
param = Parameter('param', 'path')
|
|
|
|
|
|
|
|
assert param.allow_empty_value is False
|
|
|
|
assert param.style == ParameterStyle.SIMPLE
|
|
|
|
assert param.explode is False
|
|
|
|
|
|
|
|
def test_query(self):
|
|
|
|
param = Parameter('param', 'query')
|
|
|
|
|
|
|
|
assert param.allow_empty_value is False
|
|
|
|
assert param.style == ParameterStyle.FORM
|
|
|
|
assert param.explode is True
|
|
|
|
|
|
|
|
def test_header(self):
|
|
|
|
param = Parameter('param', 'header')
|
|
|
|
|
|
|
|
assert param.allow_empty_value is False
|
|
|
|
assert param.style == ParameterStyle.SIMPLE
|
|
|
|
assert param.explode is False
|
|
|
|
|
|
|
|
def test_cookie(self):
|
|
|
|
param = Parameter('param', 'cookie')
|
|
|
|
|
|
|
|
assert param.allow_empty_value is False
|
|
|
|
assert param.style == ParameterStyle.FORM
|
|
|
|
assert param.explode is True
|
|
|
|
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
class TestParameterCast(object):
|
2017-10-18 13:42:10 +00:00
|
|
|
|
|
|
|
def test_deprecated(self):
|
|
|
|
param = Parameter('param', 'query', deprecated=True)
|
|
|
|
value = 'test'
|
|
|
|
|
|
|
|
with pytest.warns(DeprecationWarning):
|
2019-09-03 00:38:19 +00:00
|
|
|
result = param.cast(value)
|
2017-10-18 13:42:10 +00:00
|
|
|
|
|
|
|
assert result == value
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
def test_query_empty(self):
|
|
|
|
param = Parameter('param', 'query')
|
|
|
|
value = ''
|
|
|
|
|
|
|
|
with pytest.raises(EmptyParameterValue):
|
|
|
|
param.cast(value)
|
|
|
|
|
2017-10-18 13:42:10 +00:00
|
|
|
def test_query_valid(self):
|
|
|
|
param = Parameter('param', 'query')
|
|
|
|
value = 'test'
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
result = param.cast(value)
|
2017-10-18 13:42:10 +00:00
|
|
|
|
|
|
|
assert result == value
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
|
|
|
|
class TestParameterUnmarshal(object):
|
|
|
|
|
|
|
|
def test_query_valid(self):
|
2017-10-18 13:42:10 +00:00
|
|
|
param = Parameter('param', 'query')
|
2019-09-03 00:38:19 +00:00
|
|
|
value = 'test'
|
2017-10-18 13:42:10 +00:00
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
result = param.unmarshal(value)
|
|
|
|
|
|
|
|
assert result == value
|
2017-10-18 13:42:10 +00:00
|
|
|
|
|
|
|
def test_query_allow_empty_value(self):
|
|
|
|
param = Parameter('param', 'query', allow_empty_value=True)
|
|
|
|
value = ''
|
|
|
|
|
|
|
|
result = param.unmarshal(value)
|
|
|
|
|
|
|
|
assert result == value
|
2019-09-03 00:38:19 +00:00
|
|
|
|
|
|
|
def test_query_schema_type_invalid(self):
|
|
|
|
schema = Schema('integer', _source={'type': 'integer'})
|
|
|
|
param = Parameter('param', 'query', schema=schema)
|
|
|
|
value = 'test'
|
|
|
|
|
|
|
|
with pytest.raises(InvalidParameterValue):
|
|
|
|
param.unmarshal(value)
|
|
|
|
|
|
|
|
def test_query_schema_custom_format_invalid(self):
|
2020-01-28 15:40:45 +00:00
|
|
|
|
|
|
|
class CustomFormatter(Formatter):
|
|
|
|
def unmarshal(self, value):
|
|
|
|
raise ValueError
|
|
|
|
formatter = CustomFormatter()
|
|
|
|
custom_format = 'custom'
|
|
|
|
custom_formatters = {
|
|
|
|
custom_format: formatter,
|
|
|
|
}
|
2019-09-03 00:38:19 +00:00
|
|
|
schema = Schema(
|
|
|
|
'string',
|
2020-01-28 15:40:45 +00:00
|
|
|
schema_format=custom_format,
|
2019-09-03 00:38:19 +00:00
|
|
|
_source={'type': 'string', 'format': 'custom'},
|
|
|
|
)
|
|
|
|
param = Parameter('param', 'query', schema=schema)
|
|
|
|
value = 'test'
|
|
|
|
|
|
|
|
with pytest.raises(InvalidParameterValue):
|
|
|
|
param.unmarshal(value, custom_formatters=custom_formatters)
|