-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
models.py
498 lines (408 loc) · 18.6 KB
/
models.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# pylint: disable=too-many-instance-attributes
import base64
import datetime
import http
from collections import Counter
from contextlib import contextmanager
from enum import IntEnum
from logging import LogRecord
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, Iterator, List, Optional, Tuple, Union, cast
from urllib.parse import urljoin
import attr
import requests
import werkzeug
from hypothesis.strategies import SearchStrategy
from .checks import ALL_CHECKS
from .exceptions import InvalidSchema
from .types import Body, Cookies, FormData, Headers, PathParameters, Query
from .utils import GenericResponse, WSGIResponse
if TYPE_CHECKING:
from .schemas import BaseSchema
from .hooks import HookDispatcher
@attr.s(slots=True) # pragma: no mutate
class Case:
"""A single test case parameters."""
endpoint: "Endpoint" = attr.ib() # pragma: no mutate
path_parameters: Optional[PathParameters] = attr.ib(default=None) # pragma: no mutate
headers: Optional[Headers] = attr.ib(default=None) # pragma: no mutate
cookies: Optional[Cookies] = attr.ib(default=None) # pragma: no mutate
query: Optional[Query] = attr.ib(default=None) # pragma: no mutate
body: Optional[Body] = attr.ib(default=None) # pragma: no mutate
form_data: Optional[FormData] = attr.ib(default=None) # pragma: no mutate
@property
def path(self) -> str:
return self.endpoint.path
@property
def method(self) -> str:
return self.endpoint.method
@property
def base_url(self) -> Optional[str]:
return self.endpoint.base_url
@property
def app(self) -> Any:
return self.endpoint.app
@property
def formatted_path(self) -> str:
# pylint: disable=not-a-mapping
try:
return self.path.format(**self.path_parameters or {})
except KeyError:
raise InvalidSchema("Missing required property `required: true`")
def get_code_to_reproduce(self, headers: Optional[Dict[str, Any]] = None) -> str:
"""Construct a Python code to reproduce this case with `requests`."""
base_url = self.base_url or "http://localhost"
kwargs = self.as_requests_kwargs(base_url)
if headers:
final_headers = kwargs["headers"] or {}
final_headers.update(headers)
kwargs["headers"] = final_headers
method = kwargs["method"].lower()
def are_defaults(key: str, value: Optional[Dict]) -> bool:
default_value: Optional[Dict] = {"json": None}.get(key, None)
return value == default_value
printed_kwargs = ", ".join(
f"{key}={repr(value)}"
for key, value in kwargs.items()
if key not in ("method", "url") and not are_defaults(key, value)
)
args_repr = f"'{kwargs['url']}'"
if printed_kwargs:
args_repr += f", {printed_kwargs}"
return f"requests.{method}({args_repr})"
def _get_base_url(self, base_url: Optional[str] = None) -> str:
if base_url is None:
if self.base_url is not None:
base_url = self.base_url
else:
raise ValueError(
"Base URL is required as `base_url` argument in `call` or should be specified "
"in the schema constructor as a part of Schema URL."
)
return base_url
def as_requests_kwargs(self, base_url: Optional[str] = None) -> Dict[str, Any]:
"""Convert the case into a dictionary acceptable by requests."""
base_url = self._get_base_url(base_url)
formatted_path = self.formatted_path.lstrip("/") # pragma: no mutate
url = urljoin(base_url + "/", formatted_path)
# Form data and body are mutually exclusive
extra: Dict[str, Optional[Body]]
if self.form_data:
extra = {"files": self.form_data}
elif is_multipart(self.body):
extra = {"data": self.body}
else:
extra = {"json": self.body}
return {
"method": self.method,
"url": url,
"cookies": self.cookies,
"headers": self.headers,
"params": self.query,
**extra,
}
def call(
self,
base_url: Optional[str] = None,
session: Optional[requests.Session] = None,
headers: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> requests.Response:
"""Make a network call with `requests`."""
if session is None:
session = requests.Session()
close_session = True
else:
close_session = False
base_url = self._get_base_url(base_url)
data = self.as_requests_kwargs(base_url)
if headers is not None:
data["headers"] = {**(data["headers"] or {}), **headers}
data.update(kwargs)
response = session.request(**data) # type: ignore
if close_session:
session.close()
return response
def as_werkzeug_kwargs(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
"""Convert the case into a dictionary acceptable by werkzeug.Client."""
final_headers = self.headers.copy() if self.headers is not None else {}
if headers:
final_headers.update(headers)
extra: Dict[str, Optional[Body]]
if self.form_data:
extra = {"data": self.form_data}
final_headers = final_headers or {}
final_headers.setdefault("Content-Type", "multipart/form-data")
elif is_multipart(self.body):
extra = {"data": self.body}
else:
extra = {"json": self.body}
return {
"method": self.method,
"path": self.formatted_path,
"headers": final_headers,
"query_string": self.query,
**extra,
}
def call_wsgi(self, app: Any = None, headers: Optional[Dict[str, str]] = None, **kwargs: Any) -> WSGIResponse:
application = app or self.app
if application is None:
raise RuntimeError(
"WSGI application instance is required. "
"Please, set `app` argument in the schema constructor or pass it to `call_wsgi`"
)
data = self.as_werkzeug_kwargs(headers)
client = werkzeug.Client(application, WSGIResponse)
with cookie_handler(client, self.cookies):
return client.open(**data, **kwargs)
def validate_response(
self,
response: Union[requests.Response, WSGIResponse],
checks: Tuple[Callable[[Union[requests.Response, WSGIResponse], "Case"], None], ...] = ALL_CHECKS,
) -> None:
errors = []
for check in checks:
try:
check(response, self)
except AssertionError as exc:
errors.append(exc.args[0])
if errors:
raise AssertionError(*errors)
def is_multipart(item: Optional[Body]) -> bool:
"""A poor detection if the body should be a multipart request.
It traverses the structure and if it contains bytes in any value, then it is a multipart request, because
it may happen only if there was `format: binary`, which usually is in multipart payloads.
Probably a better way would be checking actual content types defined in `requestBody` and drive behavior based on
that fact.
"""
if isinstance(item, bytes):
return True
if isinstance(item, dict):
for value in item.values():
if is_multipart(value):
return True
if isinstance(item, list):
for value in item:
if is_multipart(value):
return True
return False
@contextmanager
def cookie_handler(client: werkzeug.Client, cookies: Optional[Cookies]) -> Generator[None, None, None]:
"""Set cookies required for a call."""
if not cookies:
yield
else:
for key, value in cookies.items():
client.set_cookie("localhost", key, value)
yield
for key in cookies:
client.delete_cookie("localhost", key)
def empty_object() -> Dict[str, Any]:
return {"properties": {}, "additionalProperties": False, "type": "object", "required": []}
@attr.s(slots=True) # pragma: no mutate
class EndpointDefinition:
"""A wrapper to store not resolved endpoint definitions.
To prevent recursion errors we need to store definitions without resolving references. But endpoint definitions
itself can be behind a reference (when there is a ``$ref`` in ``paths`` values), therefore we need to store this
scope change to have a proper reference resolving later.
"""
raw: Dict[str, Any] = attr.ib() # pragma: no mutate
scope: str = attr.ib() # pragma: no mutate
@attr.s(slots=True) # pragma: no mutate
class Endpoint:
"""A container that could be used for test cases generation."""
path: str = attr.ib() # pragma: no mutate
method: str = attr.ib() # pragma: no mutate
definition: EndpointDefinition = attr.ib() # pragma: no mutate
schema: "BaseSchema" = attr.ib() # pragma: no mutate
app: Any = attr.ib(default=None) # pragma: no mutate
base_url: Optional[str] = attr.ib(default=None) # pragma: no mutate
path_parameters: Optional[PathParameters] = attr.ib(default=None) # pragma: no mutate
headers: Optional[Headers] = attr.ib(default=None) # pragma: no mutate
cookies: Optional[Cookies] = attr.ib(default=None) # pragma: no mutate
query: Optional[Query] = attr.ib(default=None) # pragma: no mutate
body: Optional[Body] = attr.ib(default=None) # pragma: no mutate
form_data: Optional[FormData] = attr.ib(default=None) # pragma: no mutate
def as_strategy(self, hooks: Optional["HookDispatcher"] = None) -> SearchStrategy:
from ._hypothesis import get_case_strategy # pylint: disable=import-outside-toplevel
return get_case_strategy(self, hooks)
class Status(IntEnum):
"""Status of an action or multiple actions."""
success = 1 # pragma: no mutate
failure = 2 # pragma: no mutate
error = 3 # pragma: no mutate
@attr.s(slots=True, repr=False) # pragma: no mutate
class Check:
"""Single check run result."""
name: str = attr.ib() # pragma: no mutate
value: Status = attr.ib() # pragma: no mutate
example: Optional[Case] = attr.ib(default=None) # pragma: no mutate
message: Optional[str] = attr.ib(default=None) # pragma: no mutate
@attr.s(slots=True, repr=False) # pragma: no mutate
class Request:
"""Request data extracted from `Case`."""
method: str = attr.ib() # pragma: no mutate
uri: str = attr.ib() # pragma: no mutate
body: str = attr.ib() # pragma: no mutate
headers: Headers = attr.ib() # pragma: no mutate
@classmethod
def from_case(cls, case: Case, session: requests.Session) -> "Request":
"""Create a new `Request` instance from `Case`."""
base_url = case.base_url or "http://localhost"
kwargs = case.as_requests_kwargs(base_url)
request = requests.Request(**kwargs)
prepared = session.prepare_request(request) # type: ignore
return cls.from_prepared_request(prepared)
@classmethod
def from_prepared_request(cls, prepared: requests.PreparedRequest) -> "Request":
"""A prepared request version is already stored in `requests.Response`."""
body = prepared.body or b""
if isinstance(body, str):
# can be a string for `application/x-www-form-urlencoded`
body = body.encode("utf-8")
# these values have `str` type at this point
uri = cast(str, prepared.url)
method = cast(str, prepared.method)
return cls(
uri=uri,
method=method,
headers={key: [value] for (key, value) in prepared.headers.items()},
body=base64.b64encode(body).decode(),
)
def serialize_payload(payload: bytes) -> str:
return base64.b64encode(payload).decode()
@attr.s(slots=True, repr=False) # pragma: no mutate
class Response:
"""Unified response data."""
status_code: int = attr.ib() # pragma: no mutate
message: str = attr.ib() # pragma: no mutate
headers: Dict[str, List[str]] = attr.ib() # pragma: no mutate
body: str = attr.ib() # pragma: no mutate
encoding: str = attr.ib() # pragma: no mutate
http_version: str = attr.ib() # pragma: no mutate
elapsed: float = attr.ib() # pragma: no mutate
@classmethod
def from_requests(cls, response: requests.Response) -> "Response":
"""Create a response from requests.Response."""
headers = {name: response.raw.headers.getlist(name) for name in response.raw.headers.keys()}
# Similar to http.client:319 (HTTP version detection in stdlib's `http` package)
http_version = "1.0" if response.raw.version == 10 else "1.1"
return cls(
status_code=response.status_code,
message=response.reason,
body=serialize_payload(response.content),
encoding=response.encoding or "utf8",
headers=headers,
http_version=http_version,
elapsed=response.elapsed.total_seconds(),
)
@classmethod
def from_wsgi(cls, response: WSGIResponse, elapsed: float) -> "Response":
"""Create a response from WSGI response."""
message = http.client.responses.get(response.status_code, "UNKNOWN")
headers = {name: response.headers.getlist(name) for name in response.headers.keys()}
return cls(
status_code=response.status_code,
message=message,
body=serialize_payload(response.data),
encoding=response.content_encoding or "utf-8",
headers=headers,
http_version="1.1",
elapsed=elapsed,
)
@attr.s(slots=True) # pragma: no mutate
class Interaction:
"""A single interaction with the target app."""
request: Request = attr.ib() # pragma: no mutate
response: Response = attr.ib() # pragma: no mutate
recorded_at: str = attr.ib(factory=lambda: datetime.datetime.now().isoformat()) # pragma: no mutate
@classmethod
def from_requests(cls, response: requests.Response) -> "Interaction":
return cls(request=Request.from_prepared_request(response.request), response=Response.from_requests(response))
@classmethod
def from_wsgi(cls, case: Case, response: WSGIResponse, headers: Dict[str, Any], elapsed: float) -> "Interaction":
session = requests.Session()
session.headers.update(headers)
return cls(request=Request.from_case(case, session), response=Response.from_wsgi(response, elapsed))
@attr.s(slots=True, repr=False) # pragma: no mutate
class TestResult:
"""Result of a single test."""
endpoint: Endpoint = attr.ib() # pragma: no mutate
checks: List[Check] = attr.ib(factory=list) # pragma: no mutate
errors: List[Tuple[Exception, Optional[Case]]] = attr.ib(factory=list) # pragma: no mutate
interactions: List[Interaction] = attr.ib(factory=list) # pragma: no mutate
logs: List[LogRecord] = attr.ib(factory=list) # pragma: no mutate
is_errored: bool = attr.ib(default=False) # pragma: no mutate
seed: Optional[int] = attr.ib(default=None) # pragma: no mutate
# To show a proper reproduction code if a failure happens
overridden_headers: Optional[Dict[str, Any]] = attr.ib(default=None) # pragma: no mutate
def mark_errored(self) -> None:
self.is_errored = True
@property
def has_errors(self) -> bool:
return bool(self.errors)
@property
def has_failures(self) -> bool:
return any(check.value == Status.failure for check in self.checks)
@property
def has_logs(self) -> bool:
return bool(self.logs)
def add_success(self, name: str, example: Case) -> None:
self.checks.append(Check(name, Status.success, example))
def add_failure(self, name: str, example: Case, message: str) -> None:
self.checks.append(Check(name, Status.failure, example, message))
def add_error(self, exception: Exception, example: Optional[Case] = None) -> None:
self.errors.append((exception, example))
def store_requests_response(self, response: requests.Response) -> None:
self.interactions.append(Interaction.from_requests(response))
def store_wsgi_response(self, case: Case, response: WSGIResponse, headers: Dict[str, Any], elapsed: float) -> None:
self.interactions.append(Interaction.from_wsgi(case, response, headers, elapsed))
@attr.s(slots=True, repr=False) # pragma: no mutate
class TestResultSet:
"""Set of multiple test results."""
results: List[TestResult] = attr.ib(factory=list) # pragma: no mutate
def __iter__(self) -> Iterator[TestResult]:
return iter(self.results)
@property
def is_empty(self) -> bool:
"""If the result set contains no results."""
return len(self.results) == 0
@property
def has_failures(self) -> bool:
"""If any result has any failures."""
return any(result.has_failures for result in self)
@property
def has_errors(self) -> bool:
"""If any result has any errors."""
return any(result.has_errors for result in self)
@property
def has_logs(self) -> bool:
"""If any result has any captured logs."""
return any(result.has_logs for result in self)
def _count(self, predicate: Callable) -> int:
return sum(1 for result in self if predicate(result))
@property
def passed_count(self) -> int:
return self._count(lambda result: not result.has_errors and not result.has_failures)
@property
def failed_count(self) -> int:
return self._count(lambda result: result.has_failures and not result.is_errored)
@property
def errored_count(self) -> int:
return self._count(lambda result: result.has_errors or result.is_errored)
@property
def total(self) -> Dict[str, Dict[Union[str, Status], int]]:
"""Aggregated statistic about test results."""
output: Dict[str, Dict[Union[str, Status], int]] = {}
for item in self.results:
for check in item.checks:
output.setdefault(check.name, Counter())
output[check.name][check.value] += 1
output[check.name]["total"] += 1
# Avoid using Counter, since its behavior could harm in other places:
# `if not total["unknown"]:` - this will lead to the branch execution
# It is better to let it fail if there is a wrong key
return {key: dict(value) for key, value in output.items()}
def append(self, item: TestResult) -> None:
"""Add a new item to the results list."""
self.results.append(item)
CheckFunction = Callable[[GenericResponse, Case], None] # pragma: no mutate