Ejemplo n.º 1
0
def run_testsuite(request):
    """debug testsuite
    {
        name: str,
        body: dict
    }
    """
    body = request.data["body"]
    project = request.data["project"]
    name = request.data["name"]

    testcase_list = []
    config = None

    for test in body:
        test = loader.load_test(test, project=project)
        if "base_url" in test["request"].keys():
            config = test
            continue

        testcase_list.append(test)

    summary = loader.debug_api(testcase_list,
                               project,
                               name=name,
                               config=config)

    return Response(summary)
Ejemplo n.º 2
0
def run_api_tree(request):
    """run api by tree
    {
        project: int
        relation: list
        name: str
        async: bool
    }
    """
    # order by id default
    project = request.data['project']
    relation = request.data["relation"]
    back_async = request.data["async"]
    name = request.data["name"]

    testcase = []
    for relation_id in relation:
        api = models.API.objects.filter(
            project__id=project,
            relation=relation_id).order_by('id').values('body')
        for content in api:
            testcase.append(eval(content['body']))
    if back_async:
        loader.async_debug_api(testcase, project, name)
        summary = loader.TEST_NOT_EXISTS
        summary["msg"] = "接口运行中,请稍后查看报告"
    else:
        summary = loader.debug_api(testcase, project)

    return Response(summary)
Ejemplo n.º 3
0
def run_testsuite_pk(request, **kwargs):
    """run testsuite by pk
        {
            project: int,
            name: str,
            host: str
        }
    """
    pk = kwargs["pk"]

    test_list = models.CaseStep.objects. \
        filter(case__id=pk).order_by("step").values("body")

    project = request.query_params["project"]
    name = request.query_params["name"]
    host = request.query_params["host"]

    test_case = []
    config = None

    if host != "请选择":
        host = models.HostIP.objects.get(name=host, project=project).value.splitlines()

    for content in test_list:
        body = eval(content["body"])

        if "base_url" in body["request"].keys():
            config = eval(models.Config.objects.get(name=body["name"], project__id=project).body)
            continue

        test_case.append(parse_host(host, body))

    summary = loader.debug_api(test_case, project, name=name, config=parse_host(host, config))

    return Response(summary)
Ejemplo n.º 4
0
def run_testsuite_pk(request, **kwargs):
    """run testsuite by pk
        {
            project: int,
            name: str
        }
    """
    pk = kwargs["pk"]

    test_list = models.CaseStep.objects. \
        filter(case__id=pk).order_by("step").values("body")

    project = request.query_params["project"]
    name = request.query_params["name"]

    testcase_list = []
    config = None

    for content in test_list:
        body = eval(content["body"])

        if "base_url" in body["request"].keys():
            config = eval(
                models.Config.objects.get(name=body["name"],
                                          project__id=project).body)
            continue

        testcase_list.append(body)

    summary = loader.debug_api(testcase_list,
                               project,
                               name=name,
                               config=config)

    return Response(summary)
Ejemplo n.º 5
0
def run_testsuite(request):
    """debug testsuite
    {
        name: str,
        body: dict
        host: str
    }
    """
    body = request.data["body"]
    project = request.data["project"]
    name = request.data["name"]
    host = request.data["host"]

    test_case = []
    config = None

    if host != "请选择":
        host = models.HostIP.objects.get(name=host,
                                         project=project).value.splitlines()

    for test in body:
        test = loader.load_test(test, project=project)
        if "base_url" in test["request"].keys():
            config = test
            continue

        test_case.append(parse_host(host, test))

    summary = loader.debug_api(test_case,
                               project,
                               name=name,
                               config=parse_host(host, config),
                               user=request.user)

    return Response(summary)
Ejemplo n.º 6
0
def run_api(request):
    """ run api by body
    """
    name = request.data.pop('config')
    host = request.data.pop("host")
    api = Format(request.data)
    api.parse()

    config = None
    if name != '请选择':
        try:
            config = eval(
                models.Config.objects.get(name=name,
                                          project__id=api.project).body)

        except ObjectDoesNotExist:
            logger.error("指定配置文件不存在:{name}".format(name=name))
            return Response(config_err)

    if host != "请选择":
        host = models.HostIP.objects.get(
            name=host, project__id=api.project).value.splitlines()
        api.testcase = parse_host(host, api.testcase)

    summary = loader.debug_api(api.testcase,
                               api.project,
                               name=api.name,
                               config=parse_host(host, config),
                               user=request.user)

    return Response(summary)
