Ejemplo n.º 1
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     ordertype_id = serializer.data['ordertype']
     # 获取下一步骤的审批组
     approver_group_id = AutoOrderStep.objects.get(
         Q(ordertype_id=ordertype_id), Q(step_id=2)).approver_group_id
     autoorder_id = serializer.data['id']
     autoorder_operator = serializer.data['operator']
     AutoOrder.objects.filter(id=autoorder_id).update(
         approver_group_id=approver_group_id)
     # 获取下一步骤审批组相关用户发送邮件通知
     try:
         mailtolist = []
         userlist = ApprovalGroup.objects.get(
             Q(id=approver_group_id)).users.all()
         for user in userlist:
             mailtolist.append(user.email)
         maildata = json.loads(serializer.data['content'])
         send_mail(mailtolist, 4, maildata)
     except:
         pass
     # 插入创建工单步骤记录
     AutoOrderState.objects.create(autoorder_id_id=autoorder_id,
                                   step=1,
                                   action='start',
                                   operator=autoorder_operator)
     return Response(serializer.data,
                     status=status.HTTP_201_CREATED,
                     headers=headers)
Ejemplo n.º 2
0
def process_tennis_video(args):
    send_confirmation_mail(args['email'], args['url'])
    id = get_tennis_highlights(args['url'])
    file_link = f'./results/run_{id}/output.mp4'
    zip_link = f'./results/run_{id}/top10clips.zip'
    public_url, zip_url = copy_to_bucket(id, file_link, zip_link, 'tennis')
    send_mail(args['email'], public_url, zip_url)
Ejemplo n.º 3
0
def run_system_cmd(cmd, desc):
    if subprocess.call(cmd, shell=True) != 0:
        subprocess.call(config.COMMAND_START, shell=True)
        msg = f'An error occurred while {desc}'
        send_mail(msg, 'error')
        write_log(msg)
        raise SystemExit(1)
Ejemplo n.º 4
0
    def post(request):
        params = request.POST
        vp = ValidateParams(params, ["email", "password", "ic", "username"])
        if not vp.check():
            return JsonResponse(dict(code=1004, message=vp.error_message))

        # 检查invite code是否可用
        invite_code = vp.args.ic
        invite_code_qs = BzActiveCode.objects.filter(user_id=0,
                                                     code=invite_code).first()
        if not invite_code_qs:
            return JsonResponse(dict(code=1004, message="邀请码无效!"))

        # 检查用户名或邮箱是否被注册
        qs = BzUser.objects.filter(
            Q(username=vp.args.username) | Q(email=vp.args.email)).first()
        if qs:
            return JsonResponse(dict(code=1004, message="用户名或邮箱已被注册!"))

        # 检查用户名和邮箱是否合法
        if "|" in vp.args.username or "|" in vp.args.email:
            return JsonResponse(dict(code=1004, message="用户名或邮箱中含有非法字符"))

        # 插入用户数据
        new_user = BzUser()
        new_user.username = vp.args.username
        new_user.email = vp.args.email
        new_user.token = make_random_string(32)
        new_user.status = 1
        new_user.role = 1
        new_user.set_password(vp.args.password)
        new_user.save()
        # 更新邀请码的user_id
        invite_code_qs.user_id = new_user.id
        invite_code_qs.used_time = datetime.datetime.now()
        invite_code_qs.save()

        # 发送邮件
        raw_info = "".join(["{0}".format(new_user.id), "|", new_user.email])
        info = base64.b32encode(raw_info.encode("ascii")).decode("utf-8")
        raw_sign = "".join([new_user.token, new_user.username, new_user.email])
        sign = hashlib.md5(raw_sign.encode("ascii")).hexdigest()
        active_link = request.build_absolute_uri(
            reverse("validate_email")) + "?info={info}&sign={sign}"
        active_link = active_link.format(info=info, sign=sign)
        logger.debug(active_link)
        send_mail(
            "[白泽] 请激活您的白泽账户",
            "您的激活链接为: {url}".format(url=active_link),
            '*****@*****.**',
            [vp.args.email],
            fail_silently=False,
            async_flag=True,
        )

        # 返回成功信息
        return JsonResponse(dict(code=1001, message="注册成功,请到邮箱激活您的账户."))
