示例#1
0
def test_case_update(test_id):
    config_env = load_config()  # 读取配置
    result = ''
    result_old = {}
    url = "".join((config_env.TEST_DATA_BASE_URL, "api/v1/testcase/", str(test_id)))
    form = TestCaseCreateForm()

    if form.validate_on_submit():
        headers = dict()
        headers["Content-Type"] = "application/json"
        data = dict()
        data["progress"] = form.progress.data
        data["testcase_name"] = form.testcase_name.data
        data["testcase_level"] = form.testcase_level.data
        data["api_url"] = form.api_url.data
        data["api_description"] = form.api_description.data
        data["request_scheme"] = form.request_scheme.data
        data["request_type"] = form.request_type.data

        data["request_headers"] = form.request_headers.data
        data["request_data"] = form.request_data.data
        data["request_params"] = form.request_params.data

        data["check_filed"] = form.check_filed.data
        data["remark"] = form.remark.data
        data["last_success_response"] = form.last_success_response.data
        data["update_time"] = str(datetime.datetime.now())
        if data["request_headers"]:
            try:
                assert (isinstance(json.loads(data["request_headers"]), dict) == True)
            except:
                traceback.print_exc()
                result += u" [请求头部]"
        if data["request_params"]:
            try:
                assert (isinstance(json.loads(data["request_params"]), dict) == True)
            except:
                traceback.print_exc()
                result += u"  [请求参数]"

        if result:
            result += u' 必须是字典格式,请检查后再提交。'
            flash(result)
            return render_template("test_api/test_case_update.html", form=form, result_old=result_old)

        data = json.dumps(data)
        # update
        try:
            r = requests.put(url=url, headers=headers, data=data, timeout=10)
        except:
            traceback.print_exc()
        else:
            if "message" not in json.loads(r.content):
                result = u'更新成功'
                form.progress.data = ''
                form.testcase_name.data = ''
                form.testcase_level.data = ''
                form.api_url.data = ''
                form.api_description.data = ''
                form.request_scheme.data = ''
                form.request_type.data = ''
                form.request_headers.data = ''
                form.request_data.data = ''
                form.request_params.data = ''
                form.check_filed.data = ''
                form.remark.data = ''
                form.last_success_response.data = ''
            else:
                result = u'更新失败'
    # get
    try:
        r = requests.get(url=url, timeout=10)
    except:
        traceback.print_exc()
    else:
        result_old = json.loads(r.content)
        # 单引号,换行符,回车符
        replace_rule = [('\r', '\\r'), ('\n', '\\n'), ("'", "\\'")]
        for key in result_old:
            for i in replace_rule:
                if isinstance(result_old[key], unicode):
                    result_old[key] = result_old[key].replace(i[0], i[1])
    if result:
        flash(result)
    return render_template("test_api/test_case_update.html", form=form, result_old=result_old)
示例#2
0
def test_case_add():
    config_env = load_config()  # 读取配置
    form = TestCaseCreateForm()
    result = ''
    result_old = {}

    if "test_id" in request.args.keys():
        test_id = request.args["test_id"]
        url = "".join((config_env.TEST_DATA_BASE_URL, "api/v1/testcase/", str(test_id)))
        try:
            r = requests.get(url=url, timeout=10)
        except:
            traceback.print_exc()
        else:
            result_old = json.loads(r.content)
            replace_rule = [('\r', '\\r'), ('\n', '\\n'), ("'", "\\'")]
            for key in result_old:
                for i in replace_rule:
                    if isinstance(result_old[key], unicode):
                        result_old[key] = result_old[key].replace(i[0], i[1])

    if form.validate_on_submit():
        headers = dict()
        headers["Content-Type"] = "application/json"
        data = dict()
        data["progress"] = form.progress.data
        data["testcase_name"] = form.testcase_name.data
        data["testcase_level"] = form.testcase_level.data
        data["api_url"] = form.api_url.data
        data["api_description"] = form.api_description.data
        data["request_scheme"] = form.request_scheme.data
        data["request_type"] = form.request_type.data
        if form.request_headers.data:
            data["request_headers"] = form.request_headers.data
            try:
                assert (isinstance(json.loads(data["request_headers"]), dict) == True)
            except:
                traceback.print_exc()
                result += u" [请求头部]"

        if form.request_data.data:
            data["request_data"] = form.request_data.data
        if form.request_params.data:
            data["request_params"] = form.request_params.data
            try:
                assert (isinstance(json.loads(data["request_params"]), dict) == True)
            except:
                traceback.print_exc()
                result += u"  [请求参数]"

        if form.check_filed.data:
            data["check_filed"] = form.check_filed.data
        if form.remark.data:
            data["remark"] = form.remark.data
        if form.last_success_response.data:
            data["last_success_response"] = form.last_success_response.data.replace("", "  ")
        data["create_time"] = str(datetime.datetime.today())
        data["update_time"] = str(datetime.datetime.today())
        if result:
            result += u' 必须是字典格式,请检查后再提交。'
            flash(result)
            return render_template("test_api/test_case_add.html", form=form, result_old=result_old)

        data = json.dumps(data)
        url = "".join((config_env.TEST_DATA_BASE_URL, "api/v1/testcase"))
        try:
            r = requests.post(url=url, headers=headers, data=data, timeout=10)
        except:
            traceback.print_exc()
        else:
            if "message" not in json.loads(r.content):
                result = u'新增成功'
                form.progress.data = ''
                form.testcase_name.data = ''
                form.testcase_level.data = ''
                form.api_url.data = ''
                form.api_description.data = ''
                form.request_scheme.data = ''
                form.request_type.data = ''
                form.request_headers.data = ''
                form.request_data.data = ''
                form.request_params.data = ''
                form.check_filed.data = ''
                form.remark.data = ''
                return ("<script>alert('{}'); window.location.href='/test_api';</script> ".format(result))
            else:
                result = u'新增失败'

    if result:
        flash(result)
    return render_template("test_api/test_case_add.html", form=form, result_old=result_old)