示例#1
0
def saveOrUpdateData(request):
    result = {}
    if request.method == 'POST':
        req = json.loads(request.body)["params"]
        id = int(req["apiid"])
        data = req["sendData"]
        apiName = data["apiName"]
        method = data["method"]
        host = data["host"]
        url = data["url"]
        if apiName == "" or host == "" or url == "":
            result = {"code": -1, "info": "名称、host、url不能为空"}
            return JsonResponse(result)
        dependcase_apiID = data["dependcase_apiID"]
        dependData = data["dependData"]
        header = str(data["header"]).replace(" ", "")
        body_str = str(data["bodys"]).replace(" ", "")
        # cbody = body_str
        # cheader = header
        # if cbody != "" and cbody is not None:
        #     try:
        #         json.loads(cbody)
        #     except Exception as e:
        #         result = {"code": -1, "info": "body格式不正确"}
        #         return JsonResponse(result)
        # if cheader != "" and cheader is not None:
        #     try:
        #         json.loads(cheader)
        #     except Exception as e:
        #         result = {"code": -1, "info": "header格式不正确"}
        #         return JsonResponse(result)
        if body_str != "" and body_str is not None and body_str != "{}":
            body = {"showflag": 3, "datas": [{"paramValue": body_str}]}
            body = json.dumps(body)
        else:
            body = "{}"
        if header == "" or header is None:
            header = "{}"
        assertinfo = data["assert"]
        environment = data["environment"]
        userName = data["user"]
        hostid = apiInfoTable.objects.get(apiID=id).host
        try:
            apiInfoTable.objects.filter(apiID=id).update(apiName=apiName,
                                                         method=method,
                                                         url=url,
                                                         headers=header,
                                                         body=body,
                                                         creator=userName,
                                                         assertinfo=assertinfo)
            hostTags.objects.filter(id=int(hostid)).update(qa=host)
            batchUntils.getHost(hostid, environment)
        except Exception as e:
            result = {"code": -1, "info": "更新失败:" + str(e)}
            return JsonResponse(result)
        # 更新dependcase_id
        result = updatedependcase(id, dependcase_apiID)
        # 更新dependData
        result = updataDatas(id, dependData)
    return JsonResponse(result)
示例#2
0
def getapiInfos(request):
    result = {}
    if request.method == 'GET':
        id = request.GET['apiid']
        environment = request.GET["environment"]
        try:
            query = apiInfoTable.objects.get(apiID=id)
            proquery = projectList.objects.all().values("id", "projectName").distinct()
        except BaseException as e:
            print(" SQL Error: %s" % e)
            result = {'code': -1, 'info': 'sql error!'}
            return JsonResponse(result)
        if query != None:
            json_dict = {}
            module_list = []
            header_list = []
            project_list = []
            for pro in proquery:
                project_list.append(pro)
            json_dict["id"] = query.apiID
            json_dict["name"] = query.apiName
            json_dict["creator"] = query.creator
            json_dict["method"] = query.method
            showbodyState =0
            body_list = []
            if query.headers != "{}" and query.headers != "" and query.headers is not None:
                if len(re.findall(r'\${(.*?)}', str(query.headers))) == 0:
                    header_data = json.loads(query.headers)
                    for k in header_data:
                        header_dict = {}
                        header_dict["type"] = k
                        header_dict["value"] = header_data[k]
                        header_list.append(header_dict)
                else:
                    header_strList = str(query.headers)[1:-1].replace("\"", "").split(",")
                    for item in header_strList:
                        item_list = item.split(":")
                        header_dict = {}
                        header_dict["type"] = item_list[0]
                        header_dict["value"] = item_list[1]
                        header_list.append(header_dict)
                # print header_list
                json_dict["header"] = header_list
            else:
                json_dict["header"] = []
            if query.body != "{}" and query.body != "" and query.body is not None:
                bodydata = json.loads(query.body)
                stateflag = bodydata["showflag"]
                if stateflag == 3:
                    showbodyState = 3
                    for i in bodydata["datas"]:
                        body_dict = {}
                        body_dict["value"] = i["paramValue"]
                        body_list.append(body_dict)
                    json_dict["body"] = body_list
                    # json_dict["body"] = bodydata["datas"][0]["paramValue"]
                elif stateflag==1:
                    for i in bodydata["datas"]:
                        body_dict = {}
                        body_dict["name"] = i["paramName"]
                        body_dict["type"] = i["paramType"]
                        body_dict["value"] = i["paramValue"]
                        body_list.append(body_dict)
                    showbodyState = 1
                    json_dict["body"] = body_list
                elif stateflag == 2:
                    showbodyState = 2
                    for i in bodydata["datas"]:
                        body_dict = {}
                        body_dict["name"] = i["paramName"]
                        body_dict["type"] = i["paramType"]
                        body_dict["value"] = i["paramValue"]
                        body_list.append(body_dict)
                    json_dict["body"] = body_list
                    # print("body:****", json_dict["body"])
                elif stateflag == 0:
                    showbodyState = 0
                    for i in bodydata["datas"]:
                        body_dict = {}
                        body_dict["name"] = i["paramName"]
                        body_dict["type"] = i["paramType"]
                        body_dict["value"] = i["paramValue"]
                        body_list.append(body_dict)
                    json_dict["body"] = body_list
                else:
                    showbodyState = 0
                    json_dict["body"] = []
            else:
                json_dict["body"] = []
            json_dict["url"] = query.url
            if str(query.assertinfo) != "" and str(query.assertinfo) is not None:
                try:
                    json_dict["assert"] = json.loads(query.assertinfo)
                except Exception as e:
                    json_dict["assert"] = [{"item": "status_code", "item_value": 200}]
            else:
                json_dict["assert"] = [{"item": "status_code", "item_value": 200}]
            json_dict["listid"] = query.owningListID
            json_dict["response"] = query.response
            listdata = moduleList.objects.get(id=int(query.owningListID))
            json_dict["moduleName"] = listdata.moduleName
            pid = listdata.owningListID
            json_dict["projectID"] = pid
            json_dict["projectName"] = projectList.objects.get(id=int(pid)).projectName
            json_dict["host"] = batchUntils.getHost(int(query.host),environment)
            caseOwnID_list = []
            modulelist = moduleList.objects.filter(owningListID=int(pid))
            for module in modulelist:
                module_list.append(module.moduleName)
                caseOwnID_list.append(module.id)
            depend_caseList = batchUntils.getdependCaseList(caseOwnID_list, id)
            # print "*******modulelist*******", module_list
            json_dict["depend_list"] = depend_caseList
            json_dict["depend_data"] = query.depend_casedata
            json_dict["t_id"] = query.t_id
            json_dict["depend_caseId"] = query.depend_caseId
            if query.depend_caseId != "" and query.depend_caseId is not None:
                json_dict["dependCaseId_apiid"] = apiInfoTable.objects.get(t_id=query.depend_caseId).apiID  # dependCaseId_apiid表示tid不为空的用例所依赖用例的id
            else:
                json_dict["dependCaseId_apiid"] = 0
            result = {'code': 0, 'datas': json_dict, 'info': 'success',
                      "module_list": module_list,
                      "project_list": project_list,
                      "showbody":showbodyState}
    return JsonResponse(result)
示例#3
0
def newCase(request):
    if request.method == "POST":
        reqdata = json.loads(request.body)["params"]
        data = reqdata["data_to_send"]
        methods = data["methods"]
        host = data["host"]
        url = data["url"]
        headers = json.dumps(data["headers"])
        bodyinfor = data["bodyinfor"]
        projectName = data["projectName"]
        moduleName = data["moduleName"]
        caseName = data["caseName"]
        creator = request.session.get('username')
        send_body = json.dumps(bodyinfor)
        flag = reqdata["flag"]
        assertData = data["assertData"]
        # print flag
        if (flag == False):
            try:
                hostTags.objects.get_or_create(qa=host)
                pid = projectList.objects.filter(
                    projectName=projectName).values("id")
                owningListID = pid[0]["id"]
                mid = moduleList.objects.get(owningListID=owningListID,
                                             moduleName=moduleName).id
                hoststr = hostTags.objects.filter(qa=host).values("id")
                hostid = hoststr[0]["id"]
                apiInfoTable.objects.get_or_create(method=methods,
                                                   headers=headers,
                                                   host=hostid,
                                                   url=url,
                                                   body=send_body,
                                                   assertinfo=assertData,
                                                   apiName=caseName,
                                                   owningListID=int(mid),
                                                   creator=creator)
                data = {"code": 0, "msg": "保存成功"}
            except Exception as e:
                print e
                data = {"code": -1, "msg": "保存失败"}
        else:
            try:

                id1 = int(reqdata["apiId"])
                pid = projectList.objects.get(projectName=projectName).id
                modid = moduleList.objects.get(
                    owningListID=pid, moduleName=moduleName).id  #项目名不能重复可直接取id
                # hoststr = hostTags.objects.filter(qa=host).values("id")
                # hostid = hoststr[0]["id"]
                hoststr = apiInfoTable.objects.filter(apiID=id1).values("host")
                hostid = hoststr[0]["host"]
                hostTags.objects.filter(id=int(hostid)).update(qa=host)
                batchUntils.getHost(hostid, "QA")
                apiInfoTable.objects.filter(apiID=id1).update(
                    apiName=caseName,
                    method=methods,
                    url=url,
                    headers=headers,
                    assertinfo=assertData,
                    body=send_body,
                    owningListID=int(modid))
            except Exception as e:
                data = {
                    "code": -1,
                    "msg": "更新失败" + str(e),
                }
                return JsonResponse(data)
            data = {"code": 0, "msg": "更新成功"}
        return JsonResponse(data, safe=False)