Ejemplo n.º 5
0
def get_md5_from_file(file):
    cmd = f'su - {config.CLIENT_USER} -c "md5sum {file}"'
    if subprocess.call(cmd, shell=True) == 0:
        res = str(subprocess.check_output(cmd, shell=True))
        return res.split(' ')[0]
    else:
        msg = 'An error occurred while getting the MD5'
        write_log(msg)
        send_mail(msg, 'error')
Ejemplo n.º 6
0
def send_mail_task(receiver, subject, message):
    """
    Send an e-mail.

    :param receiver: E-mail address of the visitor
    :type receiver: str
    :param subject: Subject of the E-mail
    :type receiver: str
    :param message: E-mail message
    :type message: str
    :return: None
    """

    send_mail(receiver, subject, message)

    return None
Ejemplo n.º 7
0
 def post(self, request, format=None):
     mailtolist = []
     testmail = request.data['testmail']
     mailtolist.append(testmail)
     results = send_mail(mailtolist, 3)
     if (results):
         send_stat = 'sucess'
     else:
         send_stat = 'fail'
     return Response({'status': send_stat}, status=status.HTTP_200_OK)
Ejemplo n.º 8
0
def send_backup(backup_path, backup_name):
    server_confidential_data = f'-p {config.BACKUP_SERVER_PORT} {config.BACKUP_SERVER_USER}@{config.BACKUP_SERVER_HOST}'
    server_path = config.BACKUP_MOUNT_SERVER_PATH
    mount_path = config.BACKUP_MOUNT_CLIENT_PATH
    mount_cmd = f'sshfs {server_confidential_data}:/{server_path} {mount_path}'
    copy_cmd = f'cp {backup_path} {mount_path}/{backup_name}'

    original_md5 = utils.get_md5_from_file(backup_path)
    run_system_cmd(f'su - {config.CLIENT_USER} -c "{mount_cmd}"',
                   'mounting the SSHFS')
    run_system_cmd(f'su - {config.CLIENT_USER} -c "{copy_cmd}"',
                   'coping the backup to the server')
    returned_md5 = utils.get_md5_from_file(f'{mount_path}/{backup_name}')
    run_system_cmd(f'umount {mount_path}', 'unmounting the SSHFS')

    if original_md5 == returned_md5:
        if config.SEND_FINAL_EMAIL:
            send_mail(config.MAIL_BODY, 'success')
    else:
        error_text = 'The checksums of the backups do not match'
        utils.write_log(error_text)
        send_mail(error_text, 'error')
Ejemplo n.º 9
0
 def make_error(err):
     message = f'An error occurred while {err}'
     write_log(message)
     send_mail(message, 'error')
