22import subprocess
33import sys
44import time
5+ from urllib .parse import urlparse
56
67import requests
78
@@ -30,11 +31,42 @@ def get_apidocs():
3031 return res .json ()["docs" ]
3132
3233
34+ def get_apienvs_id ():
35+ res = session .get (
36+ f"{ SERVER_URL } /autotest/projects/{ PROJECT_ID } /apienvs" ,
37+ params = {"page" : 1 , "size" : 100 },
38+ )
39+ return res .json ()["envs" ][0 ]["id" ]
40+
41+
3342def delete_old_apidocs (apidocs ):
3443 for apidoc in apidocs :
3544 session .delete (f"{ SERVER_URL } /autotest/projects/{ PROJECT_ID } /apidocs/{ apidoc ['id' ]} " )
3645
3746
47+ def create_api_auth (apienv_id , apidoc_id ):
48+ parsed = urlparse (OPENAPI_URL )
49+ data = {
50+ "apienv_id" : apienv_id ,
51+ "apidoc_id" : apidoc_id ,
52+ "base_url" : parsed .scheme + "://" + parsed .netloc ,
53+ "ssl_verify" : True ,
54+ "auth_test_url" : "/version" ,
55+ "auth_test_request_method" : "GET" ,
56+ "auth_test_body" : None ,
57+ "auth_test_request_type" : None ,
58+ "auth_method" : "custom" ,
59+ "data" : {
60+ "filename" : "custom_auth.py" ,
61+ "pyscript" : "from requests import Request\n \n \n \n def set_auth(request: Request):\n # Set authentication for the request\n # Request reference can be found at https://requests.readthedocs.io/en/latest/api/#requests.Request\n pass\n " , # noqa: E501
62+ },
63+ }
64+ session .post (
65+ f"{ SERVER_URL } /autotest/projects/{ PROJECT_ID } /apiauths" ,
66+ json = data ,
67+ )
68+
69+
3870def get_local_version ():
3971 cmd = "git rev-parse HEAD"
4072 res = subprocess .run (cmd , shell = True , check = True , stdout = subprocess .PIPE )
@@ -63,8 +95,7 @@ def check_api_version():
6395 flush = True ,
6496 )
6597 time .sleep (5 )
66- except Exception as e :
67- print (f"检查 API 版本失败!{ e } " , flush = True )
98+ except Exception :
6899 time .sleep (5 )
69100
70101
@@ -86,8 +117,7 @@ def wait_for_testcase_done(testcase_id):
86117 flush = True ,
87118 )
88119 time .sleep (5 )
89- except Exception as e :
90- print (f"检查文本用例状态失败!{ e } " , flush = True )
120+ except Exception :
91121 time .sleep (5 )
92122
93123
@@ -107,8 +137,7 @@ def wait_for_testcode_done(task_id):
107137 flush = True ,
108138 )
109139 time .sleep (5 )
110- except Exception as e :
111- print (f"检查自动测试脚本生成失败!{ e } " , flush = True )
140+ except Exception :
112141 time .sleep (5 )
113142
114143
@@ -165,10 +194,11 @@ def main():
165194 api_path = args [0 ]
166195 method = args [1 ]
167196 test_target = " " .join (args [2 :])
168- docs = get_apidocs ()
197+ apidocs = get_apidocs ()
198+ apienv_id = get_apienvs_id ()
169199 with Step ("检查 API 版本是否更新..." ):
170200 check_api_version ()
171- delete_old_apidocs (docs )
201+ delete_old_apidocs (apidocs )
172202
173203 with Step (f"上传 OpenAPI 文档,并且触发 API { api_path } 的测试用例和自动测试脚本生成任务..." ):
174204 # 使用配置的OPENAPI_URL
@@ -182,10 +212,10 @@ def main():
182212 res = session .post (
183213 f"{ SERVER_URL } /autotest/projects/{ PROJECT_ID } /apidocs" ,
184214 files = {"file" : ("openapi.json" , res .content , "application/json" )},
185- data = {"apiauth_id" : docs [0 ]["apiauth_id" ]},
186215 )
187216 if res .status_code == 200 :
188217 print ("上传 OpenAPI 文档成功!\n " )
218+ create_api_auth (apienv_id , res .json ()["id" ])
189219 else :
190220 print (f"上传 OpenAPI 文档失败!{ res .text } " , flush = True )
191221 return
@@ -220,6 +250,7 @@ def main():
220250 testcode_id = testcode ["id" ]
221251 res = session .post (
222252 f"{ SERVER_URL } /autotest/projects/{ PROJECT_ID } /testcodes/{ testcode_id } /exec" ,
253+ json = {"apienv_id" : apienv_id },
223254 )
224255 if res .status_code == 200 :
225256 print ("提交执行自动测试脚本成功!" , flush = True )
0 commit comments