def step_impl(context, webapp_user_name, channel_qrcode_name): #webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) channel_setting = bdd_util.get_channel_qrcode_setting(channel_qrcode_name) assert channel_setting is not None owner_id = channel_setting.owner_id is_new = False if webapp_user_name[0] == '-': # 表示此用户还不是会员,让其关注 owner = User.objects.get(id=owner_id) webapp_user_name = webapp_user_name[1:] context.execute_steps(u"When %s关注%s的公众号" % (webapp_user_name, owner.username)) is_new = True webapp_id = bdd_util.get_webapp_id_via_owner_id(owner_id) member = bdd_util.get_member_for(webapp_user_name, webapp_id) #user = UserFactory(username=webapp_user_name) #user = User.objects.get(id=owner_id) context.user_profile = UserProfile.objects.get(user_id=owner_id) ticket = channel_setting.ticket create_channel_qrcode_has_memeber( context.user_profile, member, ticket, is_new, )
def __has_ship_info(webapp_user_name, webapp_owner_name): webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member = bdd_util.get_member_for(webapp_user_name, webapp_id) webapp_user = WebAppUser.objects.get(member_id=member.id) if ShipInfo.objects.filter(webapp_user_id=webapp_user.id).count() > 0: return True else: return False
def step_impl(context, webapp_user_name): expected = json.loads(context.text) # profile = context.client.user.profile member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) actual_data = dict() actual_data['integral'] = str(member.integral) bdd_util.assert_dict(expected, actual_data)
def step_impl(context, user, webapp_user_name): json_data = json.loads(context.text) grade_name = json_data['member_rank'] webapp_id = bdd_util.get_webapp_id_for(user) member = bdd_util.get_member_for(webapp_user_name, webapp_id) grade = MemberGrade.objects.get(name=grade_name, webapp_id=context.webapp_id) data = {'type': 'grade', 'member_id': member.id, 'checked_ids': grade.id} context.client.post('/member/api/update_member_tag_or_grade/', data)
def step_impl(context, webapp_owner_name, webapp_user_name): webapp_user_member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) url = '/member/api/integral_logs/?design_mode=0&version=1&member_id=%d&count_per_page=10&page=1&enable_paginate=1' % webapp_user_member.id response = context.client.get(url) member_logs = json.loads(response.content)['data']['items'] actual = [{"content":log['event_type'], "integral":log['integral_count']} for log in member_logs] expected = json.loads(context.text) bdd_util.assert_list(expected, actual)
def step_impl(context, webapp_owner_name, webapp_user_name, integral_count): url = '/member/api/integral/?design_mode=0&version=1' webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member_id = bdd_util.get_member_for(webapp_user_name, webapp_id).id data = { 'integral': integral_count, 'member_id': member_id } context.client.post(url, data)
def step_impl(context, webapp_user_name, webapp_owner_name): webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member = bdd_util.get_member_for(webapp_user_name, webapp_id) integral_logs = MemberIntegralLog.objects.filter(member=member).order_by('-id') json_data = json.loads(context.text) actual_list = [] for data in integral_logs: actual_list.append({"content": data.event_type, "integral": data.integral_count}) bdd_util.assert_list(actual_list, json_data)
def __create_default_ship_info(webapp_user_name, webapp_owner_name): webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member = bdd_util.get_member_for(webapp_user_name, webapp_id) webapp_user = WebAppUser.objects.get(member_id=member.id) ShipInfo.objects.create(webapp_user_id=webapp_user.id, ship_name='bill', ship_tel='13811223344', ship_address=u'泰兴大厦', area='1_1_8', is_selected=True)
def step_impl(context, webapp_user_name): expected = json.loads(context.text) member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) url = '/workbench/jqm/preview/?woid=%d&module=user_center&model=wishlist&action=get&member_id=%s' % ( context.webapp_owner_id, member.id) response = context.client.get(bdd_util.nginx(url), follow=True) products = response.context['products'] actual = [] for product in products: product_info = {} product_info['name'] = product.name product_info['price'] = product.price_info[ 'min_price'] if product.price_info['min_price'] else product.price actual.append(product_info) bdd_util.assert_list(expected, actual)
def step_impl(context, webapp_owner_name, webapp_user_name, action): if hasattr(context, 'client'): context.client.logout() context.client = bdd_util.login(webapp_owner_name) webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member = bdd_util.get_member_for(webapp_user_name, webapp_id) context.client.request_member = member member_json = dict() member_json['member_id'] = member.id member_json['is_for_buy_test'] = 1 member_json['grade_id'] = member.grade_id #member_json['integral'] = member.integral member_json['sex'] = 0 if action == u'分配': url = u'/member/api/member/update/' response = context.client.post(bdd_util.nginx(url), member_json)
def step_impl(context, webapp_user_name, lottery_name): #webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) lottery = bdd_util.get_lottery_setting(lottery_name) assert lottery is not None owner_id = lottery.owner_id webapp_id = bdd_util.get_webapp_id_via_owner_id(owner_id) member = bdd_util.get_member_for(webapp_user_name, webapp_id) #print("member: {}".format(member)) LotteryRecord.objects.create(owner=lottery.owner, member=member, lottery=lottery, lottery_name=lottery.name, prize_type=0, prize_level=0, prize_name=u'谢谢参与', is_awarded=True, prize_number=time.time(), prize_detail='', prize_money=0)
def step_impl(context): """ 处理批量参加微信抽奖用户的情况 """ for row in context.table: activity_name = row['activity_name'] #lottery = bdd_util.get_lottery_setting(activity_name) #assert lottery is not None #owner_id = lottery.owner_id user_name = row['participant'] responsible_person = row['responsible_person'] member_source = row.get('member_source') print('member_source: [{}]'.format(member_source)) if member_source == u'会员分享': share_link_attention = row['share_link_attention'] some_user, activity = share_link_attention.split(',') # 模拟通过微信抽奖关注 context.execute_steps(u"Given %s关注%s的公众号" % (user_name, responsible_person)) # 模拟在MemberMarketUrl中添加记录。实际应该在oauth_shared_url_service中添加记录。 webapp_id = bdd_util.get_webapp_id_for(responsible_person) member = bdd_util.get_member_for(user_name, webapp_id) if MemberMarketUrl.objects.filter(member=member).count() == 0: # 只在创建时添加记录 MemberMarketUrl.objects.create( member=member, market_tool_name='lottery', url=share_link_attention, page_title='微信抽奖新增会员记录', follower_member_token='-', created_at=row['start_time'], ) context.execute_steps(u"When %s参加抽奖活动'%s'" % (user_name, activity_name))
def step_impl(context, webapp_owner_name, webapp_user_name, grade_name): user = context.client.user member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) db_grade = MemberGrade.objects.get(name=grade_name, webapp_id=user.get_profile().webapp_id) context.client.post('/member/api/update_member_tag_or_grade/', { 'checked_ids': db_grade.id, 'member_id': member.id, 'type': 'grade'})
def _update_member_by_name(webapp_id, member_data): webapp_user_name = member_data.get('name') member = bdd_util.get_member_for(webapp_user_name, webapp_id) member.integral = member_data['integral'] member.save()
def step_impl(context, webapp_user_name, webapp_owner_name): """最近修改: duhao 20160401 weapp中一些BDD仍然需要购买相关的测试场景,在这里调用apiserver的接口实现购买操作 e.g.: { "order_id": "" # 订单号 "ship_area": "", "ship_name": "bill", "ship_address": "", "ship_tel": "", "customer_message": "", "integral": "10", "integral_money": "10", "weizoom_card": [{"card_name":"", "card_pass": ""}], "coupon": "coupon_1", "date": "" # 下单时间 "products": [ { "count": "", "name": "", "promotion": {"name": ""}, integral: "" },... ] } """ if hasattr(context, 'caller_step_purchase_info') and context.caller_step_purchase_info: args = context.caller_step_purchase_info else: args = json.loads(context.text) def __get_current_promotion_id_for_product(product, member_grade_id): promotion_ids = [r.promotion_id for r in ProductHasPromotion.objects.filter(product_id=product.id)] promotions = Promotion.objects.filter(id__in=promotion_ids, status=PROMOTION_STATUS_STARTED).exclude(type__gt=3) if len(promotions) > 0 and (promotions[0].member_grade_id <= 0 or \ promotions[0].member_grade_id == member_grade_id): # 存在促销信息,且促销设置等级对该会员开放 if promotions[0].type != PROMOTION_TYPE_INTEGRAL_SALE: return promotions[0].id return 0 settings = IntegralStrategySttings.objects.filter(webapp_id=context.webapp_id) integral_each_yuan = settings[0].integral_each_yuan member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) group2integralinfo = dict() if webapp_owner_name == u'订单中': is_order_from_shopping_cart = "true" webapp_owner_id = context.webapp_owner_id product_ids = [] product_counts = [] promotion_ids = [] product_model_names = [] products = context.response.context['order'].products integral = 0 integral_group_items = [] for product in products: product_counts.append(str(product.purchase_count)) product_ids.append(str(product.id)) if hasattr(product, 'promotion'): promotion = Promotion.objects.get(name=product.promotion.name) promotion_ids.append(str(promotion.id)) else: promotion_ids.append(str(__get_current_promotion_id_for_product(product_obj, member.grade_id))) product_model_names.append(_get_product_model_ids_from_name(webapp_owner_id, product.model_name)) if hasattr(product, 'integral') and product.integral > 0: integral += product.integral integral_group_items.append('%s_%s' % (product.id, product.model['name'])) if integral: group2integralinfo['-'.join(integral_group_items)] = { "integral": integral, "money": round(integral / integral_each_yuan, 2) } else: is_order_from_shopping_cart = "false" webapp_owner_id = bdd_util.get_user_id_for(webapp_owner_name) product_ids = [] product_counts = [] product_model_names = [] promotion_ids = [] products = args['products'] # integral = 0 # integral_group_items = [] for product in products: product_counts.append(str(product['count'])) product_name = product['name'] product_obj = Product.objects.get(owner_id=webapp_owner_id, name=product_name) product_ids.append(str(product_obj.id)) if product.has_key('promotion'): promotion = Promotion.objects.get(name=product['promotion']['name']) promotion_ids.append(str(promotion.id)) else: promotion_ids.append(str(__get_current_promotion_id_for_product(product_obj, member.grade_id))) _product_model_name = _get_product_model_ids_from_name(webapp_owner_id, product.get('model', None)) product_model_names.append(_product_model_name) if __has_active_integral_sale(product_obj.id): if 'integral' in product and product['integral'] > 0: # integral += product['integral'] # integral_group_items.append('%s_%s' % (product_obj.id, _product_model_name)) group2integralinfo['%s_%s' % (product_obj.id, _product_model_name)] = { "integral": product['integral'], "money": round(product['integral'] / integral_each_yuan, 2) } # if integral: # group2integralinfo['-'.join(integral_group_items)] = { # "integral": integral, # "money": round(integral / integral_each_yuan, 2) # } order_type = args.get('type', 'object') # 处理中文地区转化为id,如果数据库不存在的地区则自动添加该地区 ship_area = get_area_ids(args.get('ship_area')) data = { "webapp_user_name": webapp_user_name, "webapp_owner_name": webapp_owner_name, #参数中携带webapp_user_name和webapp_owner_name,方便apiserver处理 "woid": webapp_owner_id, "is_order_from_shopping_cart": is_order_from_shopping_cart, "product_ids": '_'.join(product_ids), "promotion_ids": '_'.join(promotion_ids), "product_counts": '_'.join(product_counts), "product_model_names": '$'.join(product_model_names), "ship_name": args.get('ship_name', "未知姓名"), "area": ship_area, "ship_id": 0, "ship_address": args.get('ship_address', "长安大街"), "ship_tel": args.get('ship_tel', "11111111111"), "is_use_coupon": "false", "coupon_id": 0, # "coupon_coupon_id": "", "message": args.get('customer_message', ''), "group2integralinfo": json.JSONEncoder().encode(group2integralinfo), "card_name": '', "card_pass": '', "xa-choseInterfaces": PAYNAME2ID.get(args.get("pay_type",u"微信支付"),-1) } if not group2integralinfo: if 'integral' in args and int(args['integral'] > 0): # 整单积分抵扣 # orderIntegralInfo:{"integral":20,"money":"10.00"}" orderIntegralInfo = dict() orderIntegralInfo['integral'] = args['integral'] if 'integral_money' in args: orderIntegralInfo['money'] = args['integral_money'] else: orderIntegralInfo['money'] = round(int(args['integral'])/integral_each_yuan, 2) data["orderIntegralInfo"] = json.JSONEncoder().encode(orderIntegralInfo) if order_type == u'测试购买': data['order_type'] = PRODUCT_TEST_TYPE else: data['order_type'] = order_type if u'weizoom_card' in args: for card in args[u'weizoom_card']: data['card_name'] += card[u'card_name'] + ',' data['card_pass'] += card[u'card_pass'] + ',' #填充商品积分 # for product_model_id, integral in product_integrals: # data['is_use_integral_%s' % product_model_id] = 'on' # data['integral_%s' % product_model_id] = integral #填充优惠券信息 # 根据优惠券规则名称填充优惠券ID coupon = args.get('coupon', None) if coupon: data['is_use_coupon'] = 'true' data['coupon_id'] = coupon access_token = bdd_util.get_access_token(member.id, webapp_owner_id) openid = bdd_util.get_openid(member.id, webapp_owner_id) url = 'http://api.weapp.com/default/mall/order/?_method=put' data['access_token'] = access_token data['openid'] = openid data['woid'] = webapp_owner_id response = requests.post(url, data=data) #response结果为: {"errMsg": "", "code": 200, "data": {"msg": null, "order_id": "20140620180559"}} response_json = json.loads(response.text) context.response = response_json # raise '----------------debug test----------------------' if response_json['code'] == 200: # context.created_order_id为订单ID context.created_order_id = response_json['data']['order_id'] #访问支付结果链接 pay_url_info = response_json['data']['pay_url_info'] pay_type = pay_url_info['type'] del pay_url_info['type'] # if pay_type == 'cod': # pay_url = 'http://api.weapp.com/wapi/pay/pay_result/?_method=put' # data = { # 'pay_interface_type': pay_url_info['pay_interface_type'], # 'order_id': pay_url_info['order_id'], # 'access_token': access_token # } # pay_response = requests.post(url, data=data) # pay_response_json = json.loads(pay_response.text) #同步更新支付时间 if Order.objects.get(order_id=context.created_order_id).status > ORDER_STATUS_CANCEL and args.has_key('payment_time'): Order.objects.filter(order_id=context.created_order_id).update(payment_time=bdd_util.get_datetime_str(args['payment_time'])) else: context.created_order_id = -1 context.response_json = response_json context.server_error_msg = response_json['innerErrMsg'] print("buy_error----------------------------",context.server_error_msg,response) if context.created_order_id != -1: if 'date' in args: Order.objects.filter(order_id=context.created_order_id).update(created_at=bdd_util.get_datetime_str(args['date'])) if 'order_id' in args: db_order = Order.objects.get(order_id=context.created_order_id) db_order.order_id=args['order_id'] db_order.save() if db_order.origin_order_id <0: for order in Order.objects.filter(origin_order_id=db_order.id): order.order_id = '%s^%s' % (args['order_id'], order.order_id.split('^')[1]) order.save() context.created_order_id = args['order_id'] context.product_ids = product_ids context.product_counts = product_counts context.product_model_names = product_model_names context.webapp_owner_name = webapp_owner_name
def set_order_dict(order, profile): """ order -> { 'status': 'logistics': 'type': 'methods_of_payment': 'member': 'order_no': 'number': 'integral': 'ship_name': 'ship_tel': 'order_time': 'payment_time': 'products': } """ status = _get_status_by_name(order.get('status')) express_value = express_util.get_value_by_name(order.get('logistics')) type = _get_type_by_name(order.get('type')) pay_interface_type = _get_paytype_by_name(order.get('methods_of_payment')) webapp_user_id = 1 if order.get('member'): webapp_user_name = order.get('member') member = bdd_util.get_member_for(webapp_user_name, profile.webapp_id) try: webapp_user_id = WebAppUser.objects.get(member_id=member.id).id except: pass area = get_area_ids(order.get('ship_area', None)) order_model = OrderFactory(order_id=order.get('order_no'), express_company_name=express_value, express_number=order.get('number', ''), status=status, webapp_id=profile.webapp_id, type=type, pay_interface_type=pay_interface_type, integral=order.get('integral', 0), webapp_user_id=webapp_user_id, ship_name=order.get('ship_name', u'收货人'), ship_tel=order.get('ship_tel', u'1333333333'), webapp_source_id=profile.webapp_id, ship_address=order.get('ship_address', ''), area=area) order_model.product_price = 0 if order.get('order_time'): order_model.created_at = order.get('order_time') order_model.save() if order.get('payment_time'): order_model.payment_time = order.get('payment_time') order_model.save() if order.get('sources') == u"商城": order_model.order_source = ORDER_SOURCE_WEISHOP if order.get('products'): for product_data in order.get('products'): product = Product.objects.get(name=product_data.get('name')) product.stocks = product.stocks - product_data.get('count') product.save() model = product_data.get('model', None) model_name = get_product_model_keys(model) # TODO wan shan gui ge # if model: # value = ProductModelPropertyValue.objects.get(name=model) # model_name = '%s:%s' % (value.property_id, value.id) # else: # model_name = 'standard' product_model = ProductModel.objects.get(product_id=product.id, name=model_name) count = product_data.get('count', None) if not count: count = 1 count = int(count) order_model.product_price += product_model.price * count OrderHasProduct.objects.create(order=order_model, product=product, product_name=product.name, product_model_name=model_name, price=product_model.price, total_price=product_model.price * count, number=count) # else: # OrderHasProduct.objects.create( # order=order_model, # product=product, # product_name=product.name, # product_model_name='standard', # price=product.price, # total_price=product.price, # number=product_data.get('count') # ) order_model.final_price = order_model.product_price order_model.save() if order.get('integral'): member.integral = member.integral - order.get('integral') member.save() if order.get('is_first_order') == 'true': order_model.is_first_order = True order_model.save()
def step_impl(context, webapp_user_name, pay_interface_name): """ 最近修改: duhao 20160405 支付改为调用apiserver的接口 """ # order = Order.objects.get(order_id=context.created_order_id) # data = { # "webapp_owner_id": context.webapp_owner_id, # "module": "mall", # "target_api": "order/pay", # "order_id": order.id # } interface_type = None from mall.models import PayInterface, PAYTYPE2NAME pay_interfaces = PayInterface.objects.all() for pay_interface in pay_interfaces: if PAYTYPE2NAME[pay_interface.type] != pay_interface_name: continue # data.setdefault('interface_type', pay_interface.type) # data.setdefault('interface_id', pay_interface.id) interface_type = pay_interface.type break member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) openid = bdd_util.get_openid(member.id, context.webapp_owner_id) access_token = bdd_util.get_access_token(member.id, context.webapp_owner_id) pay_url = 'http://api.weapp.com/pay/pay_result/?_method=put' data = { 'pay_interface_type': interface_type, 'order_id': context.created_order_id, 'access_token': access_token, 'openid': openid, 'woid': context.webapp_owner_id } pay_response = requests.post(pay_url, data=data) pay_result_url = 'http://api.weapp.com/pay/pay_result/?_method=get' data = { 'order_id': context.created_order_id, 'access_token': access_token, 'openid': openid, 'woid': context.webapp_owner_id } pay_result_response = requests.get(pay_result_url, params=data) pay_result = json.loads(pay_result_response.text) context.pay_result = pay_result['data'] # url = '/webapp/api/project_api/call/' # response = context.client.post(url, data) # bdd_util.assert_api_call_success(response) # # if pay_interface.type == PAY_INTERFACE_WEIZOOM_COIN: # # _pay_weizoom_card(context, data, order) # if pay_interface.type == PAY_INTERFACE_WEIXIN_PAY: # response_data = json.loads(response.content)['data'] # pay_url = response_data['url'] # pay_result_url = __do_weixin_pay(context, pay_url) # if pay_result_url: # context.client.get(pay_result_url, follow=True) # elif pay_interface.type == PAY_INTERFACE_COD: # response_data = json.loads(response.content)['data'] # context.pay_result_url = response_data['url'] # url = '/workbench/jqm/preview/%s' % context.pay_result_url[2:] # response = context.client.get(bdd_util.nginx(url), follow=True) # context.pay_result = response.context # # 直接修改数据库的订单状态 # elif pay_interface.type == PAY_INTERFACE_ALIPAY:#ali # url = '/termite/workbench/jqm/preview/?module=mall&model=pay_result&action=get&pay_interface_type=%s&out_trade_no=%s&woid=%s&result=success' % (data['interface_type'], order.order_id, context.webapp_owner_id) # context.client.get(bdd_util.nginx(url), follow=True) if hasattr(context, 'order_payment_time'): order.payment_time = context.order_payment_time order.save() delattr(context, 'order_payment_time') context.pay_order_id = context.created_order_id
def step_impl(context, webapp_user_name, webapp_owner_name, integral_count): webapp_id = bdd_util.get_webapp_id_for(webapp_owner_name) member = bdd_util.get_member_for(webapp_user_name, webapp_id) Member.objects.filter(id=member.id).update(integral=int(integral_count))