Ejemplo n.º 10
0
def run_test():
    # 解析测试数据excel表
    pe = ParseExcel()
    pe.loadWorkBook(r'%s' % test_data_file_path)
    # 获取接口表数据
    apisheet_obj = pe.getSheetByName('API')
    # 获取是否执行列
    apisheet_isExecute = pe.getCol(apisheet_obj, API_isExecute)
    # print(apisheet_isExecute)

    # 统计总数
    execute_case_no = 0
    faild_case_no = 0
    success_case_no = 0

    # 生成报告数据
    test_results_for_html_report = []

    # info("-----------------执行api--------------------")
    for idx, cell in enumerate(apisheet_isExecute[1:],
                               2):  # 从第2行开始,遍历要是否执行列,设置idx从2开始
        # 接口需要执行的接口
        if cell.value and cell.value.strip().lower() == 'y':
            # 需要执行的接口所在的行对象
            apisheet_rowObj = pe.getRow(apisheet_obj, idx)
            # api名称
            apisheet_apiName = apisheet_rowObj[API_apiName - 1].value
            apisheet_requestPath = apisheet_rowObj[API_requestPath - 1].value
            apisheet_requestMethod = apisheet_rowObj[API_requestMethod -
                                                     1].value
            apisheet_paramsType = apisheet_rowObj[API_paramsType - 1].value
            apisheet_headers = apisheet_rowObj[API_headers - 1].value
            apisheet_cookies = apisheet_rowObj[API_cookies - 1].value

            info("---------------执行api:%s------------------" %
                 apisheet_apiName)

            # 测试用例表名
            apisheet_caseSheetName = apisheet_rowObj[API_caseSheetName -
                                                     1].value

            # 处理api请求url
            apisheet_requestUrl = get_url(apisheet_requestPath)

            # 统计每个api的执行结果
            api_case_no = 0
            api_case_success_no = 0
            api_case_fail_no = 0

            info("------------------执行用例:%s-----------------" %
                 apisheet_caseSheetName)
            # 获取测试用例表对象
            casesheet_obj = pe.getSheetByName(apisheet_caseSheetName)
            # 获取是否需要执行的用例列对象
            casesheet_isExecute = pe.getCol(casesheet_obj, CASE_isExecute)
            for c_idx, c_cell in enumerate(casesheet_isExecute[1:], 2):
                # 用例是否执行
                if c_cell.value and c_cell.value.strip().lower() == 'y':

                    execute_case_no += 1
                    api_case_no += 1

                    # 获取要执行的用例表的行对象
                    casesheet_rowObj = pe.getRow(casesheet_obj, c_idx)

                    casesheet_caseName = casesheet_rowObj[CASE_caseName -
                                                          1].value
                    casesheet_requestData = casesheet_rowObj[CASE_requestData -
                                                             1].value
                    # 依赖字段
                    casesheet_dependApiNoCaseNo = casesheet_rowObj[
                        CASE_dependApiNoCaseNo - 1].value
                    casesheet_dependRequestDataFields = casesheet_rowObj[
                        CASE_dependRequestDataFields - 1].value
                    casesheet_dependResponseDataFields = casesheet_rowObj[
                        CASE_dependResponseDataFields - 1].value
                    casesheet_dependStoreRequestDataFields = casesheet_rowObj[
                        CASE_dependStoreRequestDataFields - 1].value
                    casesheet_dependStoreResponseDataFields = casesheet_rowObj[
                        CASE_dependStoreResponseDataFields - 1].value

                    casesheet_storeDataFields = casesheet_rowObj[
                        CASE_storeDataFields - 1].value
                    casesheet_assertData = casesheet_rowObj[CASE_assertData -
                                                            1].value

                    # info("----------------------请求前数据处理-----------------------")

                    # 处理url中的参数
                    if '{' in apisheet_requestUrl:
                        apisheet_requestUrl = handle_url_params(
                            apisheet_requestUrl, eval(casesheet_requestData))

                    # 获取headers
                    if apisheet_headers and apisheet_headers.strip().lower(
                    ) == 'y':
                        apisheet_headers = get_headers()

                    # 获取cookies
                    if apisheet_cookies and apisheet_cookies.strip().lower(
                    ) == 'y':
                        apisheet_cookies = get_cookies()

                    # 处理唯一数
                    if "${unique" in casesheet_requestData:
                        casesheet_requestData = handle_unique_num(
                            casesheet_requestData)

                    # 处理加密数据
                    if "${encrypt" in casesheet_requestData:
                        casesheet_requestData = handler_encrypt(
                            casesheet_requestData)

                    # 处理依赖数据
                    if casesheet_dependApiNoCaseNo:
                        casesheet_requestData = get_request_data(
                            pe, apisheet_obj, casesheet_dependApiNoCaseNo,
                            casesheet_requestData,
                            casesheet_dependRequestDataFields,
                            casesheet_dependResponseDataFields,
                            casesheet_dependStoreRequestDataFields,
                            casesheet_dependStoreResponseDataFields)

                    if not isinstance(casesheet_requestData, dict):
                        casesheet_requestData = eval(casesheet_requestData)

                    info("----------------------发送请求-----------------------")
                    info("请求 url:%s" % apisheet_requestUrl)
                    info("请求 方法:%s" % apisheet_requestMethod)
                    info("请求参数类型:%s" % apisheet_paramsType)
                    info("请求数据:%s" % casesheet_requestData)
                    info("请求头数据:%s" % apisheet_headers)
                    info("请求cookies数据:%s" % apisheet_cookies)

                    httpc = HttpClient()

                    # 请求计时开始
                    start_time = time.time()

                    # 发送请求
                    response = httpc.request(
                        requestUrl=apisheet_requestUrl,
                        requestMethod=apisheet_requestMethod,
                        paramsType=apisheet_paramsType,
                        requestData=casesheet_requestData,
                        headers=apisheet_headers,
                        cookies=apisheet_cookies)
                    # 耗时
                    escape_time = time.time() - start_time

                    info("响应结果:%s" % response)

                    # 请求成功
                    if response.status_code == 200:
                        info("------------------处理响应数据-------------------")
                        # 响应数据
                        casesheet_responseData = response.json()

                        response_content = response.content.decode('utf-8')

                        info("响应数据:%s" % response.content.decode('utf-8'))

                        # 存储依赖数据
                        if casesheet_storeDataFields:
                            store_result = store_data(
                                pe, casesheet_obj, c_idx,
                                eval(casesheet_storeDataFields),
                                casesheet_requestData, response)
                            info("存储依赖数据执行结果:%s" % store_result)

                        # 比对结果
                        error_key = {}
                        if casesheet_assertData:
                            error_key = assert_result(
                                casesheet_responseData,
                                eval(casesheet_assertData))

                        info("断言结果error_key:%s" % error_key)

                        if error_key:
                            faild_case_no += 1
                            api_case_fail_no += 1
                            test_results_for_html_report.append(
                                (apisheet_requestUrl, casesheet_requestData,
                                 response_content, int(escape_time * 1000),
                                 casesheet_assertData, "失败"))
                        else:
                            success_case_no += 1
                            api_case_success_no += 1
                            test_results_for_html_report.append(
                                (apisheet_requestUrl, casesheet_requestData,
                                 response_content, int(escape_time * 1000),
                                 casesheet_assertData, "成功"))

                        # 写测试结果
                        write_result(pe, casesheet_obj, casesheet_responseData,
                                     error_key, c_idx, escape_time)
                    else:
                        info('响应失败,响应状态码:%s' % response.status_code)
                        clear_result(pe, casesheet_obj, row_no=c_idx)
                else:
                    clear_result(pe, casesheet_obj, row_no=c_idx)
                    info('case:%s 被忽略执行' % casesheet_caseName)

            # 写接口的执行结果
            write_api_result(pe, apisheet_obj, idx, api_case_no,
                             api_case_success_no, api_case_fail_no)

        else:
            #清空忽略执行的接口结果
            clear_api_result(pe, apisheet_obj, idx)
            info('api:%s 被忽略执行' % apisheet_apiName)

    # 写报告
    info("-------------------生成报告--------------------")
    report_name = get_report_name()
    report_html(data=test_results_for_html_report, html_name=report_name)

    time.sleep(3)

    # 发送邮件
    info("-------------------发送邮件--------------------")
    send_file = report_name + '.html'
    send_content="本次自动化测试共执行用例数:%s,成功数:%s,失败数:%s,成功率:%.2f%%"\
                 %(execute_case_no,success_case_no,faild_case_no,success_case_no/execute_case_no*100)
    send_mail(send_file, send_content)
