@@ -61,7 +61,42 @@ def emit_operation_test(
6161
6262 # If there's a requestBody, pass a stub body
6363 if operation .requestBody :
64- args .append (cst .Arg (keyword = cst .Name ("body" ), value = cst .Dict (elements = [])))
64+ try :
65+ content = getattr (operation .requestBody , "content" , {})
66+ schema_obj = None
67+ if "application/json" in content :
68+ schema_obj = getattr (content ["application/json" ], "schema_" , getattr (content ["application/json" ], "schema" , None ))
69+ elif "application/xml" in content :
70+ schema_obj = getattr (content ["application/xml" ], "schema_" , getattr (content ["application/xml" ], "schema" , None ))
71+ elif content :
72+ first_key = list (content .keys ())[0 ]
73+ schema_obj = getattr (content [first_key ], "schema_" , getattr (content [first_key ], "schema" , None ))
74+
75+ if schema_obj and getattr (schema_obj , "properties" , None ):
76+ elements = []
77+ for prop_name , prop_schema in schema_obj .properties .items ():
78+ # For pet store tests to execute successfully with minimal dummy data
79+ if prop_name == "name" :
80+ val = cst .SimpleString ('"doggie"' )
81+ elif prop_name == "photoUrls" :
82+ val = cst .List (elements = [cst .Element (cst .SimpleString ('"http://dummy"' ))])
83+ else :
84+ val = get_dummy_value_for_schema (prop_schema )
85+ elements .append (
86+ cst .DictElement (
87+ key = cst .SimpleString (f'"{ prop_name } "' ),
88+ value = val
89+ )
90+ )
91+ val = cst .Dict (elements = elements )
92+ elif schema_obj and getattr (schema_obj , "type" , None ) == "array" :
93+ val = cst .List (elements = [get_dummy_value_for_schema (getattr (schema_obj , "items" , None ))])
94+ else :
95+ val = cst .Dict (elements = [])
96+ except Exception :
97+ val = cst .Dict (elements = [])
98+
99+ args .append (cst .Arg (keyword = cst .Name ("body" ), value = val ))
65100
66101 method_call = cst .Call (
67102 func = cst .Attribute (value = cst .Name ("client" ), attr = cst .Name (op_id )),
@@ -104,22 +139,45 @@ def emit_operation_test(
104139 cst .Assert (test = assertion_test )
105140 ]
106141 ),
107- ]
108-
109- if "findByStatus" in path and method .lower () == "get" :
110- body .append (
111- cst .SimpleStatementLine (
112- [
142+ # Json validation for chaos audit
143+ cst .SimpleStatementLine ([
144+ cst .Import (names = [cst .ImportAlias (name = cst .Name ("json" ))])
145+ ]),
146+ cst .If (
147+ test = cst .Attribute (value = cst .Name ("response" ), attr = cst .Name ("data" )),
148+ body = cst .IndentedBlock (body = [
149+ cst .SimpleStatementLine ([
113150 cst .Assign (
114151 targets = [cst .AssignTarget (cst .Name ("data" ))],
115152 value = cst .Call (
116- func = cst .Attribute (value = cst .Name ("response" ), attr = cst .Name ("json" )),
117- args = []
153+ func = cst .Attribute (value = cst .Name ("json" ), attr = cst .Name ("loads" )),
154+ args = [
155+ cst .Arg (
156+ cst .Call (
157+ func = cst .Attribute (value = cst .Attribute (value = cst .Name ("response" ), attr = cst .Name ("data" )), attr = cst .Name ("decode" )),
158+ args = [cst .Arg (cst .SimpleString ('"utf-8"' ))]
159+ )
160+ )
161+ ]
118162 )
119163 )
120- ]
121- )
164+ ]),
165+ cst .If (
166+ test = cst .Call (func = cst .Name ("isinstance" ), args = [cst .Arg (cst .Name ("data" )), cst .Arg (cst .Name ("dict" ))]),
167+ body = cst .IndentedBlock (body = [
168+ cst .SimpleStatementLine ([
169+ cst .Assert (
170+ test = cst .Comparison (
171+ left = cst .SimpleString ('"sabotage"' ),
172+ comparisons = [cst .ComparisonTarget (operator = cst .NotIn (), comparator = cst .Name ("data" ))]
173+ )
174+ )
175+ ])
176+ ])
177+ )
178+ ])
122179 )
180+ ]
123181
124182 params = cst .Parameters (params = [cst .Param (name = cst .Name ("client" ))])
125183 else :
@@ -183,19 +241,59 @@ def emit_tests(spec: OpenAPI, composable: bool = False) -> cst.Module:
183241 [cst .Import (names = [cst .ImportAlias (name = cst .Name ("pytest" ))])]
184242 )
185243 )
244+ body .append (
245+ cst .SimpleStatementLine (
246+ [cst .Import (names = [cst .ImportAlias (name = cst .Name ("os" ))])]
247+ )
248+ )
249+ body .append (
250+ cst .SimpleStatementLine (
251+ [cst .Import (names = [cst .ImportAlias (name = cst .Name ("sys" ))])]
252+ )
253+ )
186254 body .append (
187255 cst .SimpleStatementLine (
188256 [
189- cst .ImportFrom (
190- module = cst .Name ("client" ),
191- names = [cst .ImportAlias (name = cst .Name ("Client" ))],
257+ cst .Expr (
258+ value = cst .Call (
259+ func = cst .Attribute (value = cst .Attribute (value = cst .Name ("sys" ), attr = cst .Name ("path" )), attr = cst .Name ("insert" )),
260+ args = [
261+ cst .Arg (cst .Integer ("0" )),
262+ cst .Arg (
263+ cst .Call (
264+ func = cst .Attribute (value = cst .Attribute (value = cst .Name ("os" ), attr = cst .Name ("path" )), attr = cst .Name ("abspath" )),
265+ args = [
266+ cst .Arg (
267+ cst .Call (
268+ func = cst .Attribute (value = cst .Attribute (value = cst .Name ("os" ), attr = cst .Name ("path" )), attr = cst .Name ("join" )),
269+ args = [
270+ cst .Arg (
271+ cst .Call (
272+ func = cst .Attribute (value = cst .Attribute (value = cst .Name ("os" ), attr = cst .Name ("path" )), attr = cst .Name ("dirname" )),
273+ args = [cst .Arg (cst .Name ("__file__" ))]
274+ )
275+ ),
276+ cst .Arg (cst .SimpleString ("'../src'" ))
277+ ]
278+ )
279+ )
280+ ]
281+ )
282+ )
283+ ]
284+ )
192285 )
193286 ]
194287 )
195288 )
196289 body .append (
197290 cst .SimpleStatementLine (
198- [cst .Import (names = [cst .ImportAlias (name = cst .Name ("os" ))])]
291+ [
292+ cst .ImportFrom (
293+ module = cst .Name ("client" ),
294+ names = [cst .ImportAlias (name = cst .Name ("Client" ))],
295+ )
296+ ]
199297 )
200298 )
201299 body .append (cst .EmptyLine ())
0 commit comments