Ejemplo n.º 7
0
def run_test(request):
    """debug single test
    {
        host: str
        body: dict
        project :int
        config: null or dict
    }
    """

    body = request.data["body"]
    config = request.data.get("config", None)
    project = request.data["project"]
    host = request.data["host"]

    if host != "请选择":
        host = models.HostIP.objects.get(name=host,
                                         project=project).value.splitlines()

    if config:
        config = eval(
            models.Config.objects.get(project=project,
                                      name=config["name"]).body)

    summary = loader.debug_api(parse_host(host, loader.load_test(body)),
                               project,
                               name=body.get('name', None),
                               config=parse_host(host, config),
                               user=request.user)

    return Response(summary)
Ejemplo n.º 8
0
def auto_run_testsuite_pk(**kwargs):
    """
    :param pk: int 用例组主键
    :param config: int 运行环境
    :param project_id: int 项目id
    :return:
    """

    pk = kwargs.get('pk')
    run_type = kwargs.get('run_type')
    project_id = kwargs.get('project_id')

    name = models.Case.objects.get(pk=pk).name

    # 通过主键获取单个用例
    test_list = models.CaseStep.objects. \
        filter(case__id=pk).order_by("step").values("body")

    # 把用例加入列表
    testcase_list = []
    for content in test_list:
        body = eval(content["body"])

        if "base_url" in body["request"].keys():
            config = eval(models.Config.objects.get(name=body["name"], project__id=project_id).body)
            continue
        testcase_list.append(body)

    summary = debug_api(testcase_list, project_id, name=name, config=config, save=False)

    save_summary(f'{name}_'+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), summary, project_id, type=3)

    ding_message = DingMessage(run_type)
    ding_message.send_ding_msg(summary)
Ejemplo n.º 9
0
def run_api(request):
    """ run api by body
    """
    api = Format(request.data)
    api.parse()

    summary = loader.debug_api(api.testcase, api.project)

    return Response(summary)
Ejemplo n.º 10
0
def run_api_pk(request, **kwargs):
    """run api by pk
    """
    api = models.API.objects.get(id=kwargs['pk'])
    testcase = eval(api.body)

    summary = loader.debug_api(testcase, api.project.id)

    return Response(summary)
Ejemplo n.º 11
0
def run_api_tree(request):
    """run api by tree
    {
        project: int
        relation: list
        name: str
        async: bool
        host: str
    }
    """
    # order by id default
    host = request.data["host"]
    project = request.data['project']
    relation = request.data["relation"]
    back_async = request.data["async"]
    name = request.data["name"]
    config = request.data["config"]

    config = None if config == '请选择' else eval(models.Config.objects.get(name=config, project__id=project).body)
    test_case = []

    temp_config = []
    temp_baseurl = ''
    if host != "请选择":
        host = models.HostIP.objects.get(name=host, project=project)
        host_info = json.loads(host.hostInfo)
        temp_config.extend(host_info["variables"])
        temp_baseurl = host.base_url if host.base_url else ''

    if config and host != "请选择":
        config.get("variables", []).extend(temp_config)
        if temp_baseurl:
            config["request"]["base_url"] = temp_baseurl
    if not config and host != "请选择":
        config = {
            "variables": temp_config,
            "request": {
                "base_url": temp_baseurl
            }
        }

    for relation_id in relation:
        api = models.API.objects.filter(project__id=project, relation=relation_id).order_by('id').values('body')
        for content in api:
            api = eval(content['body'])
            test_case.append(parse_host(host, api))

    if back_async:
        tasks.async_debug_api.delay(test_case, project, name, config=parse_host(host, config))
        summary = loader.TEST_NOT_EXISTS
        summary["msg"] = "接口运行中,请稍后查看报告"
    else:
        try:
            summary = loader.debug_api(test_case, project, config=parse_host(host, config))
        except Exception as e:
            return Response({'traceback': str(e)}, status=400)
    return Response(summary)
Ejemplo n.º 12
0
def run_api_pk(request, **kwargs):
    """run api by pk and config
    """
    api = models.API.objects.get(id=kwargs['pk'])
    testcase = eval(api.body)

    summary = loader.debug_api(testcase, request.query_params["config"])

    return Response(summary)