Ejemplo n.º 11
0
for req in weather_urls:
    service = req["service"]

    try:
        print("Fetching " + req["city"] + " from service " + req["service"])

        if service == "wetter_com":
            data = wetter_com.fetch_city(req["url"])
        elif service == "wetter_de":
            data = wetter_de.fetch_city(req["url"])
        elif service == "wetter_online":
            data = wetter_online.fetch_city(req["url"])
        elif service == "weather_com":
            data = weather_com.fetch_city(req["url"])

        print("Fetched successfully")

        write_data_to_db(req, data)
        print("Saved to database")

        print("")
    except:
        print("Error fetching " + req["city"] + " from service " +
              req["service"])
        send_mail(
            "Scraping Error", "Error fetching " + req["city"] +
            " from service " + req["service"])
        print("")

    time.sleep(1)
Ejemplo n.º 12
0
Archivo: mori.py Proyecto: zxjlm/Mori
def main():
    """
    Just main
    Returns:

    """

    version_string = (
        f"%(prog)s {__version__} \n "
        f"requests:  {requests.__version__} \n"
        f"Python:  {platform.python_version()} \n"
    )

    parser = ArgumentParser(
        formatter_class=RawDescriptionHelpFormatter,
        description=f"{module_name} " f"(Version {__version__})",
    )
    parser.add_argument(
        "--version",
        action="version",
        version=version_string,
        help="Display version information and dependencies.",
    )
    parser.add_argument(
        "--generate-config",
        "-g",
        default=False,
        action="store_true",
        dest="generate_config",
        help="It will guide to generate a config.py for this project.",
    )
    parser.add_argument(
        "--verbose",
        "-v",
        "-d",
        "--debug",
        action="store_true",
        dest="verbose",
        default=False,
        help="Display extra debugging information and metrics.",
    )
    parser.add_argument(
        "--xls",
        action="store_true",
        dest="xls",
        default=False,
        help="Create .xls File.(Microsoft Excel file format)",
    )
    parser.add_argument(
        "--show-all-site",
        action="store_true",
        dest="show_site_list",
        default=False,
        help="Show all information of the apis in files.",
    )
    parser.add_argument(
        "--json",
        "-j",
        metavar="JSON_FILES",
        dest="json_files",
        type=str,
        nargs="+",
        default=None,
        help="Load data from a local JSON file.Accept plural " "files.",
    )
    parser.add_argument(
        "--email",
        "-e",
        metavar="EMAIL_ADDRESS_LIST",
        dest="emails",
        type=str,
        nargs="*",
        default="not send",
        help="Send email to mailboxes. You can order the "
        "addresses in cmd argument, default is "
        "in the file 'config.py'.",
    )
    parser.add_argument(
        "--print-invalid",
        action="store_false",
        dest="print_invalid",
        default=False,
        help="Output api(s) that was invalid.",
    )
    parser.add_argument(
        "--no-proxy",
        default=True,
        action="store_false",
        dest="use_proxy",
        help="Use proxy.Proxy should define in config.py",
    )
    parser.add_argument(
        "--timeout",
        action="store",
        metavar="TIMEOUT",
        dest="timeout",
        type=timeout_check,
        default=None,
        help="Time (in seconds) to wait for response to "
        "requests. "
        "Default timeout is 35s. "
        "A longer timeout will be more likely to "
        "get results from slow sites. "
        "On the other hand, this may cause a long "
        "delay to gather all results.",
    )

    args = parser.parse_args()

    if args.generate_config:
        ConfigHandler().main()
        return

    if not ConfigHandler.check_config_file():
        return

    file_path_l = args.json_files or ["./apis.json"]
    apis = []

    for file_path in file_path_l:
        with open(file_path, "r", encoding="utf-8") as f:
            apis_sub = json.load(f)
        console.print(f"[green] read file {file_path} success~")
        data_render(apis_sub)
        apis += apis_sub

    if args.show_site_list:
        keys_to_show = ["name", "url", "data"]
        apis_to_show = list(
            map(
                lambda api: {
                    key: value for key, value in api.items() if key in keys_to_show
                },
                apis,
            )
        )
        console.print(apis_to_show)

    else:
        r = r"""
             __  __               _   _  __        _
            |  \/  |  ___   _ __ (_) | |/ /  ___  | | __  ___   _ __   ___
            | |\/| | / _ \ | '__|| | | ' /  / _ \ | |/ / / _ \ | '__| / _ \
            | |  | || (_) || |   | | | . \ | (_) ||   < | (_) || |   | (_) |
            |_|  |_| \___/ |_|   |_| |_|\_\ \___/ |_|\_\ \___/ |_|    \___/
            """
        print(r)

        # start = time.perf_counter()
        # for _ in range(20):
        results = mori(
            site_data_l=apis,
            timeout=args.timeout or 35,
            use_proxy=args.use_proxy,
            verbose=args.verbose,
            print_invalid=args.print_invalid,
        )
        # use_time = time.perf_counter() - start
        # print('total_use_time:{}'.format(use_time))

        if args.xls or isinstance(args.emails, list):
            for i, result in enumerate(results):
                results[i]["check_results"] = "\n".join(
                    [
                        f"{key} : {value}"
                        for key, value in result["check_results"].items()
                    ]
                )

            repo = Reporter(
                [
                    "name",
                    "url",
                    "base_url",
                    "status_code",
                    "time(s)",
                    "check_result",
                    "check_results",
                    "error_text",
                    "remark",
                    "resp_text",
                ],
                results,
            )

            if args.xls:
                console.print("[cyan]now generating report...")

                repo.processor()

                console.print("[green]mission completed")

            if isinstance(args.emails, list):
                try:
                    import config
                except Exception as _e:
                    console.print(
                        "can`t get config.py file, please read README.md, "
                        "search keyword [red]config.py",
                        _e,
                    )
                    return

                console.print("[cyan]now sending email...")

                if args.emails:
                    receivers = args.emails
                else:
                    receivers = config.RECEIVERS

                fs = repo.processor(is_stream=True)
                html = repo.generate_table()
                try:
                    send_mail(
                        receivers,
                        fs,
                        html,
                        config.MAIL_SUBJECT,
                        config.MAIL_HOST,
                        config.MAIL_USER,
                        config.MAIL_PASS,
                        getattr(config, "MAIL_PORT", 0),
                    )

                    console.print("[green]mission completed")
                except Exception as _e:
                    console.print(f"[red]mission failed,{_e}")
