PcPost.py 3.01 KB
from atdd.ATDDCase import ATDDCase
import hashlib
import json


class PcPost(ATDDCase):

    # 多企业登录
    def get_enterprise(self, data):
        result = data['result']

        if isinstance(result, list):
            user = result[1]
            if len(result) > 1:
                print("选择企业:", user['enterpriseInfo']['epName'])

            return user

    def do_login(self, host, mobile=None, pwd=None, domain=None):

        assert host is not None, "登录 host 必须设置"
        assert (domain is not None) or (
            mobile is not None and pwd is not None), 'domain 和 mobile/pwd 不能同时为空'

        if mobile is None or pwd is None:
            print('没有手机或密码,跳过登陆阶段')
            return

        check_mobile_pwd_url = self.host + '/comm/Public/Apicp/Admin/ChkMobilePwd?_identifier=common'
        print("校验登录账号密码", check_mobile_pwd_url)

        pwd = hashlib.md5(pwd.encode('utf-8')).hexdigest()
        response = self.post(
            check_mobile_pwd_url,
            {
                'mobile': mobile,
                'passwd': pwd
            }
        )

        assert response.status_code == 200, "检测手机和密码" + response.text

        data = response.json()
        assert data['errcode'] == 0, json.dumps(data, indent=2, sort_keys=False, ensure_ascii=False)

        user = self.get_enterprise(data)
        assert user is not None, '选择企业管理员失败'

        self.domain = user['enterpriseInfo']['epEnumber']
        logintoken = user['adminerInfo']['loginToken']

        login_url = self.host + '/' + self.domain +'/Public/Apicp/Admin/Login?_identifier=common'
        print("开始登录", login_url)
        response = self.post(
            login_url,
            {
                'loginToken': logintoken,
                'passwd': pwd
            }
        )

        assert response.status_code == 200, "登陆请求失败" + response.text

        data = response.json()
        assert data['errcode'] == 0, json.dumps(data, indent=2, sort_keys=False, ensure_ascii=False)
        return data

    # 后端接口模板-正常
    def PcPost(self, interfaceUrl, data=None, **options):

        self.interfaceUrl = interfaceUrl
        self.data = data

        url = self.host + '/' + self.domain + self.interfaceUrl
        r = self.post(url, self.data, **options)
        self.assertEqual(200, r.status_code)
        result = json.loads(r.text)
        errcode = result['errcode']
        self.assertEqual(0, errcode)
        return result

    # 后端接口模板-异常判断
    def PcPostAbnormal(self, interfaceUrl, data=None, code = None,**options):
        self.interfaceUrl = interfaceUrl
        self.data = data
        self.code = code

        url = self.host + '/' + self.domain + self.interfaceUrl
        r = self.post(url, self.data, **options)
        self.assertEqual(200, r.status_code)
        result = json.loads(r.text)
        errcode = result['errcode']
        self.assertEqual(code, errcode)
        return result