def add_loan(member_id, token, title='借钱实现财富自由', amount=5000, loan_rate=12.0, loan_term=3, loan_date_type=1, bidding_days=5): data = { 'member_id': member_id, 'title': title, 'amount': amount, 'loan_rate': loan_rate, 'loan_term': loan_term, 'loan_date_type': loan_date_type, 'bidding_days': bidding_days, } url = settings.PROJECT_HOST + settings.INTERFACE['add'] logger.info('用户{}加标借钱{}'.format(member_id, amount)) headers = {'authorization': 'Bearer ' + token} headers.update(settings.CUSTOM_HEADERS['v2']) res = send_request(url=url, method='post', json=data, headers=headers).json() if res['code'] == 0: return res raise RuntimeError('用户加标失败,错误信息:{}'.format(res))
def login(mobile_phone, pwd): """ 登录 :param mobile_phone: :param pwd: :return: """ data = {'mobile_phone': mobile_phone, 'pwd': pwd} url = settings.PROJECT_HOST + settings.INTERFACE['login'] headers = settings.CUSTOM_HEADERS['v2'] logger.info('登录用户:{}'.format(data)) res = send_request(url, method='POST', json=data, headers=headers).json() if res['code'] == 0: return res['data'] raise RuntimeError('错误信息:{}'.format(res))
def register(mobile_phone, pwd, type_=1): """ 注册一个用户 :param mobile_phone: :param pwd: :param type_: 类型 1 普通用户 0 管理员账号 :return: """ data = {'mobile_phone': mobile_phone, 'pwd': pwd, 'type': type_} url = settings.PROJECT_HOST + settings.INTERFACE['register'] headers = settings.CUSTOM_HEADERS['v1'] res = send_request(url, method='POST', json=data, headers=headers).json() logger.info('注册用户: {}'.format(data)) if res['code'] == 0: return True raise RuntimeError('错误信息:{}'.format(res))
def recharge(member_id, amount, token): """ 充值 :param member_id: :param amount: :param token: :return: """ data = {'member_id': member_id, 'amount': amount} url = settings.PROJECT_HOST + settings.INTERFACE['recharge'] # 鉴权 headers = {'authorization': 'Bearer ' + token} headers.update(settings.CUSTOM_HEADERS['v2']) logger.info('给用户{member_id}充值{amount}'.format(**data)) res = send_request(url, method='POST', json=data, headers=headers).json() if res['code'] == 0: return res['data'] raise RuntimeError('错误信息:{}'.format(res))
def test_register(self, case): # 1. 测试数据 # request_data, expect_data # 判断一下是否需要生成手机号码 if '#phone#' in case['request_data']: # 需要进行手机号码替换 phone = generate_phone_num() logger.info('生成手机号码:{}'.format(phone)) case['request_data'] = case['request_data'].replace('#phone#', phone) request_data = json.loads(case['request_data']) expect_data = json.loads(case['expect_data']) # 2. 测试步骤 # headers = { # 'X-Lemonban-Media-Type': 'lemonban.v1' # } headers = settings.CUSTOM_HEADERS[self.auth_key] url = settings.PROJECT_HOST + settings.INTERFACE[case['interface']] res = send_request(url=url, method=case['method'], json=request_data, headers=headers).json() # 3. 断言 res_data = {'code': res['code'], 'msg': res['msg']} # self.assertEqual(expect_data['code'], res['code']) # self.assertEqual(expect_data['msg'], res['msg']) try: self.assertEqual(expect_data, res_data) except Exception as e: self.logger.exception('{}::测试失败'.format(case['title'])) self.logger.warning('请求数据是: {}'.format(request_data)) self.logger.warning('期望结果是:{}'.format(expect_data)) self.logger.warning('实际结果是:{}'.format(res_data)) raise e # 4. 看下是否要校验数据库? if case['sql']: # 校验数据库 # 用代码执行sql try: self.assertTrue(db.exist(case['sql'])) # self.assertEqual(True, db.exist()) except Exception as e: self.logger.exception('{}::数据库校验失败'.format(case['title'])) self.logger.warning('执行的sql是:{}'.format(case['sql'])) raise e