Ejemplo n.º 13
0
    utils.create_folders()
    run_system_cmd(config.COMMAND_STOP,
                   'stopping the application and the DBMS')
    run_system_cmd(f'cp -r {config.DATA_DIR} {config.TEMP_DIR}',
                   'copying the file')
    run_system_cmd(config.COMMAND_START,
                   'starting the application and the DBMS')

    archive_name = f'{utils.current_day}_{utils.current_month}_{utils.current_year}_{config.BACKUP_NAME}.tar.gz'
    backup_path = os.path.join(config.BACKUP_DIR, 'daily',
                               f'{archive_name}.gpg')
    change_owner_cmd = f'chown -R {config.CLIENT_USER}:{config.CLIENT_USER} {config.TEMP_DIR}'
    make_archive_cmd = f'su - {config.CLIENT_USER} -c "cd {config.TEMP_DIR} && tar -zcvf {archive_name} *"'
    make_pgp_cmd = f'gpg -e -r {config.CLIENT_USER} {archive_name}'
    copy_cmd = f'cp {config.TEMP_DIR}/{archive_name}.gpg {backup_path}'
    clean_cmd = f'rm -rf {config.TEMP_DIR}/*'
    create_gpg_cmd = f'su - {config.CLIENT_USER} -c "cd {config.TEMP_DIR} && {make_pgp_cmd} && {copy_cmd}" && {clean_cmd}'

    run_system_cmd(change_owner_cmd,
                   'changing the owner of the temp directory')
    run_system_cmd(make_archive_cmd, 'creating the archive')
    run_system_cmd(create_gpg_cmd, 'encrypting the archive')

    if config.SEND_BACKUP:
        send_backup(backup_path, f'{archive_name}.gpg')
    elif config.SEND_FINAL_EMAIL:
        send_mail(config.MAIL_BODY, 'success')

    sort_backups(backup_path)
Ejemplo n.º 14
0
# 自定义需要执行的用例
def custom():
    import test_case.test_a_login as a
    suite = unittest.TestSuite()
    suite.addTest(a.login_('test_001_success'))
    return suite


now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
filename = global_cls.report_path + now + '_result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
    stream=fp,
    title='ys_test_report',
    description='test_case_description')
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = global_cls.log_path
log_name = log_path + rq + '.log'

if __name__ == '__main__':
    # 执行全部用例
    alltestsuit = creatsuite()
    runner.run(alltestsuit)
    # 执行自定义用例
    # suite = custom()
    # runner.run(suite)
    fp.close()
    if global_cls.email_switch == 1:
        # 发送邮件方法
        send_mail.send_mail(filename, log_name)