Ejemplo n.º 13
0
def run_api(request):
    """ run api by body
    """
    name = request.data.pop('config')
    host = request.data.pop("host")
    api = Format(request.data)
    api.parse()

    test_data = None
    if request.data["testDataExcel"] != '请选择' and request.data["testDataSheet"]:
        test_data = (request.data["testDataExcel"],
                     request.data["testDataSheet"])
    config = None
    if name != '请选择':
        try:
            config = eval(
                models.Config.objects.get(name=name,
                                          project__id=api.project).body)
        except ObjectDoesNotExist:
            logger.error("指定配置文件不存在:{name}".format(name=name))
            return Response(config_err)

    temp_config = []
    temp_baseurl = ''
    if host != "请选择":
        host = models.HostIP.objects.get(name=host, project=api.project)
        host_info = json.loads(host.hostInfo)
        temp_config.extend(host_info["variables"])
        temp_baseurl = host.base_url if host.base_url else ''

    if config and host != "请选择":
        config["variables"].extend(temp_config)
        if temp_baseurl:
            config["request"]["base_url"] = temp_baseurl
    if not config and host != "请选择":
        config = {
            "variables": temp_config,
            "request": {
                "base_url": temp_baseurl
            }
        }

    # if host != "请选择":
    #     try:
    #         host = models.HostIP.objects.get(name=host, project__id=api.project).value.splitlines()
    #         api.testcase = parse_host(host, api.testcase)
    #     except ObjectDoesNotExist:
    #         logger.error("指定域名不存在:{name}".format(name=host))
    try:
        summary = loader.debug_api(api.testcase,
                                   api.project,
                                   config=parse_host(host, config),
                                   test_data=test_data)
    except Exception as e:
        return Response({'traceback': str(e)}, status=400)
    return Response(summary)
Ejemplo n.º 14
0
def run_test(request):
    """debug single test
    {
        body: dict
    }
    """

    body = request.data["body"]
    summary = loader.debug_api(loader.load_test(body), request.data["project"])
    return Response(summary)
Ejemplo n.º 15
0
def run_api(request):
    """ run api by body and config
    """
    config = request.data.pop("config")
    api = Format(request.data)
    api.parse()

    summary = loader.debug_api(api.testcase, config, api.project)

    return Response(summary)
Ejemplo n.º 16
0
def async_debug_test(test_case, project, name, report_name, config, test_data):
    """异步执行testcase
    """
    summary = debug_api(test_case,
                        project,
                        name=name,
                        config=config,
                        save=False,
                        test_data=test_data)
    save_summary(report_name, summary, project)
Ejemplo n.º 17
0
def run_api_pk(request, **kwargs):
    """run api by pk and config
    """
    api = models.API.objects.get(id=kwargs['pk'])
    name = request.query_params["config"]
    config = None if name == '请选择' else eval(
        models.Config.objects.get(name=name).body)

    test_case = eval(api.body)
    summary = loader.debug_api(test_case, api.project.id, config=config)

    return Response(summary)
Ejemplo n.º 18
0
def run_api(request):
    """ run api by body
    """
    name = request.data.pop('config')
    api = Format(request.data)
    api.parse()

    config = None if name == '请选择' else eval(
        models.Config.objects.get(name=name).body)
    summary = loader.debug_api(api.testcase, api.project, config=config)

    return Response(summary)
Ejemplo n.º 19
0
def run_api_pk(request, **kwargs):
    """run api by pk and config
    """
    host = request.query_params["host"]
    api = models.API.objects.get(id=kwargs['pk'])
    name = request.query_params["config"]
    config = None if name == '请选择' else eval(
        models.Config.objects.get(name=name, project=api.project).body)
    test_case = eval(api.body)

    test_data = None
    if request.query_params["testDataExcel"] != '请选择' and request.query_params[
            "testDataSheet"]:
        test_data = (request.query_params["testDataExcel"],
                     request.query_params["testDataSheet"])

    temp_config = []
    temp_baseurl = ''
    if host != "请选择":
        host = models.HostIP.objects.get(name=host, project=api.project)
        host_info = json.loads(host.hostInfo)
        temp_config.extend(host_info["variables"])
        temp_baseurl = host.base_url if host.base_url else ''

    if config and host != "请选择":
        config["variables"].extend(temp_config)
        if temp_baseurl:
            config["request"]["base_url"] = temp_baseurl
    if not config and host != "请选择":
        config = {
            "variables": temp_config,
            "request": {
                "base_url": temp_baseurl
            }
        }
    # if host != "请选择":
    #     try:
    #         host = models.HostIP.objects.get(name=host, project=api.project).value.splitlines()
    #         test_case = parse_host(host, test_case)
    #     except ObjectDoesNotExist:
    #         logger.error("指定域名不存在:{name}".format(name=host))
    try:
        summary = loader.debug_api(test_case,
                                   api.project.id,
                                   config=parse_host(host, config),
                                   test_data=test_data)
    except Exception as e:
        return Response({'traceback': str(e)}, status=400)

    return Response(summary)
