22import requests
33
44
5+ HEADER = "\033 [95m"
6+ OKBLUE = "\033 [94m"
7+ OKCYAN = "\033 [96m"
8+ OKGREEN = "\033 [92m"
9+ WARNING = "\033 [93m"
10+ FAIL = "\033 [91m"
11+ ENDC = "\033 [0m"
12+ BOLD = "\033 [1m"
13+ UNDERLINE = "\033 [4m"
514
615
7- HEADER = '\033 [95m'
8- OKBLUE = '\033 [94m'
9- OKCYAN = '\033 [96m'
10- OKGREEN = '\033 [92m'
11- WARNING = '\033 [93m'
12- FAIL = '\033 [91m'
13- ENDC = '\033 [0m'
14- BOLD = '\033 [1m'
15- UNDERLINE = '\033 [4m'
16-
1716def pretty_output (
1817 hostname ,
1918 method ,
2019 test_info ,
2120 url ,
22- direct_call = False ,
23- json_send = False ,
24- pull_key = False ,
25- token = False
21+ direct_call = False ,
22+ json_send = False ,
23+ pull_key = False ,
24+ token = False ,
2625):
2726
2827 # Describe the test.
29- print (' \n ' )
28+ print (" \n " )
3029 print (f"{ BOLD } ======= Test { test_info ['test_number' ]} of 16 ======={ ENDC } " )
31- print (' \n ' )
32- print (' Description: ' + test_info [' description' ])
33- print (' \n ' )
34-
30+ print (" \n " )
31+ print (" Description: " + test_info [" description" ])
32+ print (" \n " )
33+
3534 # Is the call constructed or direct?
36- call = ''
35+ call = ""
3736
3837 if direct_call == True :
3938 call = url
4039 else :
4140 call = hostname + url
42-
41+
4342 # Make the request.
44- r = ''
43+ r = ""
44+
45+ if method == "GET" :
4546
46- if method == 'GET' :
47-
4847 if token != False :
4948
50- hdrs = {
51- 'Authorization' : 'Token {}' .format (token )
52- }
49+ hdrs = {"Authorization" : "Token {}" .format (token )}
5350
5451 print (f"{ OKCYAN } Call ({ method } ): { call } " )
5552 print (f"Headers: { json .dumps (hdrs , indent = 4 )} { ENDC } " )
56- print ('\n ' )
57-
58- r = requests .get (
59- call ,
60- headers = hdrs
61- )
53+ print ("\n " )
54+
55+ r = requests .get (call , headers = hdrs )
6256
6357 else :
6458
6559 print (f"{ OKCYAN } Call ({ method } ): { call } " )
6660 print (f"Headers: None" )
67- print ('\n ' )
61+ print ("\n " )
62+
63+ r = requests .get (call )
64+
65+ elif method == "POST" :
6866
69- r = requests .get (
70- call
71- )
72-
73- elif method == 'POST' :
74-
7567 if token != False :
76-
77- hdrs = {
78- 'Authorization' : 'Token {}' .format (token )
79- }
80-
68+
69+ hdrs = {"Authorization" : "Token {}" .format (token )}
70+
8171 print (f"{ OKCYAN } Call ({ method } ): { call } " )
8272 print (f"Headers: { json .dumps (hdrs , indent = 4 )} " )
8373 print (f"Request body: { json .dumps (json_send , indent = 4 )} { ENDC } " )
84- print ('\n ' )
85-
86- r = requests .post (
87- call ,
88- json = json_send ,
89- headers = hdrs
90- )
91-
74+ print ("\n " )
75+
76+ r = requests .post (call , json = json_send , headers = hdrs )
77+
9278 else :
9379
9480 print (f"{ OKCYAN } Call ({ method } ): { call } " )
9581 print (f"Headers: None" )
9682 print (f"Request body: { json .dumps (json_send , indent = 4 )} { ENDC } " )
97- print ('\n ' )
98-
99- r = requests .post (
100- call ,
101- json = json_send
102- )
103-
83+ print ("\n " )
84+
85+ r = requests .post (call , json = json_send )
86+
10487 print (f"{ WARNING } +++ TEST RESULTS +++" )
105- print (' \n ' )
106-
88+ print (" \n " )
89+
10790 # What did we get?
108- if str (r .status_code ) + ' ' + r .reason == test_info ['expected_response_code' ]:
91+ if str (r .status_code ) + " " + r .reason == test_info ["expected_response_code" ]:
92+
93+ print (
94+ f"{ BOLD } { OKGREEN } Expected response code: { test_info ['expected_response_code' ]} "
95+ )
96+ print (
97+ f"{ OKGREEN } Receieved response code: { str (r .status_code ) + ' ' + r .reason } { ENDC } "
98+ )
99+ print ("\n " )
109100
110- print (f"{ BOLD } { OKGREEN } Expected response code: { test_info ['expected_response_code' ]} " )
111- print (f"{ OKGREEN } Receieved response code: { str (r .status_code ) + ' ' + r .reason } { ENDC } " )
112- print ('\n ' )
113-
114101 else :
115102
116- print (f"{ BOLD } { FAIL } Expected response code: { test_info ['expected_response_code' ]} " )
117- print (f"{ FAIL } Receieved response code: { str (r .status_code ) + ' ' + r .reason } { ENDC } " )
118- print ('\n ' )
119-
103+ print (
104+ f"{ BOLD } { FAIL } Expected response code: { test_info ['expected_response_code' ]} "
105+ )
106+ print (
107+ f"{ FAIL } Receieved response code: { str (r .status_code ) + ' ' + r .reason } { ENDC } "
108+ )
109+ print ("\n " )
110+
120111 try :
121112
122- dumped = json .dumps (json .loads (r .text ), indent = 4 )
113+ dumped = json .dumps (json .loads (r .text ), indent = 4 )
123114
124115 print (f"{ OKBLUE } Response" )
125- print (' --------' )
116+ print (" --------" )
126117 print (f"{ dumped } { ENDC } " )
127- print (' \n ' )
118+ print (" \n " )
128119
129120 # Do we need to pull anything?
130121 if pull_key != False :
131-
122+
132123 # Is the pull the entire response or
133124 # just one of the keys?
134125
135126 if pull_key == True :
136127 return json .loads (r .text )
137128 else :
138129 return json .loads (r .text )[pull_key ]
139-
130+
140131 except json .decoder .JSONDecodeError :
141132
142133 print (f"{ BOLD } { FAIL } Response" )
143134 print (f"--------" )
144135 print (f"Response failed...{ ENDC } " )
145- print (' \n ' )
136+ print (" \n " )
146137
147138
148-
149-
150- def relationships (
151- hn ,
152- oi ,
153- user1 ,
154- group1 ,
155- group2 ,
156- wheel_key
157- ):
139+ def relationships (hn , oi , user1 , group1 , group2 , wheel_key ):
158140
159141 print ("------------Relationship Tests ------------" )
160142 # Go through all possible user/group object permissions combinatorially.
@@ -164,57 +146,54 @@ def relationships(
164146
165147 # Set each test, then compare the object permission
166148 # results with what we would expect.
167- for value in [1 ,2 , 3 ]:
168- for perm in [' change' , ' delete' , ' view' ]:
169-
170- # Don't share prefix permissions.
149+ for value in [1 , 2 , 3 ]:
150+ for perm in [" change" , " delete" , " view" ]:
151+
152+ # Don't share prefix permissions.
171153
172- # We can set an arbitrary prefix permission.
154+ # We can set an arbitrary prefix permission.
173155 pretty_output (
174- hostname = hn ,
175- json_send = {
176- ' POST_api_prefixes_permissions_set' : [
156+ hostname = hn ,
157+ json_send = {
158+ " POST_api_prefixes_permissions_set" : [
177159 {
178- 'group' : [
179- 'bco_drafter'
180- ],
181- 'permissions' : [
182- 'view'
183- ],
184- 'prefix' : 'TEST' ,
185- 'username' : [
186- user1 ['username' ]
187- ],
160+ "group" : ["bco_drafter" ],
161+ "permissions" : ["view" ],
162+ "prefix" : "TEST" ,
163+ "username" : [user1 ["username" ]],
188164 }
189165 ],
190166 },
191- method = ' POST' ,
192- test_info = {
193- ' description' : ' Set the prefix permissions for a user or a group.' ,
194- ' expected_response_code' : ' 200 OK'
167+ method = " POST" ,
168+ test_info = {
169+ " description" : " Set the prefix permissions for a user or a group." ,
170+ " expected_response_code" : " 200 OK" ,
195171 },
196- token = user1 [' token' ],
197- url = ' /api/prefixes/permissions/set/'
172+ token = user1 [" token" ],
173+ url = " /api/prefixes/permissions/set/" ,
198174 )
199175
200176 for possibly in [0 , 1 ]:
201- for perm in [' change' , ' delete' , ' view' ]:
177+ for perm in [" change" , " delete" , " view" ]:
202178 print ("hi" )
203- # Share prefix permissions
204-
179+ # Share prefix permissions
205180
206- def superuser ():
181+
182+ def superuser ():
207183 print ("info" )
208184
185+
209186def account ():
210187 print ("info" )
211188
189+
212190def publishing ():
213191 print ("info" )
214192
193+
215194def drafts ():
216195 print ("info" )
217196
218197
219198def group ():
220- print ("info" )
199+ print ("info" )
0 commit comments