# -*- coding: utf-8 -*- # !/usr/bin/env python from hypothesis import given, strategies as st from apilib.monetary import RMB from apilib.utils_datetime import generate_timestamp_ex from apps.web.agent.define import AGENT_INCOME_TYPE from apps.web.agent.models import Agent ### ### Withdraw related ### @given(income_type = st.sampled_from( [AGENT_INCOME_TYPE.AD, AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, AGENT_INCOME_TYPE.DEALER_CARD_FEE]), amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000))) def test_agent_incr_fund(agent_no_customized, source_key, income_type, amount): # type:(Agent, str, str, [int, float])->None """ :return: """ added = RMB(amount) desired_balance = agent_no_customized.sub_balance(income_type, source_key) + added desired_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) agent_no_customized.incr_fund(income_type, source_key, added) assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance ### ### Withdraw related ### @given(income_type = st.sampled_from( [AGENT_INCOME_TYPE.AD, AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, AGENT_INCOME_TYPE.DEALER_CARD_FEE]), amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000))) def test_agent_decr_fund(agent_no_customized, source_key, income_type, amount): # type:(Agent, str, str, [int, float])->None """ :return: """ agent_no_customized.set_balance(income_type, source_key, RMB('23456')) agent_no_customized.reload() sub = RMB(amount) desired_balance = agent_no_customized.sub_balance(income_type, source_key) - sub desired_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) agent_no_customized.decr_fund(income_type, source_key, sub) assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance ### ### Withdraw related ### @given(income_type = st.sampled_from( [AGENT_INCOME_TYPE.AD, AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, AGENT_INCOME_TYPE.DEALER_CARD_FEE]), amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000))) def test_agent_set_balance(agent_no_customized, source_key, income_type, amount): # type:(Agent, str, str, [int, float])->None """ :return: """ money = RMB(amount) desired_balance = money desired_frozen_balance = RMB(0) agent_no_customized.set_balance(income_type, source_key, money) assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance @given(income_type = st.sampled_from( [AGENT_INCOME_TYPE.AD, AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, AGENT_INCOME_TYPE.DEALER_CARD_FEE]), amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)), initial_amount = st.one_of(st.floats(min_value = 1000, max_value = 10000), st.integers(min_value = 1000, max_value = 10000))) def test_agent_recover_freeze_balance(agent_no_customized, source_key, income_type, amount, initial_amount): # type:(Agent, str, str, [int, float], [int, float])->None initial = RMB(initial_amount) agent_no_customized.set_balance(income_type, source_key, initial) agent_no_customized.reload() withdrawed = RMB(amount) transaction_id = str(generate_timestamp_ex()) before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) before_balance = agent_no_customized.sub_balance(income_type, source_key) agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == before_frozen_balance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == before_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert exists agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == before_frozen_balance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == before_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert exists agent_no_customized.recover_frozen_balance(income_type, withdrawed, source_key, transaction_id) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance assert agent_no_customized.sub_balance(income_type, source_key) == before_balance assert agent_no_customized.sub_balance(income_type, source_key) == initial exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert not exists agent_no_customized.recover_frozen_balance(income_type, withdrawed, source_key, transaction_id) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance assert agent_no_customized.sub_balance(income_type, source_key) == before_balance assert agent_no_customized.sub_balance(income_type, source_key) == initial exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert not exists @given(income_type = st.sampled_from( [AGENT_INCOME_TYPE.AD, AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, AGENT_INCOME_TYPE.DEALER_CARD_FEE]), amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)), initial_amount = st.one_of(st.floats(min_value = 1000, max_value = 10000), st.integers(min_value = 1000, max_value = 10000))) def test_agent_clear_freeze_balance(agent_no_customized, source_key, income_type, amount, initial_amount): # type:(Agent, str, str, [int, float], [int, float])->None initial = RMB(initial_amount) agent_no_customized.set_balance(income_type, source_key, initial) agent_no_customized.reload() withdrawed = RMB(amount) transaction_id = str(generate_timestamp_ex()) pre_frozenBalance = agent_no_customized.sub_frozen_balance(income_type, source_key) pre_balance = agent_no_customized.sub_balance(income_type, source_key) agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == pre_frozenBalance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert exists agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == pre_frozenBalance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert exists agent_no_customized.clear_frozen_balance(transaction_id) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) == pre_frozenBalance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert not exists agent_no_customized.clear_frozen_balance(transaction_id) agent_no_customized.reload() assert agent_no_customized.sub_frozen_balance(income_type, source_key) == pre_frozenBalance assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance exists = False for item in agent_no_customized.inhandWithdrawList: if transaction_id == item['transaction_id']: exists = True break assert not exists # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000))) # def test_agent_withdraw_with_wechat(mocker, agent_no_customized, income_type, gateway_key, amount, # agent_no_customized_client): # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None # # money = RMB(amount) # # desired_balance = agent_no_customized.sub_balance(income_type, gateway_key) # assert agent_no_customized.incr_fund(income_type, gateway_key, money), 'balance inc failed' # # agent_no_customized.reload() # # desired_service_fee = money * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT) # desired_actual_pay = money - desired_service_fee # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(money), # 'sourceType': income_type, # 'sourceId': gateway_key # } # # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_SUCCEEDED) # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # agent_no_customized.reload() # # assert desired_balance == agent_no_customized.sub_balance(income_type, gateway_key) # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 1 # assert json_response['description'] == u'提现成功' # # withdraw_record_id = json_response['payload']['paymentId'] # # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord # assert record # assert record.serviceFee == desired_service_fee # assert record.actualPay == desired_actual_pay # assert record.amount == money # assert record.ownerId == str(agent_no_customized.id) # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT # assert record.balance == (desired_balance + money) # assert record.withdrawGatewayKey == gateway_key # assert record.status == WithdrawStatus.SUCCEEDED # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # amount = st.one_of(st.floats(min_value = 0.01, max_value = settings.WITHDRAW_MINIMUM - 0.01), # st.integers(min_value = 1, max_value = settings.WITHDRAW_MINIMUM - 1))) # def test_agent_withdraw_with_wechat_less_min(mocker, agent_no_customized, income_type, source_key, amount, # agent_no_customized_client): # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None # money = RMB(amount) # # pre_balance = agent_no_customized.sub_balance(income_type, source_key) # assert agent_no_customized.incr_fund(income_type, source_key, money), 'balance inc failed' # added_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(money), # 'sourceType': income_type, # 'sourceId': source_key # } # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 0 # assert json_response['description'] == u"提现金额不能少于%s元" % (settings.WITHDRAW_MINIMUM,) # # assert added_balance == agent_no_customized.reload().sub_balance(income_type, source_key) # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # amount = st.one_of( # st.floats(min_value = settings.WITHDRAW_MAXIMUM + 0.01, max_value = settings.WITHDRAW_MAXIMUM + 10), # st.integers(min_value = settings.WITHDRAW_MAXIMUM + 1, max_value = settings.WITHDRAW_MAXIMUM + 10))) # def test_agent_withdraw_with_wechat_more_max(mocker, agent_no_customized, income_type, source_key, amount, # agent_no_customized_client): # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None # money = RMB(amount) # # pre_balance = agent_no_customized.sub_balance(income_type, source_key) # assert agent_no_customized.incr_fund(income_type, source_key, money), 'balance inc failed' # added_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # from apps.web.dealer.views import dealerWithdraw # url = url_fn(dealerWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(money), # 'sourceType': income_type, # 'sourceId': source_key # } # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 0 # assert json_response['description'] == u"单次提现金额不得大于%s元" % (settings.WITHDRAW_MAXIMUM,) # # assert added_balance == agent_no_customized.reload().sub_balance(income_type, source_key) # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE])) # def test_agent_withdraw_with_wechat_less_balance(mocker, agent_no_customized, income_type, source_key, agent_no_customized_client): # # type: (MockFixture, Agent, str, str, RequestTestClient)->None # withdrawed = RMB(20) # # agent_no_customized.set_balance(income_type, source_key, RMB(10)) # pre_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # assert pre_balance == RMB(10) # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(withdrawed), # 'sourceType': income_type, # 'sourceId': source_key # } # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 0 # assert json_response['description'] == u"余额不足" # # assert pre_balance == agent_no_customized.reload().sub_balance(income_type, source_key) # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # test_id = st.integers(min_value = 0, max_value = len(WECHAT_WITHDRAW_ERROR) - 1)) # def test_agent_withdraw_via_wechat_error(mocker, agent_no_customized, income_type, source_key, # agent_no_customized_client, test_id): # # type: (MockFixture, Agent, str, str, RequestTestClient, int)->None # """ # 测试提现到微信失败的场景 # """ # desired_result = WECHAT_WITHDRAW_ERROR[test_id]['result'] # if 'remarks' in WECHAT_WITHDRAW_ERROR[test_id]: # desired_remarks = WECHAT_WITHDRAW_ERROR[test_id]['remarks'] # else: # desired_remarks = u'{err_code_des}({err_code})'.format( # err_code = WECHAT_WITHDRAW_ERROR[test_id]['err_code'], # err_code_des = WECHAT_WITHDRAW_ERROR[test_id][ # 'err_code_des']) # # if 'show_message' in WECHAT_WITHDRAW_ERROR[test_id]: # desired_show_message = WECHAT_WITHDRAW_ERROR[test_id]['show_message'] # else: # desired_show_message = WECHAT_WITHDRAW_ERROR[test_id]['err_code_des'] # # withdrawed = RMB(20) # # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key) # # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed' # # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT) # desired_actual_pay = withdrawed - desired_service_fee # # before_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(withdrawed), # 'sourceType': income_type, # 'sourceId': source_key # } # # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_ERROR[test_id]) # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == desired_result # assert json_response['description'] == desired_show_message # # agent_no_customized.reload() # # if WECHAT_WITHDRAW_ERROR[test_id]['refund']: # assert before_withdraw_balance == agent_no_customized.sub_balance(income_type, source_key) # else: # assert after_withdraw_balance == agent_no_customized.sub_balance(income_type, source_key) # # withdraw_record_id = json_response['payload']['paymentId'] # # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord # assert record # assert record.serviceFee == desired_service_fee # assert record.actualPay == desired_actual_pay # assert record.amount == withdrawed # assert record.ownerId == str(agent_no_customized.id) # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT # assert record.balance == before_withdraw_balance # assert record.withdrawGatewayKey == source_key # # if WECHAT_WITHDRAW_ERROR[test_id]['refund']: # assert record.status == WithdrawStatus.CLOSED # else: # assert record.status == WithdrawStatus.FAILED # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # amount = st.one_of(st.floats(min_value = 10, max_value = 1000), # st.integers(min_value = 10, max_value = 1000))) # def test_agent_withdraw_with_bank(mocker, agent_no_customized, income_type, source_key, amount, # agent_no_customized_client): # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None # # withdrawed = RMB(amount) # # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key) # # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed' # # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT) # desired_actual_pay = withdrawed - desired_service_fee # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.BANK, # 'amount': str(withdrawed), # 'sourceType': income_type, # 'sourceId': source_key, # 'bankAccount': BANK_ACCOUNT_CODE # } # # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_SUCCEEDED) # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # assert after_withdraw_balance == agent_no_customized.reload().sub_balance(income_type, source_key) # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 1 # assert json_response['description'] == u'提现申请微信已经受理' # # withdraw_record_id = json_response['payload']['paymentId'] # # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord # assert record # assert record.serviceFee == desired_service_fee # assert record.actualPay == desired_actual_pay # assert record.amount == withdrawed # assert record.ownerId == str(agent_no_customized.id) # assert record.payType == WITHDRAW_PAY_TYPE.BANK # assert record.balance == before_withdraw_balance # assert record.withdrawGatewayKey == source_key # assert record.status == WithdrawStatus.PROCESSING # assert record.remarks == u'提现申请微信已经受理' # # # @given(income_type = st.sampled_from( # [AGENT_INCOME_TYPE.AD, # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE, # AGENT_INCOME_TYPE.DEALER_CARD_FEE]), # manual_type = st.integers(min_value = 1, max_value = 2)) # # def test_agent_withdraw_with_bank_manual(mocker, agent_no_customized, income_type, source_key, agent_no_customized_client, manual_type): # # type: (MockFixture, Agent, str, str, RequestTestClient, int)->None # # if manual_type == 1: # pass # else: # wechat_payment_app = get_wechat_app_from_gateway_key(source_key) # type: WechatPayApp # wechat_payment_app.manual_withdraw = True # # withdrawed = RMB(20) # # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key) # # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed' # # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT) # desired_actual_pay = withdrawed - desired_service_fee # # from apps.web.agent.views import agentWithdraw # url = url_fn(agentWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.BANK, # 'amount': str(withdrawed), # 'sourceType': income_type, # 'sourceId': source_key, # 'bankAccount': BANK_ACCOUNT_CODE # } # # response = agent_no_customized_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 1 # assert json_response['description'] == u"提现申请已经受理" # # assert after_withdraw_balance == agent_no_customized.reload().sub_balance(income_type, source_key) # # withdraw_record_id = json_response['payload']['paymentId'] # # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord # assert record # assert record.serviceFee == desired_service_fee # assert record.actualPay == desired_actual_pay # assert record.amount == withdrawed # assert record.ownerId == str(agent_no_customized.id) # assert record.payType == WITHDRAW_PAY_TYPE.BANK # assert record.balance == before_withdraw_balance # assert record.withdrawGatewayKey == source_key # assert record.status == WithdrawStatus.PROCESSING # assert record.remarks == u'提现申请已经受理' # # # @given(income_type = st.sampled_from([DEALER_INCOME_TYPE.DEVICE_INCOME, DEALER_INCOME_TYPE.AD_INCOME]), # amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)), # fee_ratio = st.integers(min_value = Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO, # max_value = Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO + Permillage("10"))) # def test_record_agent_withdraw_fee(mocker, agent_no_customized, dealer, dealer_client, income_type, amount, fee_ratio): # # type: (MockFixture, Agent, Dealer, RequestTestClient, str, [int, float], int)->None # money = RMB(amount) # # dealer.withdrawFeeRatio = fee_ratio # dealer.save() # # dealer.reload() # # pre_balance = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, money), 'balance inc failed' # # dealer.reload() # # desired_service_fee = money * (dealer.withdrawFeeRatio / Const.WITHDRAW_FEE_UNIT) # desired_actual_pay = money - desired_service_fee # # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO: # earned = money * ( # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT) # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # source_key) + earned # else: # earned = RMB(0) # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) # # from apps.web.dealer.views import dealerWithdraw # url = url_fn(dealerWithdraw) # payload = { # 'code': '1234', # 'payType': WITHDRAW_PAY_TYPE.WECHAT, # 'amount': str(money), # 'sourceType': income_type, # 'sourceId': source_key # } # # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_SUCCEEDED) # # response = dealer_client.post_json(url, data = payload) # # assert response # # dealer.reload() # # assert pre_balance == dealer.sub_balance(income_type, source_key) # # import simplejson as json # json_response = json.loads(response.content) # assert json_response['result'] == 1 # assert json_response['description'] == u'提现成功' # # withdraw_record_id = json_response['payload']['paymentId'] # # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord # assert record # assert record.serviceFee == desired_service_fee # assert record.actualPay == desired_actual_pay # assert record.amount == money # assert record.ownerId == str(dealer.id) # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT # assert record.balance == (pre_balance + money) # assert record.withdrawGatewayKey == source_key # assert record.status == WithdrawStatus.SUCCEEDED # # assert agent_no_customized.reload().sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # source_key) == desired_agent_balance # # report = AgentIncomeReport.objects(agentId = str(agent_no_customized.id), amount = earned).first() # type: AgentIncomeReport # # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO: # assert report # assert report.amount == earned # # report.delete() # else: # assert not report # # # def test_getFeatureList(agent_client, agent): # from apps.web.agent.views import getFeatureList # # from apps.web.common.models import Feature # # agent.update(set__features = []) # # url = url_fn(getFeatureList) # # assert agent_client.post_json(url).json()['payload'] == {} # # with DisposableModel(model = Feature, name = 'test', key = 'test', role = "agent") as model: # agent.update(set__features = [model.key]) # assert agent_client.post_json(url, data = {'list': ['test']}).json()['payload'] == {'test': True} # agent.update(set__features = []) # # # def test_agent_revoke_dealer_withdraw(mocker, dealer, source_key, merchant, default_agent_client, agent_no_customized): # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0]) # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, withdrawed) # before_balance = dealer.reload().sub_balance(income_type, source_key) # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key) # before_outgoing = len(dealer.inhandWithdrawList) # # withdraw_service = DealerWithdrawService(payee = dealer, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(source_key = source_key, recurrent = False) # # assert result['result'] == 0 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.FAILED # # from apps.web.agent.views import revokeWithdrawApplication # url = url_fn(revokeWithdrawApplication) # payload = { # 'orderNo': withdraw_record.order, # 'reason': u'测试的原因' # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first() # assert refund_record # # dealer.reload() # assert dealer.sub_balance(income_type, source_key) == before_balance # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(dealer.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.CLOSED # # # def test_agent_revoke_dealer_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent, # agent_no_customized): # default_agent.payAppWechat.manual_withdraw = True # default_agent.payAppWechat.save() # default_agent.payAppWechat.reload() # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, withdrawed) # before_balance = dealer.reload().sub_balance(income_type, source_key) # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key) # before_outgoing = len(dealer.inhandWithdrawList) # # withdraw_service = DealerWithdrawService(payee = dealer, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 1 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.PROCESSING # # from apps.web.agent.views import revokeWithdrawApplication # url = url_fn(revokeWithdrawApplication) # payload = { # 'orderNo': withdraw_record.order, # 'reason': u'测试的原因' # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first() # assert refund_record # # dealer.reload() # assert dealer.sub_balance(income_type, source_key) == before_balance # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(dealer.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.CLOSED # # # def test_agent_approve_dealer_withdraw(mocker, dealer, source_key, merchant, default_agent, default_agent_client, agent_no_customized): # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0]) # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, withdrawed) # before_balance = dealer.reload().sub_balance(income_type, source_key) # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key) # before_outgoing = len(dealer.inhandWithdrawList) # # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # source_key) + withdrawed * ( # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT) # else: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) # # withdraw_service = DealerWithdrawService(payee = dealer, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 0 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.FAILED # # from apps.web.agent.views import adminAgreeWallet # url = url_fn(adminAgreeWallet) # payload = { # 'orderNo': withdraw_record.order # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # dealer.reload() # assert dealer.sub_balance(income_type, source_key) == balance_if_success # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(dealer.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.SUCCEEDED # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance # # # def test_agent_approve_dealer_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent, # agent_no_customized): # default_agent.payAppWechat.manual_withdraw = True # default_agent.payAppWechat.save() # default_agent.payAppWechat.reload() # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, withdrawed) # before_balance = dealer.reload().sub_balance(income_type, source_key) # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key) # before_outgoing = len(dealer.inhandWithdrawList) # # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # source_key) + withdrawed * ( # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT) # else: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) # # withdraw_service = DealerWithdrawService(payee = dealer, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 1 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.PROCESSING # # from apps.web.agent.views import adminAgreeWallet # url = url_fn(adminAgreeWallet) # payload = { # 'orderNo': withdraw_record.order # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # dealer.reload() # assert dealer.sub_balance(income_type, source_key) == balance_if_success # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(dealer.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.SUCCEEDED # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance # # # def test_agent_revoke_agent_withdraw(mocker, dealer, source_key, merchant, default_agent_client, agent_no_customized): # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0]) # # income_type = AGENT_INCOME_TYPE.DEALER_DEVICE_FEE # # withdrawed = RMB(20) # # balance_after_withdraw = agent_no_customized.sub_balance(income_type, source_key) # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed) # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) # before_outgoing = len(agent_no_customized.inhandWithdrawList) # # withdraw_service = AgentWithdrawService(payee = agent_no_customized, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 0 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.FAILED # assert withdraw_record.role == ROLE.agent # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(income_type, source_key) == balance_after_withdraw # # from apps.web.agent.views import revokeWithdrawApplication # url = url_fn(revokeWithdrawApplication) # payload = { # 'orderNo': withdraw_record.order, # 'reason': u'测试的原因' # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first() # assert refund_record # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(income_type, source_key) == before_balance # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(agent_no_customized.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.CLOSED # # # def test_agent_revoke_agent_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent, # agent_no_customized): # default_agent.payAppWechat.manual_withdraw = True # default_agent.payAppWechat.save() # default_agent.payAppWechat.reload() # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_after_withdraw = agent_no_customized.sub_balance(income_type, source_key) # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed) # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) # before_outgoing = len(agent_no_customized.inhandWithdrawList) # # withdraw_service = AgentWithdrawService(payee = agent_no_customized, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 1 # # agent_no_customized.reload().sub_balance(income_type, source_key) == balance_after_withdraw # # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.PROCESSING # # from apps.web.agent.views import revokeWithdrawApplication # url = url_fn(revokeWithdrawApplication) # payload = { # 'orderNo': withdraw_record.order, # 'reason': u'测试的原因' # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first() # assert refund_record # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(income_type, source_key) == before_balance # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(agent_no_customized.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.CLOSED # # # def test_agent_approve_agent_withdraw(mocker, dealer, source_key, merchant, default_agent, default_agent_client, # agent_no_customized): # from apps.web.core.payment.wechat import WechatPaymentGateway # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0]) # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = agent_no_customized.sub_balance(income_type, source_key) # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed) # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key) # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key) # before_outgoing = len(agent_no_customized.inhandWithdrawList) # # withdraw_service = AgentWithdrawService(payee = agent_no_customized, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 0 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.FAILED # # from apps.web.agent.views import adminAgreeWallet # url = url_fn(adminAgreeWallet) # payload = { # 'orderNo': withdraw_record.order # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(income_type, source_key) == balance_if_success # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(agent_no_customized.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.SUCCEEDED # # # def test_agent_approve_agent_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent, # agent_no_customized): # default_agent.payAppWechat.manual_withdraw = True # default_agent.payAppWechat.save() # default_agent.payAppWechat.reload() # # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME # withdrawed = RMB(20) # # balance_if_success = dealer.sub_balance(income_type, source_key) # assert dealer.incr_fund(income_type, source_key, withdrawed) # before_balance = dealer.reload().sub_balance(income_type, source_key) # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key) # before_outgoing = len(dealer.inhandWithdrawList) # # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, # source_key) + withdrawed * ( # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT) # else: # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) # # withdraw_service = DealerWithdrawService(payee = dealer, # income_type = income_type, # code = None, amount = withdrawed, # pay_type = WITHDRAW_PAY_TYPE.BANK, # bank_card_no = merchant.accountCode) # result = withdraw_service.execute(gateway_key = source_key, recurrent = False) # # assert result['result'] == 1 # withdraw_record_id = result['payload']['paymentId'] # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord # assert withdraw_record # assert withdraw_record.status == WithdrawStatus.PROCESSING # # from apps.web.agent.views import adminAgreeWallet # url = url_fn(adminAgreeWallet) # payload = { # 'orderNo': withdraw_record.order # } # # response = default_agent_client.post_json(url, data = payload) # # assert response # # import simplejson as json # json_response = json.loads(response.content) # # assert json_response['result'] == 1 # # dealer.reload() # assert dealer.sub_balance(income_type, source_key) == balance_if_success # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance # assert before_outgoing == len(dealer.inhandWithdrawList) # # withdraw_record.reload() # assert withdraw_record.status == WithdrawStatus.SUCCEEDED # # agent_no_customized.reload() # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance