def check_sql_text(self,data): """ 插入的sql 语句检查是否有加限制条件 :param data: :return: """ sql_text_li = data.get('sql_text_li',False) delete_patterm = re.compile(r'delete from (.*)') update_patterm = re.compile(r'update (.*)') if sql_text_li: sql_text_li = eval(sql_text_li) for sql_text in sql_text_li: delete_res = delete_patterm.findall(sql_text) if delete_res: delete_res_patterm = re.compile('where') res = delete_res_patterm.findall(delete_res[0]) if not res: result = "The delete statement must be conditional." # 删除语句必出加上限制条件 return JsonResponse(code=status.HTTP_404_NOT_FOUND,data = {'res':result},msg = 'fail' ) else: update_res = update_patterm.findall(sql_text) if update_res: update_res_patterm = re.compile('where') res = update_res_patterm.findall(update_res[0]) if not res: result = "The update statement must be conditional." # update 语句必须加上现在条件 return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={'res': result}, msg='fail')
def get(self,request): """ 查询所有全局参数列表 :param request: :return: """ data = request.data try: page_size = request.GET.get('page_size',20) page = int(request.GET.get('page',1)) except Exception as ex: return JsonResponse(code=status.HTTP_400_BAD_REQUEST, msg=ex) qurieyset = GlobalData.objects.all() paginator = Paginator(qurieyset,page_size) total = paginator.num_pages try: obj = paginator.page(page_size) except PageNotAnInteger: obj = paginator.page(1) except EmptyPage: obj = paginator.page(paginator.num_pages) serializer = GlobalParamsSerializers(obj,many=True) return JsonResponse(code=status.HTTP_200_OK,data={ "data":serializer.data, "page":page, "total":total },msg="success")
def delete(self,request,pk): queryset = self.check_query_obj(pk) if not queryset: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res": "删除的数据不存在".format(pk)}, msg="fail") queryset.delete() return JsonResponse(code=status.HTTP_200_OK,msg="success")
def get(self,request): """ 获取全部用例执行步骤 :param request: :return: """ caseId = request.GET.get('caseId',False) try: page_size = int(request.GET.get('page_size',20)) page = int(request.GET.get('page',20)) except Exception as ex: return JsonResponse(code=status.HTTP_400_BAD_REQUEST, msg=ex) case_objects = CaseProcedure.objects.filter(caseId=caseId) paginator = Paginator(case_objects, page_size) total = paginator.num_pages try: obm = paginator.page(page) except PageNotAnInteger: obm = paginator.page(1) except EmptyPage: obm = paginator.page(paginator.num_pages) serializer = CaseProcedureSerializers(obm,many=True) return JsonResponse(code=status.HTTP_200_OK,data={ "data":serializer.data, "page": page, "total": total },msg="success")
def check_params(self, data): """ 执行用例,检查参数的有效性 :param data: projectId、caseId 是否有效 :return: """ projectId = data.get("projectId") try: project_queryset = Project.objects.get(id=projectId) except: return JsonResponse( code=status.HTTP_404_NOT_FOUND, data={"res": "projectId:{0} not exist".format(projectId)}, msg="fail") caseGroupId = data.get("caseGroupId") try: caseGroup_queryset = CaseGroup.objects.get(id=caseGroupId) except: return JsonResponse( code=status.HTTP_404_NOT_FOUND, data={"res": "caseGroupId:{0} not exist".format(caseGroupId)}, msg="fail") caseId = data.get("caseId") try: case_queryset = Case.objects.get(id=caseId) except: return JsonResponse( code=status.HTTP_404_NOT_FOUND, data={"res": "CaseId:{0} not exist".format(caseId)}, msg="fail")
def delete(self,request,pk): queryset = self.check_pk(pk) if not queryset: res = "pk: {0} not found".format(pk) return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res": res}, msg="fail") with transaction.atomic(): queryset.dalete() return JsonResponse(code=status.HTTP_200_OK, data= {}, msg='seccuss')
def put(self,request,pk): data = request.data queryset = self.check_query_obj(pk) if not queryset: return JsonResponse(code=status.HTTP_404_NOT_FOUND,data={"res":"没有id为:{}的数据".format(pk)},msg="fail") serialzers = GlobalParamsSerializers(queryset,data=data) if serialzers.is_valid(): serialzers.save() return JsonResponse(code=status.HTTP_200_OK,data=serialzers.data,msg="success") return JsonResponse(code=status.HTTP_502_BAD_GATEWAY,data=serialzers.errors,msg="fail")
def post(self,request): """ 添加全局参数接口 :param request: :return: """ data = request.data serializers = GlobalParamsSerializers(data=data) if serializers.is_valid(): serializers.save() return JsonResponse(code=status.HTTP_200_OK,data=serializers.data,msg='seccuss') return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,data=serializers.errors,msg='fail')
def put(self,request,pk): data = request.data queryset = self.check_pk(pk) if not queryset: res = "pk: {0} not found".format(pk) return JsonResponse(code = status.HTTP_404_NOT_FOUND, data = {"res": res}, msg = "fail") serializer = TearDownCaseSerializer(pk = pk,data = data) with transaction.atomic(): if serializer.is_valid(): serializer.save() return JsonResponse(code = status.HTTP_200_OK,data =serializer.data,msg = 'seccuss') return JsonResponse(code = status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete(self,request,pk): """ 删除用例组接口 :param request: :param pk: :return: """ pk_obj = self.get_objects(pk) if not pk_obj: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res: not find pk:{0}".format(pk)},msg="fail") with transaction.atomic(): pk_obj.delete() return JsonResponse(code=status.HTTP_200_OK,msg="success")
def post(self,request): data = request.data res = self.check_params(data) if res: return res sql_res = self.check_sql_text(data) if sql_res: return sql_res serializer = TearDownCaseSerializer(data = data) if serializer.is_valid(): serializer.save() return JsonResponse(code=status.HTTP_200_OK,data=serializer.data,msg="seccuss") return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,data=serializer.errors,msg="fail")
def get(self,request,pk): queryset = self.check_pk(pk) if not queryset: res = "pk: {0} not found".format(pk) return JsonResponse(code=status.HTTP_404_NOT_FOUND,data={"res":res},msg="fail") # try: # page_size = request.GET.get('page_size',20) # page_size = int(page_size) # except Exception as e: # return JsonResponse(code=status.HTTP_404_NOT_FOUND,data={'res':e},msg='fail') serializer = TearDownCaseSerializer(data=queryset) return JsonResponse(code=status.HTTP_200_OK,data=serializer.data,msg='seccuss')
def check_params(self,data): caseId = data.get('caseId',False) groupId = data.get('groupId',False) try: Case.objects.get(id=caseId) except: res = "caseId:{0} not found ".format(caseId) return JsonResponse(code=status.HTTP_404_NOT_FOUND,data={"res":res},msg='fail') try: CaseGroup.objects.get(id=groupId) except: res = "groupId:{0} not found ".format(groupId) return JsonResponse(code=status.HTTP_404_NOT_FOUND,data = {'res':res} ,msg='fail')
def post(self,request): data = request.data serializers = ApiParamsSerializer(data=data) with transaction.atomic(): if serializers.is_valid(): api_id = data.get("api") api_id = self.check_api_id(api_id) if not api_id: data = {'res':'api id not is exist'} return JsonResponse(code=status.HTTP_404_NOT_FOUND,data=data,msg="fail") serializers.save() return JsonResponse(code=status.HTTP_200_OK,msg='seccuss',data=serializers.data) return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,msg='fail',data=serializers.errors)
def parameter_check(self, data): user_name = data.get('username', False) password = data.get('password', False) if not user_name or not password: return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data={'res': "账号密码错误"}, msg='fail') else: if not isinstance(user_name, str) or not isinstance(password, str): return JsonResponse( code=status.HTTP_500_INTERNAL_SERVER_ERROR, data={'res': 'username,password must be str'}, msg='fail', )
def get(self,request,caseId,seqId): """ 获取单个用例接口数据 :param request: :param caseId: :param seqId: :return: """ if not caseId: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={'res': 'caseId not found'}, msg="fail") if not seqId: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={'res': 'seqId not found'}, msg="fail") res = get_apiinfo_data(caseId,seqId) return res
def delete(self,request,pk): """ 删除用例操作步骤接口 :param request: :param pk: 操作步骤id :return: """ try: case_datails = self.get_object(pk) if not case_datails: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res: not find pk:{0}".format(pk)},msg="fail") case_datails.delete() return JsonResponse(code=status.HTTP_200_OK,msg="success") except: return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,msg="未知错误")
def delete(self, request, pk): """ 删除项目接口 :param request: 请求 :param pk: 项目id :return: """ try: project_objects = self.get_objects(pk) with transaction.atomic(): project_objects.delete() return JsonResponse(code=status.HTTP_200_OK, msg="success") except: return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, msg="未知错误")
def get_apiinfo_data(caseId,seqId=None): caseId_res = check_caseId(caseId) if caseId_res: return caseId_res if seqId is not None: seq_res = get_check_seq(caseId,seqId) if seq_res: return seq_res apiinfo_queryset = ApiInfo.objects.filter(caseId=caseId,seqId=seqId) headers_queryset = ApiHeaders.objects.filter(caseId=caseId,seqId=seqId) params_queryset = ApiParams.objects.filter(caseId=caseId,seqId=seqId) api_info = model_to_dict(apiinfo_queryset[0]) if not headers_queryset: headers = {} else: headers = model_to_dict(headers_queryset[0]) if not params_queryset: params = [] else: params = [] for param_queryset in params_queryset: params.append(model_to_dict(param_queryset)) data = {"apiinfo":api_info,"headers":headers,"params":params} return JsonResponse(code=status.HTTP_200_OK,data=data,msg='seccuss')
def post(self,request): """ 创建用例组接口 :param request: :return: """ data = request.data serializer = CaseGroupSerializers(data=data) with transaction.atomic(): if serializer.is_valid(): create_time = str(time.time())[:10] data['createTime'] = create_time data['updateTime'] = create_time CaseGroup.objects.create(**data) return JsonResponse(code=status.HTTP_200_OK,data=serializer.data,msg="seccuss") return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,data=serializer.errors,msg="fail")
def put(self,request,pk): """ 修改用例操作步骤接口 :param request: :param pk: 步骤id :return: """ case_datails = self.get_object(pk) if not case_datails: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res: not find pk:{0}".format(pk)},msg="fail") serializer = CaseProcedureSerializers(case_datails, data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse(code=status.HTTP_200_OK, data=serializer.data,msg="success") return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, msg=serializer.errors)
def post(self, request): """ 创建用例接口 :param request: :return: """ data = request.data serializers = CaseSerializers(data=data) with transaction.atomic(): if serializers.is_valid(): serializers.save() return JsonResponse(code=status.HTTP_200_OK, data=data, msg="success") return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data=serializers.errors, msg='fail')
def check_caseGroupId(self, case_group_id): try: CaseGroup.objects.get(id=case_group_id) except: data = {'res': 'caseGroupId not exiest'} return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data=data, msg='fail')
def post(self,request): """ 添加用例操作步骤接口 :param request: 接口参数 :return: """ data = request.data serializer = CaseProcedureSerializers(data=data) if serializer.is_valid(): caseId = data.get("caseId") step = data.get("step") res = self.check_params(caseId,step) if res: return res serializer.save() return JsonResponse(code=status.HTTP_200_OK,data = serializer.data,msg='seccuss') return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,msg= serializer.errors)
def post(self,request): """ 添加测试url信息接口 :param request: headers、requestparams 必须为字典类型 :return: """ data = request.data serializers = AddInterfaceApiSerializer(data=data) with transaction.atomic(): if serializers.is_valid(): try: caseId = data.get('caseId') seqId = data.get('seqId') caseId_res = check_caseId(caseId) if caseId_res: return caseId_res seq_res = check_seq(caseId,seqId) if seq_res: return seq_res caseId, seqId, httpType, name, path, model, paramsType,headers, requestparams = [params for params in data.values()] if not isinstance(headers,dict) or not isinstance(requestparams,dict): data = {'res':'requestparams or headers object has no attribute items'} return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,data=data,msg='fail') create_time = str(time.time())[:10] # 添加APIInfo基础信息 ApiInfo.objects.create(caseId=caseId,seqId=seqId,httpType=httpType,name=name, path=path,model=model,paramsType=paramsType,createTime=create_time,updateTime=create_time) # 添加接口请求头 for name,value in headers.items(): ApiHeaders.objects.create(caseId=caseId,seqId=seqId,name=name,value=value) # 添加接口参数 for name,value in requestparams.items(): ApiParams.objects.create(caseId=caseId,seqId=seqId,name=name,value=value) return JsonResponse(code=status.HTTP_200_OK, data=data, msg="success") except Exception as e : res = str(e) return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data={'res':res}, msg='fail') return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data=serializers.errors, msg='fail')
def check_params(self,caseId,step): """ 用于检测一个用例的步骤是否已存在 :param caseId: :param step: :return: 查询结果 """ try: case_id_obj = Case.objects.get(id=caseId) except: return JsonResponse(code=status.HTTP_201_CREATED, data={ "res": "caseId is not exit" }, msg='fail') res = CaseProcedure.objects.filter(caseId=caseId,step=step) if res: return JsonResponse(code=status.HTTP_201_CREATED, data={ "res": "step is exit" }, msg='fail')
def get(self,request,pk): """ 获取用例单个操作步骤 :param request: :param pk: 操作步骤id :return: """ case_datails = self.get_object(pk) serializer = CaseProcedureSerializers(case_datails) return JsonResponse(code=status.HTTP_200_OK,data=serializer.data,msg="success")
def put(self,request,pk): """ 修改用例组接口 :param request: :param pk: :return: """ pk_obj = self.get_objects(pk) if not pk_obj: return JsonResponse(code=status.HTTP_404_NOT_FOUND, data={"res: not find pk:{0}".format(pk)},msg="fail") data = request.data seriailzer = CaseGroupSerializers(pk_obj,data=request.data) with transaction.atomic(): if seriailzer.is_valid(): update_time = str(time.time())[:10] data['updateTime'] = update_time CaseGroup.objects.filter(id=pk).update(**data) return JsonResponse(code=status.HTTP_200_OK,data=seriailzer.data,msg="seccuss") return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR,data=seriailzer.errors,msg="fail")
def post(self, request): """用户登录""" data = request.data result = self.parameter_check(data) if result: return result try: user_serializer = UserSerializers(data=data) if user_serializer.is_valid(): username = data.get('username', False) password = data.get('password', False) obj = User.objects.filter(username=username, password=password).first() if obj: user_id = obj.id # 里为了简单,应该是进行加密,再加上其他参数 token = str(time.time()) + username UserToken.objects.update_or_create( username=obj, defaults={'token': token}) token_queryset = UserToken.objects.filter( username=user_id).first() res = { "userId": user_id, "UserToken": "{0}".format(token_queryset.token) } return JsonResponse(code=status.HTTP_200_OK, msg='seccuss', data=res) else: return JsonResponse( code=status.HTTP_500_INTERNAL_SERVER_ERROR, msg='fila', data={"res": "账号或密码不存在"}) else: return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, msg='fail', data=user_serializer.errors) except Exception as e: res = {"res": "{0}".format(e)} return JsonResponse(code=status.HTTP_500_INTERNAL_SERVER_ERROR, data=res, msg='参数有误')
def get_check_seq(caseId,seqId): """ 获取用例接口的数据 :param caseId: :param seqId: :return: """ queryset = ApiInfo.objects.filter(caseId=caseId, seqId=seqId) if not queryset: data = {'res': '用例步骤不存在'} return JsonResponse(code=status.HTTP_502_BAD_GATEWAY, data=data, msg='fail')