Files
connexion/tests/test_references.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

114 lines
3.0 KiB
Python

import mock
import pytest
from connexion.apis.flask_api import Jsonifier
from connexion.json_schema import RefResolutionError, resolve_refs
DEFINITIONS = {
'new_stack': {
'required': [
'image_version',
'keep_stacks',
'new_traffic',
'senza_yaml'
],
'type': 'object',
'properties': {
'keep_stacks': {
'type': 'integer',
'description': 'Number of older stacks to keep'
},
'image_version': {
'type': 'string',
'description': 'Docker image version to deploy'
},
'senza_yaml': {
'type': 'string',
'description': 'YAML to provide to senza'
},
'new_traffic': {
'type': 'integer',
'description': 'Percentage of the traffic'
}
}
},
'composed': {
'required': ['test'],
'type': 'object',
'properties': {
'test': {
'schema': {'$ref': '#/definitions/new_stack'}
}
}
},
'problem': {"some": "thing"}
}
PARAMETER_DEFINITIONS = {'myparam': {'in': 'path', 'type': 'integer'}}
@pytest.fixture
def api():
return mock.MagicMock(jsonifier=Jsonifier)
def test_non_existent_reference(api):
op_spec = {
'parameters': [{
'in': 'body',
'name': 'new_stack',
'required': True,
'schema': {'$ref': '#/definitions/new_stack'}
}]
}
with pytest.raises(RefResolutionError) as exc_info: # type: py.code.ExceptionInfo
resolve_refs(op_spec, {})
exception = exc_info.value
assert "definitions/new_stack" in str(exception)
def test_invalid_reference(api):
op_spec = {
'parameters': [{
'in': 'body',
'name': 'new_stack',
'required': True,
'schema': {'$ref': '#/notdefinitions/new_stack'}
}]
}
with pytest.raises(RefResolutionError) as exc_info: # type: py.code.ExceptionInfo
resolve_refs(op_spec, {
"definitions": DEFINITIONS,
"parameters": PARAMETER_DEFINITIONS
})
exception = exc_info.value
assert "notdefinitions/new_stack" in str(exception)
def test_resolve_invalid_reference(api):
op_spec = {
'operationId': 'fakeapi.hello.post_greeting',
'parameters': [{'$ref': '/parameters/fail'}]
}
with pytest.raises(RefResolutionError) as exc_info:
resolve_refs(op_spec, {
"parameters": PARAMETER_DEFINITIONS
})
exception = exc_info.value
assert "parameters/fail" in str(exception)
def test_resolve_web_reference(api):
op_spec = {
'parameters': [{'$ref': 'https://reallyfake.asd/parameters.json'}]
}
store = {
"https://reallyfake.asd/parameters.json": {"name": "test"}
}
spec = resolve_refs(op_spec, store=store)
assert spec["parameters"][0]["name"] == "test"