|
| 1 | +import pytest |
| 2 | +from base64 import b64encode |
| 3 | +from six import iteritems, text_type |
| 4 | + |
| 5 | +from openapi_core.schema.media_types.models import MediaType |
| 6 | +from openapi_core.schema.operations.models import Operation |
| 7 | +from openapi_core.schema.parameters.models import Parameter |
| 8 | +from openapi_core.schema.paths.models import Path |
| 9 | +from openapi_core.schema.request_bodies.models import RequestBody |
| 10 | +from openapi_core.schema.responses.models import Response |
| 11 | +from openapi_core.schema.schemas.models import Schema |
| 12 | +from openapi_core.schema.servers.models import Server, ServerVariable |
| 13 | +from openapi_core.shortcuts import create_spec |
| 14 | +from openapi_core.validation.request.validators import RequestValidator |
| 15 | +from openapi_core.validation.response.validators import ResponseValidator |
| 16 | + |
| 17 | + |
| 18 | +class TestPetstore(object): |
| 19 | + |
| 20 | + api_key = '12345' |
| 21 | + |
| 22 | + @property |
| 23 | + def api_key_encoded(self): |
| 24 | + api_key_bytes = self.api_key.encode('utf8') |
| 25 | + api_key_bytes_enc = b64encode(api_key_bytes) |
| 26 | + return text_type(api_key_bytes_enc, 'utf8') |
| 27 | + |
| 28 | + @pytest.fixture |
| 29 | + def spec_uri(self): |
| 30 | + return "file://tests/integration/data/v3.0/petstore.yaml" |
| 31 | + |
| 32 | + @pytest.fixture |
| 33 | + def spec_dict(self, factory): |
| 34 | + return factory.spec_from_file("data/v3.0/petstore.yaml") |
| 35 | + |
| 36 | + @pytest.fixture |
| 37 | + def spec(self, spec_dict, spec_uri): |
| 38 | + return create_spec(spec_dict, spec_uri) |
| 39 | + |
| 40 | + @pytest.fixture |
| 41 | + def request_validator(self, spec): |
| 42 | + return RequestValidator(spec) |
| 43 | + |
| 44 | + @pytest.fixture |
| 45 | + def response_validator(self, spec): |
| 46 | + return ResponseValidator(spec) |
| 47 | + |
| 48 | + def test_spec(self, spec, spec_dict): |
| 49 | + url = 'http://petstore.swagger.io/v1' |
| 50 | + assert spec.info.title == spec_dict['info']['title'] |
| 51 | + assert spec.info.version == spec_dict['info']['version'] |
| 52 | + |
| 53 | + assert spec.get_server_url() == url |
| 54 | + |
| 55 | + for idx, server in enumerate(spec.servers): |
| 56 | + assert type(server) == Server |
| 57 | + |
| 58 | + server_spec = spec_dict['servers'][idx] |
| 59 | + assert server.url == server_spec['url'] |
| 60 | + assert server.default_url == url |
| 61 | + |
| 62 | + for variable_name, variable in iteritems(server.variables): |
| 63 | + assert type(variable) == ServerVariable |
| 64 | + assert variable.name == variable_name |
| 65 | + |
| 66 | + variable_spec = server_spec['variables'][variable_name] |
| 67 | + assert variable.default == variable_spec['default'] |
| 68 | + assert variable.enum == variable_spec.get('enum') |
| 69 | + |
| 70 | + for path_name, path in iteritems(spec.paths): |
| 71 | + assert type(path) == Path |
| 72 | + assert path.name == path_name |
| 73 | + |
| 74 | + for http_method, operation in iteritems(path.operations): |
| 75 | + operation_spec = spec_dict['paths'][path_name][http_method] |
| 76 | + |
| 77 | + assert type(operation) == Operation |
| 78 | + assert operation.path_name == path_name |
| 79 | + assert operation.http_method == http_method |
| 80 | + assert operation.operation_id is not None |
| 81 | + assert operation.tags == operation_spec['tags'] |
| 82 | + |
| 83 | + responses_spec = operation_spec.get('responses') |
| 84 | + |
| 85 | + for http_status, response in iteritems(operation.responses): |
| 86 | + assert type(response) == Response |
| 87 | + assert response.http_status == http_status |
| 88 | + |
| 89 | + response_spec = responses_spec[http_status] |
| 90 | + |
| 91 | + if not response_spec: |
| 92 | + continue |
| 93 | + |
| 94 | + # @todo: test with defererence |
| 95 | + if '$ref' in response_spec: |
| 96 | + continue |
| 97 | + |
| 98 | + description_spec = response_spec['description'] |
| 99 | + |
| 100 | + assert response.description == description_spec |
| 101 | + |
| 102 | + for mimetype, media_type in iteritems(response.content): |
| 103 | + assert type(media_type) == MediaType |
| 104 | + assert media_type.mimetype == mimetype |
| 105 | + |
| 106 | + content_spec = response_spec['content'][mimetype] |
| 107 | + |
| 108 | + example_spec = content_spec.get('example') |
| 109 | + assert media_type.example == example_spec |
| 110 | + |
| 111 | + schema_spec = content_spec.get('schema') |
| 112 | + assert bool(schema_spec) == bool(media_type.schema) |
| 113 | + |
| 114 | + if not schema_spec: |
| 115 | + continue |
| 116 | + |
| 117 | + # @todo: test with defererence |
| 118 | + if '$ref' in schema_spec: |
| 119 | + continue |
| 120 | + |
| 121 | + assert type(media_type.schema) == Schema |
| 122 | + assert media_type.schema.type.value ==\ |
| 123 | + schema_spec['type'] |
| 124 | + assert media_type.schema.required == schema_spec.get( |
| 125 | + 'required', []) |
| 126 | + |
| 127 | + for parameter_name, parameter in iteritems( |
| 128 | + response.headers): |
| 129 | + assert type(parameter) == Parameter |
| 130 | + assert parameter.name == parameter_name |
| 131 | + |
| 132 | + headers_spec = response_spec['headers'] |
| 133 | + parameter_spec = headers_spec[parameter_name] |
| 134 | + schema_spec = parameter_spec.get('schema') |
| 135 | + assert bool(schema_spec) == bool(parameter.schema) |
| 136 | + |
| 137 | + if not schema_spec: |
| 138 | + continue |
| 139 | + |
| 140 | + # @todo: test with defererence |
| 141 | + if '$ref' in schema_spec: |
| 142 | + continue |
| 143 | + |
| 144 | + assert type(parameter.schema) == Schema |
| 145 | + assert parameter.schema.type.value ==\ |
| 146 | + schema_spec['type'] |
| 147 | + assert parameter.schema.format ==\ |
| 148 | + schema_spec.get('format') |
| 149 | + assert parameter.schema.required == schema_spec.get( |
| 150 | + 'required', []) |
| 151 | + |
| 152 | + request_body_spec = operation_spec.get('requestBody') |
| 153 | + |
| 154 | + assert bool(request_body_spec) == bool(operation.request_body) |
| 155 | + |
| 156 | + if not request_body_spec: |
| 157 | + continue |
| 158 | + |
| 159 | + assert type(operation.request_body) == RequestBody |
| 160 | + assert bool(operation.request_body.required) ==\ |
| 161 | + request_body_spec.get('required', False) |
| 162 | + |
| 163 | + for mimetype, media_type in iteritems( |
| 164 | + operation.request_body.content): |
| 165 | + assert type(media_type) == MediaType |
| 166 | + assert media_type.mimetype == mimetype |
| 167 | + |
| 168 | + content_spec = request_body_spec['content'][mimetype] |
| 169 | + schema_spec = content_spec.get('schema') |
| 170 | + assert bool(schema_spec) == bool(media_type.schema) |
| 171 | + |
| 172 | + if not schema_spec: |
| 173 | + continue |
| 174 | + |
| 175 | + # @todo: test with defererence |
| 176 | + if '$ref' in schema_spec: |
| 177 | + continue |
| 178 | + |
| 179 | + assert type(media_type.schema) == Schema |
| 180 | + assert media_type.schema.type.value ==\ |
| 181 | + schema_spec['type'] |
| 182 | + assert media_type.schema.format ==\ |
| 183 | + schema_spec.get('format') |
| 184 | + assert media_type.schema.required == schema_spec.get( |
| 185 | + 'required', False) |
| 186 | + |
| 187 | + if not spec.components: |
| 188 | + return |
| 189 | + |
| 190 | + for _, schema in iteritems(spec.components.schemas): |
| 191 | + assert type(schema) == Schema |
0 commit comments