# -*- encoding=utf8 -*-
__author__ = "liguangyu"

"""
case_tag:cmdc_api,cmdc登录接口新增雅培配置字段验证,32,32-1,sit,be
主数据平台:后台运营系统登录接口
"""

from common.common_func import commonFuc
import requests
import uuid
from common.verification_code_ocr import VerificationCodeOcr
import json
import os
import hashlib

module = "cmdc_32st"

username = commonFuc().get_business_data(module, "username1_be")
password = commonFuc().get_business_data(module, "password1_be")
# 获取唯一识别码
uuid_handle = uuid.uuid4()
# 获取验证码报文
param = {"uuid": uuid_handle}
# 获取登录页面获取验证码地址
code_url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "getVerifyCode_url")
# 发送请求获取验证码
result = requests.get(code_url, params=param)
# print(result.content)
# 获取当前文件路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
code_path = BASE_DIR + "/verifycode.png"
# 获取到验证码存入本地文件
with open(code_path, 'wb') as f:
    f.write(result.content)
# 识别并获取验证码
code = VerificationCodeOcr(code_path, "rainbow123","rainbow123").base64_api()
# 获取登录地址
cmdc_url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "do_login_url")
md = hashlib.md5(password.encode())  # 创建md5对象
EncryptPassword = md.hexdigest()  # 登录密码进行md5加密
print('加密后的密码', EncryptPassword)  # 加密后的密码
# 组装请求报文
request_body = {"userName": username, "password": EncryptPassword, "validateCode": code, "uuid": uuid_handle}


"""

 场景: 登录接口新增雅培配置字段验证
 用例名称：登录接口新增雅培配置字段验证
 输出：{"productCode":"%s"}
"""

# 发送请求
result = requests.post(cmdc_url, params=request_body)
# 获取接口响应时间
api_time = float(result.elapsed.total_seconds())

# 将接口响应时间添加至result
result = json.loads(result.content)
result["api_time"] = api_time

# 新增雅培配置字段
check_list = ["exceedSign", "examineSign", "materialSign", "purchaseSign"]
# 断言实际结果中是否包含预期结果的内容
# commonFuc().check_result(cmdc_access_token, result)
commonFuc().check_variable_exist(check_list, result["data"])