Ejemplo n.º 20
0
def run_testsuite(request):
    """debug testsuite
    {
        name: str,
        body: dict
        host: str
    }
    """
    body = request.data["body"]
    project = request.data["project"]
    name = request.data["name"]
    host = request.data["host"]

    test_case = []
    config = None

    timestamp = request.data.pop("timestamp")

    logger_file = log_file + '/' + str(timestamp) + '.log'

    if host != "请选择":
        host = models.HostIP.objects.get(name=host,
                                         project=project).value.splitlines()

    for test in body:
        test = loader.load_test(test, project=project)
        if "base_url" in test["request"].keys():
            config = test
            continue

        test_case.append(parse_host(host, test))

    try:
        summary = loader.debug_api(test_case,
                                   project,
                                   name=name,
                                   config=config,
                                   user=request.user,
                                   log_file=logger_file)
        if summary is None:
            with open(logger_file, 'r') as r:
                msg = r.readlines()
            summary = msg
            return Response({"error": summary})
        return Response(summary)
    except Exception as e:
        return Response({"error": "404"})
    finally:
        '''删除调试脚本的日志文件'''
        delfile(logger_file)
Ejemplo n.º 21
0
def run_test(request):
    """debug single test
    {
        host: str
        body: dict
        project :int
        config: null or dict
    }
    测试用例集 单个运行api
    """

    body = request.data["body"]
    config = request.data.get("config", None)
    project = request.data["project"]
    host = request.data["host"]
    timestamp = request.data.pop("timestamp")

    logger_file = log_file + '/' + str(timestamp) + '.log'
    if host != "请选择":
        host = models.HostIP.objects.get(name=host,
                                         project=project).value.splitlines()

    if config:

        config = eval(
            models.Config.objects.get(project=project,
                                      name=config["name"]).body)

    try:

        summary = loader.debug_api(parse_host(host, loader.load_test(body)),
                                   project,
                                   name=body.get('name', None),
                                   config=config,
                                   user=request.user,
                                   log_file=logger_file)

        if summary is None:
            with open(logger_file, 'r') as r:
                msg = r.readlines()
            summary = msg
            return Response({"error": summary})
        return Response(summary)
    except Exception as e:

        return Response({"error": "404"})
    finally:
        '''删除调试脚本的日志文件'''
        delfile(logger_file)
Ejemplo n.º 22
0
def run_api_tree(request):
    """run api by tree
    {
        project: int
        relation: list
        name: str
        async: bool
        host: str
    }
    """
    # order by id default
    host = request.data["host"]
    project = request.data['project']
    relation = request.data["relation"]
    back_async = request.data["async"]
    name = request.data["name"]
    config = request.data["config"]

    config = None if config == '请选择' else eval(
        models.Config.objects.get(name=config, project__id=project).body)
    test_case = []

    if host != "请选择":
        host = models.HostIP.objects.get(name=host,
                                         project=project).value.splitlines()

    for relation_id in relation:
        api = models.API.objects.filter(project__id=project,
                                        relation=relation_id,
                                        delete=0).order_by('id').values('body')
        for content in api:
            api = eval(content['body'])
            test_case.append(parse_host(host, api))

    if back_async:
        tasks.async_debug_api.delay(test_case,
                                    project,
                                    name,
                                    config=parse_host(host, config))
        summary = loader.TEST_NOT_EXISTS
        summary["msg"] = "接口运行中,请稍后查看报告"
    else:
        summary = loader.debug_api(test_case,
                                   project,
                                   name=f'批量运行{len(test_case)}条API',
                                   config=parse_host(host, config),
                                   user=request.user)

    return Response(summary)
Ejemplo n.º 23
0
def async_debug_api(test_case,
                    project,
                    name=None,
                    config=None,
                    save=False,
                    log_file=None):
    """异步执行api
    """
    summary = debug_api(test_case,
                        project,
                        name=f'批量运行{len(test_case)}条API',
                        config=config,
                        save=save,
                        log_file=log_file)

    save_summary(name, summary, project, api_type=1)
Ejemplo n.º 24
0
def run_api_pk(request, **kwargs):
    """run api by pk and config
    """
    host = request.query_params["host"]
    api = models.API.objects.get(id=kwargs['pk'])
    name = request.query_params["config"]
    config = None if name == '请选择' else eval(models.Config.objects.get(name=name, project=api.project).body)

    test_case = eval(api.body)
    if host != "请选择":
        host = models.HostIP.objects.get(name=host, project=api.project).value.splitlines()
        test_case = parse_host(host, test_case)

    summary = loader.debug_api(test_case, api.project.id, config=parse_host(host, config))

    return Response(summary)
