def api_debug(request): """ HTTP接口调试 :param request: :return: 接口调用结果 """ if request.method == "POST": url = request.POST.get("req_url", "") method = request.POST.get("req_method", "") type_ = request.POST.get("req_type", "") parameter = request.POST.get("req_parameter", "") if url == "" or method == "" or type_ == "": return common.response_failed("必传参数为空") payload = json.loads(parameter.replace("'", "\"")) if method == "get": if type_ == "from": r = requests.get(url, params=payload) else: return common.response_failed("参数类型错误") if method == "post": if type_ == "from": r = requests.post(url, data=payload) elif type_ == "json": r = requests.post(url, json=payload) else: return common.response_failed("参数类型错误") return common.response_succeed(data=r.text) else: return common.response_failed("请求方法错误")
def debug_case(request): if request.method == "POST": url = request.POST.get("url", "") method = request.POST.get("method", "").upper() param_type = request.POST.get("paramType", "").upper() headers = json.loads(request.POST.get("headers").replace("'", "\"")) params = json.loads(request.POST.get("params").replace("'", "\"")) if url == "" or method == "" or param_type == "": return response_fail("URL、请求方法、请求参数类型不能为空!") if method == "GET": if param_type == "FORM-DATA": r = requests.get(url, params=params, headers=headers) else: return response_fail("请求参数类型错误!") if method == "POST": if param_type == "FORM-DATA": r = requests.post(url, data=params, headers=headers) elif param_type == "JSON": r = requests.post(url, json=params, headers=headers) else: return response_fail("请求参数类型错误!") return response_succeed(data=r.text) else: return response_fail("该接口只支持POST请求!")
def get_project_list(request): """ 获取项目模块列表 :param request: :return: 项目接口列表 """ if request.method == "GET": project_list = Project.objects.all() data_list = [] for project in project_list: project_dict = { "name": project.name } module_list = Module.objects.filter(project_id=project.id) if len(module_list) != 0: module_name = [] for module in module_list: module_name.append(module.name) project_dict["moduleList"] = module_name data_list.append(project_dict) return common.response_succeed(data=data_list) else: return common.response_failed("请求方法错误")
def save_case(request): if request.method == "POST": module_name = request.POST.get("moduleName", "") name = request.POST.get("name", "") url = request.POST.get("url", "") method = request.POST.get("method", "").upper() param_type = request.POST.get("paramType", "") headers = request.POST.get("headers", "{}") params = request.POST.get("params", "{}") assert_text = request.POST.get("assertText", "") if module_name == "" or name == "" or url == "" or method == "" or param_type == "": return response_fail("模块名、用例名、url、请求方法、请求参数类型不能为空!") case = TestCase.objects.create( module=Module.objects.get(name=module_name), name=name, url=url, request_method=method, request_headers=headers, request_params_type=param_type, request_params=params, response_assert=assert_text, ) if case is not None: return response_succeed("保存成功!") else: return response_fail("未知错误!保存失败!")
def get_cases_for_ztree(request): if request.method == "GET": # 构造“已选用例树”节点数据 ztree_nodes_selected_cases = [{ "zId": 0, "name": "已选用例", "open": "true", "UID": "0_0_0" # 使用"项目id_模块id_用例id"作为唯一标识 }] # 构造“可选用例树”节点数据 ztree_nodes_all_cases = [{ "zId": 0, "name": "可选用例", "open": "true", "UID": "0_0_0" }] case_all = TestCase.objects.all() generate_ztree_nodes(ztree_nodes_all_cases, case_all) return response_succeed( data={ "ztreeNodesForAllCases": ztree_nodes_all_cases, "ztreeNodesForSelectedCases": ztree_nodes_selected_cases }) else: return response_fail("该接口只支持GET请求!")
def get_case_info(request): """ 获取接口数据 :param request: :return: """ if request.method == "POST": case_id = request.POST.get("caseId", "") if case_id == "": return common.response_failed("用例id为空!") case_obj = TestCase.objects.get(pk=case_id) module_obj = Module.objects.get(id=case_obj.module_id) module_name = module_obj.name project_name = Project.objects.get(id=module_obj.project_name_id).name case_info = { "modulename": module_name, "projectName": project_name, "name": case_obj.name, "url": case_obj.url, "reqMethod": case_obj.req_method, "reqType": case_obj.req_type, "reqHeader": case_obj.req_header, "reqParameter": case_obj.req_parameter, "assertText": case_obj.response_assert, } return common.response_succeed(data=case_info) else: return common.response_failed("请求方法错误!")
def get_task_info(request): if request.method == "POST": tid = request.POST.get("taskId", "") if tid == "": return common.response_failed("任务id不能为空") task_obj = TestTask.objects.get(id=tid) task_info = { "id": task_obj.id, "name": task_obj.name, "describe": task_obj.describe } cases_id = task_obj.cases.split(",") print(cases_id) cases_list = return_cases_list() print(cases_list) for i in range(len(cases_list)): for cid in cases_id: if int(cid) == int(cases_list[i]["id"]): cases_list[i]["status"] = True break else: cases_list[i]["status"] = False task_info["cases"] = cases_list return common.response_succeed(message="获取成功!", data=task_info) else: return common.response_failed("请求方法错误")
def save_case(request): """ 保存接口测试用例 :param request: :return: """ if request.method == "POST": name = request.POST.get("name", "") url = request.POST.get("req_url", "") method = request.POST.get("req_method", "") parameter = request.POST.get("req_parameter", "") req_type = request.POST.get("req_type", "") header = request.POST.get("header", "") module_name = request.POST.get("module", "") assert_text = request.POST.get("assert_text", "") if url == "" or method == "" or req_type == "" or module_name == "" or assert_text == "": return common.response_failed("必传参数为空") if parameter == "": parameter = "{}" if header == "": header = "{}" module_obj = Module.objects.get(name=module_name) case = TestCase.objects.create(name=name, module=module_obj, url=url, req_method=method, req_header=header, req_type=req_type, req_parameter=parameter, respose_assert=assert_text) if case is not None: return common.response_succeed("保存成功!")
def update_case(request, case_id): if request.method == "POST": module_name = request.POST.get("moduleName", "") name = request.POST.get("name", "") url = request.POST.get("url", "") method = request.POST.get("method", "").upper() param_type = request.POST.get("paramType", "") headers = request.POST.get("headers", "{}") params = request.POST.get("params", "{}") assert_text = request.POST.get("assertText", "") if module_name == "" or name == "" or url == "" or method == "" or param_type == "": return response_fail("模块名、用例名、url、请求方法、请求参数类型不能为空!") case = TestCase.objects.filter(id=case_id).update( module=Module.objects.get(name=module_name), name=name, url=url, request_method=method, request_headers=headers, request_params_type=param_type, request_params=params, response_assert=assert_text, ) if case == 1: return response_succeed("更新成功!") else: response_fail("更新失败!") else: return response_fail("该接口只支持POST请求!")
def delete_task(request): if request.method == "POST": id = request.POST.get("id", "") if id == "": return common.response_failed("删除失败,ID不存在") TestTask.objects.filter(id=id).delete() return common.response_succeed(message="删除成功") else: return common.response_failed("请求方法错误!")
def task_result(request): if request.method == "POST": rid = request.POST.get("result_id", "") result_obj = TestResult.objects.get(id=rid) data = { "result": result_obj.result, } return common.response_succeed(message="获取成功!", data=data) else: return common.response_failed("请求方法错误")
def delete_task(request): if request.method == "POST": tid = request.POST.get("task_id", "") if tid == "": return common.response_failed("任务id不能为空") task_obj = TestTask.objects.get(id=tid) task_obj.delete() return common.response_succeed(message="删除成功!") else: return common.response_failed("请求方法错误")
def get_case_list(request): """ 获取测试用例列表 :param request: :return: """ if request.method == "GET": cases_list = return_cases_list() return common.response_succeed(data=cases_list) else: return common.response_failed("请求方法错误")
def task_result(request): '''任务结果''' if request.method == "POST": id = request.POST.get("id", "") result_obj = TestResult.objects.get(id=id) data={ "result":result_obj.result } return common.response_succeed(message="获取数据成功!",data=data) else: return common.response_failed(message="请求方法错误!")
def get_task_info(request): if request.method == "POST": task_id = request.POST.get('taskId','') if task_id == '': return common.response_failed("task id Null.") task_obj = TestTask.objects.get(pk=task_id) task_info = { "task_name":task_obj.name, "task_describe":task_obj.describe, "task_cases" : task_obj.cases, } return common.response_succeed(data=task_info) else: return HttpResponse("404")
def save_task(request): if request.method == "POST": name = request.POST.get("task_name", "") describe = request.POST.get("task_describe", "") cases = request.POST.get("task_cases", "") if name == '': return common.response_failed("名称不能为空!") if cases == '': return common.response_failed("用例不能为空!") task = TestTask.objects.create(name = name,describe=describe,cases=cases,status=0) if task is not None: return common.response_succeed("保存成功!") else: return common.response_failed("请求方法错误!")
def debug_case(request): if request.method == 'POST': url = request.POST.get("req_url",'') method = request.POST.get("req_method",'') type_ = request.POST.get("req_type",'') header = request.POST.get("req_header",'') parameter = request.POST.get("req_parameter",'') if validators.url(url) != True: return common.response_failed("请检查URL!") if method not in ['post','get']: return common.response_failed("请检查请求方法!") if header != '': try: header_dict = json.loads(header.replace("'", "\"")) if type(header_dict) != dict: raise TypeError except Exception: return common.response_failed("请检查header格式!") else: header_dict = None if parameter != '': try: payload = json.loads(parameter.replace("'", "\"")) if type(payload) != dict: raise TypeError except json.decoder.JSONDecodeError: return common.response_failed("请检查参数的格式!") else: payload = None if method == "get": if type_ == "from": r = requests.get(url, headers=header_dict, params=payload) else: return common.response_failed("参数类型错误") if method == "post": if type_ == "from": r = requests.post(url, headers=header_dict, data=payload) elif type_ == "json": r = requests.post(url, headers=header_dict, json=payload) else: return common.response_failed("参数类型错误") print(r.text) return common.response_succeed(data=r.text) else: return common.response_failed("请求方法错误")
def assert_result(request): if request.method == "POST": response_result = request.POST.get("response_result", "") assert_text = request.POST.get("assert_text", "") if response_result == "" or assert_text == "": return response_fail("验证数据或者响应结果不能为空!") try: assert assert_text in response_result except AssertionError: return response_fail("验证失败!") else: return response_succeed("验证成功!") else: return response_fail("该接口只支持POST请求!")
def get_case_list(request): """获取项目用例列表""" if request.method == "GET": project_list = Project.objects.all() case_list = [] for project in project_list: module_list = Model.objects.filter(project_id=project.id) for module in module_list: cases = TestCase.objects.filter(module_id=module.id) for case in cases: case_info = project.title + "->" + module.name + "->" + case.name case_dict = {"id": case.id, "name": case_info} case_list.append(case_dict) return common.response_succeed(data=case_list) else: return common.response_failed("请求方法错误")
def api_assert(request): """对测试用例的断言进行验证""" if request.method == "POST": result_text = request.POST.get("result", "") assert_text = request.POST.get("assert", "") if result_text == "" or assert_text == "": return common.response_failed("验证的数据不能为空") try: assert assert_text in result_text except AssertionError: return common.response_failed("验证失败!") else: return common.response_succeed("验证成功!") else: return common.response_failed("请求方法错误")
def get_case_info(request, case_id): if case_id: target_case = TestCase.objects.get(id=case_id) response_data = {} response_data["name"] = target_case.name response_data["url"] = target_case.url response_data["method"] = target_case.request_method response_data["headers"] = target_case.request_headers response_data["paramsType"] = target_case.request_params_type response_data["params"] = target_case.request_params response_data["assertText"] = target_case.response_assert module = target_case.module response_data["moduleName"] = module.name response_data["projectName"] = module.project.name return response_succeed(data=response_data) else: return response_fail("用例ID不存在!")
def update_case(request): if request.method == 'POST': case_id = request.POST.get("cid", "") name = request.POST.get("name", "") url = request.POST.get("req_url", "") method = request.POST.get("req_method", "") parameter = request.POST.get("req_parameter", "") type_ = request.POST.get("req_type", "") header = request.POST.get("header", "") module_name = request.POST.get("module", "") project_name = request.POST.get("project", "") assert_text = request.POST.get("assert_text", "") if validators.url(url) != True: return common.response_failed("请检查URL!") if method not in ['post','get']: return common.response_failed("请检查请求方法!") if header != '': try: header_dict = json.loads(header.replace("'", "\"")) if type(header_dict) != dict: raise TypeError except Exception: return common.response_failed("请检查header格式!") else: header = "{}" if parameter != '': try: payload = json.loads(parameter.replace("'", "\"")) if type(payload) != dict: raise TypeError except json.decoder.JSONDecodeError: return common.response_failed("请检查参数的格式!") else: parameter = "{}" project = Project.objects.get(name = project_name) module_obj = Module.objects.get(name = module_name,project__id=project.id) try: TestCase.objects.filter(id=case_id).update(name = name,module=module_obj,url=url,req_method=method ,req_type=type_,req_header=header,req_parameter=parameter,responses_assert=assert_text) except Exception as e: return common.response_failed("保存失败!") return common.response_succeed("保存成功!") else: return HttpResponse("404")
def update_case(request): """ 更新接口测试用例 :param request: :return: """ if request.method == "POST": case_id = request.POST.get("cid", "") name = request.POST.get("name", "") url = request.POST.get("req_url", "") method = request.POST.get("req_method", "") parameter = request.POST.get("req_parameter", "") req_type = request.POST.get("req_type", "") header = request.POST.get("header", "") module_name = request.POST.get("module", "") project_name = request.POST.get("project","") assert_text = request.POST.get("assert_text", "") print("接口id", case_id) if url == "" or method == "" or req_type == "" or module_name == "" or assert_text == "": return common.response_failed("必传参数为空") if parameter == "": parameter = "{}" if header == "": header = "{}" module_obj = Module.objects.filter(project__name=project_name).filter(name=module_name)[0] case_obj = TestCase.objects.filter(id=case_id).update( module=module_obj, name=name, url=url, req_method=method, req_header=header, req_type=req_type, req_parameter=parameter, resp_assert=assert_text ) if case_obj == 1: return common.response_succeed("更新成功!") else: return common.response_failed("更新失败!") else: return common.response_failed("请求方法错误")
def get_projects_and_modules(request): if request.method == "GET": projects = Project.objects.all() project_list = [] for project in projects: current_project = {"projectName": project.name} modules = Module.objects.filter(project_id=project.id) if len(modules) != 0: module_names = [] for module in modules: module_names.append(module.name) current_project["moduleNames"] = module_names project_list.append(current_project) return response_succeed(data={"projects": project_list}) else: return response_fail("该接口只支持GET请求!")
def save_task_data(request): if request.method == "POST": name = request.POST.get("task_name", "") describe = request.POST.get("task_describe", "") cases = request.POST.get("task_cases", "") if name == "": return common.response_failed("任务的名称不能为空") # 保存数据库 task = TestTask.objects.create(name=name, describe=describe, cases=cases) if task is None: return common.response_failed("创建失败") return common.response_succeed(message="创建任务成功!") else: return common.response_failed("请求方法错误")
def run_task(request): if request.method == "POST": tid = request.POST.get("task_id", "") if tid == "": return common.response_failed("任务ID不能为空") task_list = TestTask.objects.all() runing_task = 0 for task in task_list: if task.status == 1: runing_task = 1 break if runing_task == 1: return common.response_failed("当前有任务正在执行...") else: TaskThread(tid).new_run() return common.response_succeed(message="已执行") else: return common.response_failed("请求方法错误")
def run_task(request): '''运行任务i''' if request.method =="POST": id = request.POST.get("id","") if id =="": return common.response_failed("任务id不能为空") task_obj = TestTask.objects.all() runing_task = 0 for i in task_obj: if i.status == 1: runing_task = 1 break if runing_task == 1: return common.response_failed("当前有任务正在执行...") else: TaskThread(id).run_new() return common.response_succeed(message="已执行") else: return common.response_failed("请求方法错误")
def get_case_list(request): #项目——》模块——》用例 if request.method == "GET": task_cases = request.GET.get("task_case", "") cases_list = [] projects = Project.objects.all() for project in projects: modules = Module.objects.filter(project_id = project.id) for module in modules: cases = TestCase.objects.filter(module_id = module.id) for case in cases: case_info = project.name + "--->" + module.name + "--->" + case.name case_dict = { "id":case.id, "name":case_info } cases_list.append(case_dict) data = {"cases_list":cases_list,"check_cases":task_cases} return common.response_succeed(data=data) else: return common.response_failed("请求方法错误!")
def api_debug(request): """ HTTP接口调试 :param request: :return: 接口调用结果 """ if request.method == "POST": url = request.POST.get("req_url", "") method = request.POST.get("req_method", "") type = request.POST.get("req_type", "") parameter = request.POST.get("req_parameter", "") header = request.POST.get("req_header", "") if url == "" or method == "" or type == "": return common.response_failed("必传参数为空!") try: header_dict = json.loads(header.replace("'", "\"")) payload = json.loads(parameter.replace("'", "\"")) except json.decoder.JSONDecodeError: return common.response_failed("请检查header或参数的格式!") if method == "get": if type == "from": r = requests.get(url, params=parameter) else: return common.response_failed("参数类型错误!") if method == "post": if type == "from": r = requests.post(url, data=parameter) elif type == "json": r = requests.post(url, json=parameter) else: return common.response_failed("参数类型错误!") return common.response_succeed(data=r.text) else: return common.response_failed("请求方法错误!")
def get_case_list(request): """ 获取测试用例列表 :param request: :return: """ if request.method == "GET": cases_list = [] projects = Project.objects.all() for project in projects: modules = Module.objects.filter(project_id=project.id) for module in modules: cases = TestCase.objects.filter(module_id=module.id) for case in cases: case_info = project.name + " -> " + module.name + " -> " + case.name case_dict = {'id': case.id, 'name': case_info} cases_list.append(case_dict) return common.response_succeed(data=cases_list) else: return common.response_failed("请求方法错误")