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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
from base64 import b64encode
import pytest
import requests
import responses
from openapi_core import V30RequestUnmarshaller
from openapi_core import V30ResponseUnmarshaller
from openapi_core import V31RequestUnmarshaller
from openapi_core import V31ResponseUnmarshaller
from openapi_core import V31WebhookRequestUnmarshaller
from openapi_core import V31WebhookResponseUnmarshaller
from openapi_core.contrib.requests import RequestsOpenAPIRequest
from openapi_core.contrib.requests import RequestsOpenAPIResponse
from openapi_core.contrib.requests import RequestsOpenAPIWebhookRequest
class TestV31RequestsFactory:
@pytest.fixture
def schema_path(self, schema_path_factory):
specfile = "contrib/requests/data/v3.1/requests_factory.yaml"
return schema_path_factory.from_file(specfile)
@pytest.fixture
def request_unmarshaller(self, schema_path):
return V31RequestUnmarshaller(schema_path)
@pytest.fixture
def response_unmarshaller(self, schema_path):
return V31ResponseUnmarshaller(schema_path)
@pytest.fixture
def webhook_request_unmarshaller(self, schema_path):
return V31WebhookRequestUnmarshaller(schema_path)
@pytest.fixture
def webhook_response_unmarshaller(self, schema_path):
return V31WebhookResponseUnmarshaller(schema_path)
@responses.activate
def test_response_validator_path_pattern(self, response_unmarshaller):
responses.add(
responses.POST,
"http://localhost/browse/12/?q=string",
json={"data": "data"},
status=200,
headers={"X-Rate-Limit": "12"},
)
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
request_prepared = request.prepare()
session = requests.Session()
response = session.send(request_prepared)
openapi_request = RequestsOpenAPIRequest(request)
openapi_response = RequestsOpenAPIResponse(response)
result = response_unmarshaller.unmarshal(
openapi_request, openapi_response
)
assert not result.errors
def test_request_validator_path_pattern(self, request_unmarshaller):
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
openapi_request = RequestsOpenAPIRequest(request)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
def test_request_validator_prepared_request(self, request_unmarshaller):
request = requests.Request(
"POST",
"http://localhost/browse/12/",
params={"q": "string"},
headers={"content-type": "application/json"},
json={"param1": 1},
)
request_prepared = request.prepare()
openapi_request = RequestsOpenAPIRequest(request_prepared)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
def test_webhook_request_validator_path(
self, webhook_request_unmarshaller
):
request = requests.Request(
"POST",
"http://otherhost/callback/",
headers={
"content-type": "application/json",
"X-Rate-Limit": "12",
},
json={"id": 1},
)
openapi_webhook_request = RequestsOpenAPIWebhookRequest(
request, "resourceAdded"
)
result = webhook_request_unmarshaller.unmarshal(
openapi_webhook_request
)
assert not result.errors
@responses.activate
def test_webhook_response_validator_path(
self, webhook_response_unmarshaller
):
responses.add(
responses.POST,
"http://otherhost/callback/",
json={"data": "data"},
status=200,
)
request = requests.Request(
"POST",
"http://otherhost/callback/",
headers={
"content-type": "application/json",
"X-Rate-Limit": "12",
},
json={"id": 1},
)
request_prepared = request.prepare()
session = requests.Session()
response = session.send(request_prepared)
openapi_webhook_request = RequestsOpenAPIWebhookRequest(
request, "resourceAdded"
)
openapi_response = RequestsOpenAPIResponse(response)
result = webhook_response_unmarshaller.unmarshal(
openapi_webhook_request, openapi_response
)
assert not result.errors
class BaseTestPetstore:
api_key = "12345"
@property
def api_key_encoded(self):
api_key_bytes = self.api_key.encode("utf8")
api_key_bytes_enc = b64encode(api_key_bytes)
return str(api_key_bytes_enc, "utf8")
class TestPetstore(BaseTestPetstore):
@pytest.fixture
def schema_path(self, schema_path_factory):
specfile = "data/v3.0/petstore.yaml"
return schema_path_factory.from_file(specfile)
@pytest.fixture
def request_unmarshaller(self, schema_path):
return V30RequestUnmarshaller(schema_path)
@pytest.fixture
def response_unmarshaller(self, schema_path):
return V30ResponseUnmarshaller(schema_path)
@responses.activate
def test_response_binary_valid(self, response_unmarshaller, data_gif):
responses.add(
responses.GET,
"http://petstore.swagger.io/v1/pets/1/photo",
body=data_gif,
content_type="image/gif",
status=200,
)
headers = {
"Authorization": "Basic testuser",
"Api-Key": self.api_key_encoded,
}
request = requests.Request(
"GET",
"http://petstore.swagger.io/v1/pets/1/photo",
headers=headers,
)
request_prepared = request.prepare()
session = requests.Session()
response = session.send(request_prepared)
openapi_request = RequestsOpenAPIRequest(request)
openapi_response = RequestsOpenAPIResponse(response)
result = response_unmarshaller.unmarshal(
openapi_request, openapi_response
)
assert not result.errors
assert result.data == data_gif
@responses.activate
def test_request_binary_valid(self, request_unmarshaller, data_gif):
headers = {
"Authorization": "Basic testuser",
"Api-Key": self.api_key_encoded,
"Content-Type": "image/gif",
}
request = requests.Request(
"POST",
"http://petstore.swagger.io/v1/pets/1/photo",
headers=headers,
data=data_gif,
)
request_prepared = request.prepare()
openapi_request = RequestsOpenAPIRequest(request_prepared)
result = request_unmarshaller.unmarshal(openapi_request)
assert not result.errors
assert result.body == data_gif
|