1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
import pytest
from openapi_spec_validator import OpenAPIV2SpecValidator
from openapi_spec_validator import OpenAPIV30SpecValidator
from openapi_spec_validator import openapi_v2_spec_validator
from openapi_spec_validator import openapi_v30_spec_validator
from openapi_spec_validator import validate
from openapi_spec_validator import validate_spec
from openapi_spec_validator import validate_spec_url
from openapi_spec_validator import validate_url
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError
from openapi_spec_validator.validation.exceptions import ValidatorDetectError
class TestValidateSpec:
def test_spec_schema_version_not_detected(self):
spec = {}
with pytest.raises(ValidatorDetectError):
validate(spec)
class TestLocalValidateSpecUrl:
def test_spec_schema_version_not_detected(self, factory):
spec_path = "data/empty.yaml"
spec_url = factory.spec_file_url(spec_path)
with pytest.raises(ValidatorDetectError):
validate_url(spec_url)
class TestLiocalValidatev2Spec:
LOCAL_SOURCE_DIRECTORY = "data/v2.0/"
def local_test_suite_file_path(self, test_file):
return f"{self.LOCAL_SOURCE_DIRECTORY}{test_file}"
@pytest.mark.parametrize(
"spec_file",
[
"petstore.yaml",
],
)
def test_valid(self, factory, spec_file):
spec_path = self.local_test_suite_file_path(spec_file)
spec = factory.spec_from_file(spec_path)
validate(spec)
validate(spec, cls=OpenAPIV2SpecValidator)
with pytest.warns(DeprecationWarning):
validate_spec(spec, validator=openapi_v2_spec_validator)
@pytest.mark.parametrize(
"spec_file",
[
"empty.yaml",
],
)
def test_falied(self, factory, spec_file):
spec_path = self.local_test_suite_file_path(spec_file)
spec = factory.spec_from_file(spec_path)
with pytest.raises(OpenAPIValidationError):
validate(spec, cls=OpenAPIV2SpecValidator)
with pytest.warns(DeprecationWarning):
with pytest.raises(OpenAPIValidationError):
validate_spec(spec, validator=openapi_v2_spec_validator)
class TestLocalValidatev30Spec:
LOCAL_SOURCE_DIRECTORY = "data/v3.0/"
def local_test_suite_file_path(self, test_file):
return f"{self.LOCAL_SOURCE_DIRECTORY}{test_file}"
@pytest.mark.parametrize(
"spec_file",
[
"petstore.yaml",
],
)
def test_valid(self, factory, spec_file):
spec_path = self.local_test_suite_file_path(spec_file)
spec = factory.spec_from_file(spec_path)
spec_url = factory.spec_file_url(spec_path)
validate(spec)
with pytest.warns(DeprecationWarning):
validate_spec(spec, spec_url=spec_url)
validate(spec, cls=OpenAPIV30SpecValidator)
with pytest.warns(DeprecationWarning):
validate_spec(spec, validator=openapi_v30_spec_validator)
@pytest.mark.parametrize(
"spec_file",
[
"empty.yaml",
],
)
def test_falied(self, factory, spec_file):
spec_path = self.local_test_suite_file_path(spec_file)
spec = factory.spec_from_file(spec_path)
with pytest.raises(OpenAPIValidationError):
validate(spec, cls=OpenAPIV30SpecValidator)
with pytest.warns(DeprecationWarning):
with pytest.raises(OpenAPIValidationError):
validate_spec(spec, validator=openapi_v30_spec_validator)
@pytest.mark.network
class TestRemoteValidatev2SpecUrl:
REMOTE_SOURCE_URL = (
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/"
)
def remote_test_suite_file_path(self, test_file):
return f"{self.REMOTE_SOURCE_URL}{test_file}"
@pytest.mark.parametrize(
"spec_file",
[
"f25a1d44cff9669703257173e562376cc5bd0ec6/examples/v2.0/"
"yaml/petstore.yaml",
"f25a1d44cff9669703257173e562376cc5bd0ec6/examples/v2.0/"
"yaml/api-with-examples.yaml",
"f25a1d44cff9669703257173e562376cc5bd0ec6/examples/v2.0/"
"yaml/petstore-expanded.yaml",
],
)
def test_valid(self, spec_file):
spec_url = self.remote_test_suite_file_path(spec_file)
validate_url(spec_url)
validate_url(spec_url, cls=OpenAPIV2SpecValidator)
with pytest.warns(DeprecationWarning):
validate_spec_url(spec_url)
validate_spec_url(spec_url, validator=openapi_v2_spec_validator)
@pytest.mark.network
class TestRemoteValidatev30SpecUrl:
REMOTE_SOURCE_URL = (
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/"
)
def remote_test_suite_file_path(self, test_file):
return f"{self.REMOTE_SOURCE_URL}{test_file}"
@pytest.mark.parametrize(
"spec_file",
[
"f75f8486a1aae1a7ceef92fbc63692cb2556c0cd/examples/v3.0/"
"petstore.yaml",
"f75f8486a1aae1a7ceef92fbc63692cb2556c0cd/examples/v3.0/"
"api-with-examples.yaml",
"970566d5ca236a5ce1a02fb7d617fdbd07df88db/examples/v3.0/"
"api-with-examples.yaml",
],
)
def test_valid(self, spec_file):
spec_url = self.remote_test_suite_file_path(spec_file)
validate_url(spec_url)
validate_url(spec_url, cls=OpenAPIV30SpecValidator)
with pytest.warns(DeprecationWarning):
validate_spec_url(spec_url, validator=openapi_v30_spec_validator)
|