2018-02-15 14:28:15 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from openapi_core.shortcuts import create_spec
|
2020-02-21 16:33:45 +00:00
|
|
|
from openapi_core.templating.paths.exceptions import (
|
|
|
|
PathNotFound, OperationNotFound,
|
|
|
|
)
|
2019-10-19 12:01:56 +00:00
|
|
|
from openapi_core.testing import MockRequest
|
2020-03-02 16:05:36 +00:00
|
|
|
from openapi_core.validation.request.datatypes import RequestParameters
|
2018-04-17 12:38:23 +00:00
|
|
|
from openapi_core.validation.request.validators import RequestValidator
|
2018-02-15 14:28:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMinimal(object):
|
|
|
|
|
|
|
|
servers = [
|
|
|
|
"http://minimal.test/",
|
|
|
|
"https://bad.remote.domain.net/",
|
|
|
|
"http://localhost",
|
|
|
|
"http://localhost:8080",
|
|
|
|
"https://u:p@a.b:1337"
|
|
|
|
]
|
|
|
|
|
|
|
|
spec_paths = [
|
|
|
|
"data/v3.0/minimal_with_servers.yaml",
|
|
|
|
"data/v3.0/minimal.yaml"
|
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("server", servers)
|
|
|
|
@pytest.mark.parametrize("spec_path", spec_paths)
|
|
|
|
def test_hosts(self, factory, server, spec_path):
|
|
|
|
spec_dict = factory.spec_from_file(spec_path)
|
|
|
|
spec = create_spec(spec_dict)
|
|
|
|
validator = RequestValidator(spec)
|
|
|
|
request = MockRequest(server, "get", "/status")
|
|
|
|
|
|
|
|
result = validator.validate(request)
|
|
|
|
|
|
|
|
assert not result.errors
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("server", servers)
|
|
|
|
@pytest.mark.parametrize("spec_path", spec_paths)
|
|
|
|
def test_invalid_operation(self, factory, server, spec_path):
|
|
|
|
spec_dict = factory.spec_from_file(spec_path)
|
|
|
|
spec = create_spec(spec_dict)
|
|
|
|
validator = RequestValidator(spec)
|
2019-06-18 15:13:44 +00:00
|
|
|
request = MockRequest(server, "post", "/status")
|
2018-02-15 14:28:15 +00:00
|
|
|
|
|
|
|
result = validator.validate(request)
|
|
|
|
|
|
|
|
assert len(result.errors) == 1
|
2020-02-21 16:33:45 +00:00
|
|
|
assert isinstance(result.errors[0], OperationNotFound)
|
2018-02-15 14:28:15 +00:00
|
|
|
assert result.body is None
|
2020-03-02 16:05:36 +00:00
|
|
|
assert result.parameters == RequestParameters()
|
2019-06-18 15:13:44 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("server", servers)
|
|
|
|
@pytest.mark.parametrize("spec_path", spec_paths)
|
|
|
|
def test_invalid_path(self, factory, server, spec_path):
|
|
|
|
spec_dict = factory.spec_from_file(spec_path)
|
|
|
|
spec = create_spec(spec_dict)
|
|
|
|
validator = RequestValidator(spec)
|
|
|
|
request = MockRequest(server, "get", "/nonexistent")
|
|
|
|
|
|
|
|
result = validator.validate(request)
|
|
|
|
|
|
|
|
assert len(result.errors) == 1
|
2020-02-21 16:33:45 +00:00
|
|
|
assert isinstance(result.errors[0], PathNotFound)
|
2019-06-18 15:13:44 +00:00
|
|
|
assert result.body is None
|
2020-03-02 16:05:36 +00:00
|
|
|
assert result.parameters == RequestParameters()
|