Ejemplo n.º 25
0
def run_testsuite(request):
    """debug testsuite
    {
        name: str,
        config: int
        body: dict
    }
    """
    body = request.data["body"]

    testcase_list = []

    for test in body:
        testcase_list.append(loader.load_test(test))

    summary = loader.debug_api(testcase_list, request.data['config'])
    return Response(summary)
Ejemplo n.º 26
0
def run_api(request):
    """ run api by body
    """
    name = request.data.pop('config')
    host = request.data.pop("host")
    timestamp = request.data.pop("timestamp")

    api = Format(request.data)
    api.parse()

    logger_file = log_file + '/' + str(timestamp) + '.log'

    config = None
    if name != '请选择':
        try:
            config = eval(
                models.Config.objects.get(name=name,
                                          project__id=api.project).body)

        except ObjectDoesNotExist:
            logger.error("指定配置文件不存在:{name}".format(name=name))
            return Response(config_err)

    if host != "请选择":
        host = models.HostIP.objects.get(
            name=host, project__id=api.project).value.splitlines()
        api.testcase = parse_host(host, api.testcase)
    try:
        summary = loader.debug_api(api.testcase,
                                   api.project,
                                   name=api.name,
                                   config=parse_host(host, config),
                                   user=request.user,
                                   log_file=logger_file)
        if summary is None:
            with open(logger_file, 'r') as r:
                msg = r.readlines()
            summary = msg
            return Response({"error": summary})
        return Response(summary)
    except Exception as e:
        return Response({"error": "404"})
    finally:
        '''删除调试脚本的日志文件'''
        delfile(logger_file)
Ejemplo n.º 27
0
def run_testsuite_pk(request, **kwargs):
    """run testsuite by pk
        pk: int
        config: int
    """
    pk = kwargs["pk"]

    test_list = models.CaseStep.objects.\
        filter(case__id=pk).order_by("step").values("body")

    testcase_list = []

    for content in test_list:
        testcase_list.append(eval(content["body"]))

    summary = loader.debug_api(testcase_list, request.query_params["config"], request.query_params["project"])

    return Response(summary)
Ejemplo n.º 28
0
def run_api_tree(request):
    """run api by tree
    {
        project: int
        relation: int
        config: int
    }
    """
    # order by id default
    api = models.API.objects. \
        filter(project__id=request.data['project'], relation=request.data['relation']). \
        order_by('id').values('body')

    testcase = []
    for content in api:
        testcase.append(eval(content['body']))

    summary = loader.debug_api(testcase, request.data["config"])

    return Response(summary)
Ejemplo n.º 29
0
def run_api_pk(request, **kwargs):
    """run api by pk and config
    """
    host = request.query_params["host"]
    api = models.API.objects.get(id=kwargs['pk'])
    name = request.query_params["config"]
    timestamp = request.query_params["timestamp"]

    config = None if name == '请选择' else eval(
        models.Config.objects.get(name=name, project=api.project).body)

    logger_file = log_file + '/' + str(timestamp) + '.log'

    test_case = eval(api.body)
    if host == '请选择' and "base_url" not in test_case["request"].keys(
    ) and "http" not in test_case["request"]["url"]:
        return Response({"status": "500", "tips": "请完善请求URL"})
    if host != "请选择":
        host = models.HostIP.objects.get(
            name=host, project=api.project).value.splitlines()
        test_case = parse_host(host, test_case)

    try:
        summary = loader.debug_api(test_case,
                                   api.project.id,
                                   name=api.name,
                                   config=parse_host(host, config),
                                   user=request.user,
                                   log_file=logger_file)
        if summary is None:
            with open(logger_file, 'r') as r:
                msg = r.readlines()
            summary = msg
            return Response({"error": summary})
        return Response(summary)
    except Exception as e:
        return Response({"error": "404"})

    finally:
        '''删除调试脚本的日志文件'''
        delfile(logger_file)
Ejemplo n.º 30
0
def run_test(request):
    """debug single test
    {
        body: dict
        project :int
        config: null or dict
    }
    """

    body = request.data["body"]
    config = request.data.get("config", None)
    project = request.data["project"]

    if config:
        config = eval(
            models.Config.objects.get(project=project,
                                      name=config["name"]).body)

    summary = loader.debug_api(loader.load_test(body), project, config=config)

    return Response(summary)