forked from olipratt/swagger-conformance
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__main__.py
103 lines (86 loc) · 2.76 KB
/
__main__.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
import argparse
import sys
from os import environ
from colorama import init, Fore, Style, Back
from swaggercheck import api_conformance_test
init()
def main():
parser = argparse.ArgumentParser(
description="Basic Swagger-defined API conformance test."
)
parser.add_argument("schema_path", help="URL or path to Swagger schema")
parser.add_argument(
"-n",
dest="num_tests_per_op",
metavar="N",
type=int,
default=20,
help="number of tests to run per API operation",
)
parser.add_argument(
"-c",
"--continue-on-error",
dest="cont_on_err",
action="store_true",
help="continue on error",
)
parser.add_argument(
"-v",
"--verbose",
dest="get_report",
action="store_true",
help="get a report after the execution",
)
parser.add_argument(
"-u", "--username", help="username (implies 'basic' auth)"
)
parser.add_argument(
"-p", "--password", help="password (implies 'basic' auth)"
)
parser.add_argument(
"-k", "--token", help="api key token (implies 'apiKey' auth)"
)
parser.add_argument(
"--security-name",
help="force a security name if not 'basic' or 'apiKey'",
)
parser.add_argument("-e", "--extra-header", action="append")
parsed_args = parser.parse_args()
test_kwargs = {
"num_tests_per_op": (
parsed_args.num_tests_per_op or environ.get("SC_TESTS")
),
"cont_on_err": (
parsed_args.cont_on_err or environ.get("SC_CONTINUE_ON_ERROR")
),
"get_report": (
parsed_args.get_report or environ.get("SC_GET_REPORT")
),
"username": parsed_args.username or environ.get("SC_BASIC_USERNAME"),
"password": parsed_args.password or environ.get("SC_BASIC_PASSWORD"),
"token": parsed_args.token or environ.get("SC_API_TOKEN"),
"security_name": (
parsed_args.security_name or environ.get("SC_SECURITY_NAME")
),
}
extra_headers = {}
if parsed_args.extra_header is not None:
for header_pair in parsed_args.extra_header:
if ":" not in header_pair:
raise ValueError("header must be in the form HEADER:VALUE")
header, value = header_pair.split(":")
extra_headers[header] = value
if extra_headers:
test_kwargs["extra_headers"] = extra_headers
try:
api_conformance_test(parsed_args.schema_path, **test_kwargs)
except KeyboardInterrupt:
print(
Fore.WHITE
+ Back.RED
+ "Interrupted by user command"
+ Style.RESET_ALL
)
sys.exit(1)
if __name__ == "__main__":
main()