test_agent.py 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from django.conf import settings
  4. from hypothesis import given, strategies as st
  5. from pytest_mock import MockFixture
  6. from typing import Union
  7. from apilib.monetary import RMB, Permillage
  8. from apilib.utils_datetime import generate_timestamp_ex
  9. from apps.web.agent.define import AGENT_INCOME_TYPE
  10. from apps.web.agent.models import Agent, AgentIncomeReport
  11. from apps.web.agent.withdraw import AgentWithdrawService
  12. from apps.web.common.models import WithdrawRecord, WithdrawRefundRecord
  13. from apps.web.common.transaction import WithdrawStatus, WITHDRAW_PAY_TYPE
  14. from apps.web.constant import Const
  15. from apps.web.core import ROLE
  16. from apps.web.core.models import BankCard
  17. from apps.web.dealer.define import DEALER_INCOME_TYPE
  18. from apps.web.dealer.models import Dealer
  19. from apps.web.dealer.withdraw import DealerWithdrawService
  20. from common import url_fn, DisposableModel, BANK_ACCOUNT_CODE, MERCHANT_ID
  21. from conftest import RequestTestClient
  22. from responses import WECHAT_WITHDRAW_SUCCEEDED, WECHAT_WITHDRAW_ERROR, BANK_WITHDRAW_SUCCEEDED, BANK_WITHDRAW_ERROR
  23. ###
  24. ### Withdraw related
  25. ###
  26. @given(income_type = st.sampled_from(
  27. [AGENT_INCOME_TYPE.AD,
  28. AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  29. AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  30. AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  31. amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)))
  32. def test_agent_incr_fund(agent_no_customized, source_key, income_type, amount):
  33. # type:(Agent, str, str, [int, float])->None
  34. """
  35. :return:
  36. """
  37. added = RMB(amount)
  38. desired_balance = agent_no_customized.sub_balance(income_type, source_key) + added
  39. desired_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  40. agent_no_customized.incr_fund(income_type, source_key, added)
  41. assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance
  42. assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance
  43. ###
  44. ### Withdraw related
  45. ###
  46. @given(income_type = st.sampled_from(
  47. [AGENT_INCOME_TYPE.AD,
  48. AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  49. AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  50. AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  51. amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)))
  52. def test_agent_decr_fund(agent_no_customized, source_key, income_type, amount):
  53. # type:(Agent, str, str, [int, float])->None
  54. """
  55. :return:
  56. """
  57. agent_no_customized.set_balance(income_type, source_key, RMB('23456'))
  58. agent_no_customized.reload()
  59. sub = RMB(amount)
  60. desired_balance = agent_no_customized.sub_balance(income_type, source_key) - sub
  61. desired_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  62. agent_no_customized.decr_fund(income_type, source_key, sub)
  63. assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance
  64. assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance
  65. ###
  66. ### Withdraw related
  67. ###
  68. @given(income_type = st.sampled_from(
  69. [AGENT_INCOME_TYPE.AD,
  70. AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  71. AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  72. AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  73. amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)))
  74. def test_agent_set_balance(agent_no_customized, source_key, income_type, amount):
  75. # type:(Agent, str, str, [int, float])->None
  76. """
  77. :return:
  78. """
  79. money = RMB(amount)
  80. desired_balance = money
  81. desired_frozen_balance = RMB(0)
  82. agent_no_customized.set_balance(income_type, source_key, money)
  83. assert agent_no_customized.reload().sub_balance(income_type, source_key) == desired_balance
  84. assert agent_no_customized.reload().sub_frozen_balance(income_type, source_key) == desired_frozen_balance
  85. @given(income_type = st.sampled_from(
  86. [AGENT_INCOME_TYPE.AD,
  87. AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  88. AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  89. AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  90. amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)),
  91. initial_amount = st.one_of(st.floats(min_value = 1000, max_value = 10000),
  92. st.integers(min_value = 1000, max_value = 10000)))
  93. def test_agent_recover_freeze_balance(agent_no_customized, source_key, income_type, amount, initial_amount):
  94. # type:(Agent, str, str, [int, float], [int, float])->None
  95. initial = RMB(initial_amount)
  96. agent_no_customized.set_balance(income_type, source_key, initial)
  97. agent_no_customized.reload()
  98. withdrawed = RMB(amount)
  99. transaction_id = str(generate_timestamp_ex())
  100. before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  101. before_balance = agent_no_customized.sub_balance(income_type, source_key)
  102. agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True)
  103. agent_no_customized.reload()
  104. assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == before_frozen_balance
  105. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == before_balance
  106. exists = False
  107. for item in agent_no_customized.inhandWithdrawList:
  108. if transaction_id == item['transaction_id']:
  109. exists = True
  110. break
  111. assert exists
  112. agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True)
  113. agent_no_customized.reload()
  114. assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == before_frozen_balance
  115. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == before_balance
  116. exists = False
  117. for item in agent_no_customized.inhandWithdrawList:
  118. if transaction_id == item['transaction_id']:
  119. exists = True
  120. break
  121. assert exists
  122. agent_no_customized.recover_frozen_balance(income_type, withdrawed, source_key, transaction_id)
  123. agent_no_customized.reload()
  124. assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  125. assert agent_no_customized.sub_balance(income_type, source_key) == before_balance
  126. assert agent_no_customized.sub_balance(income_type, source_key) == initial
  127. exists = False
  128. for item in agent_no_customized.inhandWithdrawList:
  129. if transaction_id == item['transaction_id']:
  130. exists = True
  131. break
  132. assert not exists
  133. agent_no_customized.recover_frozen_balance(income_type, withdrawed, source_key, transaction_id)
  134. agent_no_customized.reload()
  135. assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  136. assert agent_no_customized.sub_balance(income_type, source_key) == before_balance
  137. assert agent_no_customized.sub_balance(income_type, source_key) == initial
  138. exists = False
  139. for item in agent_no_customized.inhandWithdrawList:
  140. if transaction_id == item['transaction_id']:
  141. exists = True
  142. break
  143. assert not exists
  144. @given(income_type = st.sampled_from(
  145. [AGENT_INCOME_TYPE.AD,
  146. AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  147. AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  148. AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  149. amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)),
  150. initial_amount = st.one_of(st.floats(min_value = 1000, max_value = 10000),
  151. st.integers(min_value = 1000, max_value = 10000)))
  152. def test_agent_clear_freeze_balance(agent_no_customized, source_key, income_type, amount, initial_amount):
  153. # type:(Agent, str, str, [int, float], [int, float])->None
  154. initial = RMB(initial_amount)
  155. agent_no_customized.set_balance(income_type, source_key, initial)
  156. agent_no_customized.reload()
  157. withdrawed = RMB(amount)
  158. transaction_id = str(generate_timestamp_ex())
  159. pre_frozenBalance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  160. pre_balance = agent_no_customized.sub_balance(income_type, source_key)
  161. agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True)
  162. agent_no_customized.reload()
  163. assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == pre_frozenBalance
  164. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance
  165. exists = False
  166. for item in agent_no_customized.inhandWithdrawList:
  167. if transaction_id == item['transaction_id']:
  168. exists = True
  169. break
  170. assert exists
  171. agent_no_customized.freeze_balance(income_type, withdrawed, source_key, transaction_id, True)
  172. agent_no_customized.reload()
  173. assert agent_no_customized.sub_frozen_balance(income_type, source_key) - withdrawed == pre_frozenBalance
  174. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance
  175. exists = False
  176. for item in agent_no_customized.inhandWithdrawList:
  177. if transaction_id == item['transaction_id']:
  178. exists = True
  179. break
  180. assert exists
  181. agent_no_customized.clear_frozen_balance(transaction_id)
  182. agent_no_customized.reload()
  183. assert agent_no_customized.sub_frozen_balance(income_type, source_key) == pre_frozenBalance
  184. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance
  185. exists = False
  186. for item in agent_no_customized.inhandWithdrawList:
  187. if transaction_id == item['transaction_id']:
  188. exists = True
  189. break
  190. assert not exists
  191. agent_no_customized.clear_frozen_balance(transaction_id)
  192. agent_no_customized.reload()
  193. assert agent_no_customized.sub_frozen_balance(income_type, source_key) == pre_frozenBalance
  194. assert agent_no_customized.sub_balance(income_type, source_key) + withdrawed == pre_balance
  195. exists = False
  196. for item in agent_no_customized.inhandWithdrawList:
  197. if transaction_id == item['transaction_id']:
  198. exists = True
  199. break
  200. assert not exists
  201. # @given(income_type = st.sampled_from(
  202. # [AGENT_INCOME_TYPE.AD,
  203. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  204. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  205. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  206. # amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)))
  207. # def test_agent_withdraw_with_wechat(mocker, agent_no_customized, income_type, gateway_key, amount,
  208. # agent_no_customized_client):
  209. # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None
  210. #
  211. # money = RMB(amount)
  212. #
  213. # desired_balance = agent_no_customized.sub_balance(income_type, gateway_key)
  214. # assert agent_no_customized.incr_fund(income_type, gateway_key, money), 'balance inc failed'
  215. #
  216. # agent_no_customized.reload()
  217. #
  218. # desired_service_fee = money * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT)
  219. # desired_actual_pay = money - desired_service_fee
  220. #
  221. # from apps.web.agent.views import agentWithdraw
  222. # url = url_fn(agentWithdraw)
  223. # payload = {
  224. # 'code': '1234',
  225. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  226. # 'amount': str(money),
  227. # 'sourceType': income_type,
  228. # 'sourceId': gateway_key
  229. # }
  230. #
  231. # from apps.web.core.payment.wechat import WechatPaymentGateway
  232. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_SUCCEEDED)
  233. #
  234. # response = agent_no_customized_client.post_json(url, data = payload)
  235. #
  236. # assert response
  237. #
  238. # agent_no_customized.reload()
  239. #
  240. # assert desired_balance == agent_no_customized.sub_balance(income_type, gateway_key)
  241. #
  242. # import simplejson as json
  243. # json_response = json.loads(response.content)
  244. # assert json_response['result'] == 1
  245. # assert json_response['description'] == u'提现成功'
  246. #
  247. # withdraw_record_id = json_response['payload']['paymentId']
  248. #
  249. # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord
  250. # assert record
  251. # assert record.serviceFee == desired_service_fee
  252. # assert record.actualPay == desired_actual_pay
  253. # assert record.amount == money
  254. # assert record.ownerId == str(agent_no_customized.id)
  255. # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT
  256. # assert record.balance == (desired_balance + money)
  257. # assert record.withdrawGatewayKey == gateway_key
  258. # assert record.status == WithdrawStatus.SUCCEEDED
  259. #
  260. #
  261. # @given(income_type = st.sampled_from(
  262. # [AGENT_INCOME_TYPE.AD,
  263. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  264. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  265. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  266. # amount = st.one_of(st.floats(min_value = 0.01, max_value = settings.WITHDRAW_MINIMUM - 0.01),
  267. # st.integers(min_value = 1, max_value = settings.WITHDRAW_MINIMUM - 1)))
  268. # def test_agent_withdraw_with_wechat_less_min(mocker, agent_no_customized, income_type, source_key, amount,
  269. # agent_no_customized_client):
  270. # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None
  271. # money = RMB(amount)
  272. #
  273. # pre_balance = agent_no_customized.sub_balance(income_type, source_key)
  274. # assert agent_no_customized.incr_fund(income_type, source_key, money), 'balance inc failed'
  275. # added_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  276. #
  277. # from apps.web.agent.views import agentWithdraw
  278. # url = url_fn(agentWithdraw)
  279. # payload = {
  280. # 'code': '1234',
  281. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  282. # 'amount': str(money),
  283. # 'sourceType': income_type,
  284. # 'sourceId': source_key
  285. # }
  286. #
  287. # response = agent_no_customized_client.post_json(url, data = payload)
  288. #
  289. # assert response
  290. #
  291. # import simplejson as json
  292. # json_response = json.loads(response.content)
  293. # assert json_response['result'] == 0
  294. # assert json_response['description'] == u"提现金额不能少于%s元" % (settings.WITHDRAW_MINIMUM,)
  295. #
  296. # assert added_balance == agent_no_customized.reload().sub_balance(income_type, source_key)
  297. #
  298. #
  299. # @given(income_type = st.sampled_from(
  300. # [AGENT_INCOME_TYPE.AD,
  301. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  302. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  303. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  304. # amount = st.one_of(
  305. # st.floats(min_value = settings.WITHDRAW_MAXIMUM + 0.01, max_value = settings.WITHDRAW_MAXIMUM + 10),
  306. # st.integers(min_value = settings.WITHDRAW_MAXIMUM + 1, max_value = settings.WITHDRAW_MAXIMUM + 10)))
  307. # def test_agent_withdraw_with_wechat_more_max(mocker, agent_no_customized, income_type, source_key, amount,
  308. # agent_no_customized_client):
  309. # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None
  310. # money = RMB(amount)
  311. #
  312. # pre_balance = agent_no_customized.sub_balance(income_type, source_key)
  313. # assert agent_no_customized.incr_fund(income_type, source_key, money), 'balance inc failed'
  314. # added_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  315. #
  316. # from apps.web.dealer.views import dealerWithdraw
  317. # url = url_fn(dealerWithdraw)
  318. # payload = {
  319. # 'code': '1234',
  320. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  321. # 'amount': str(money),
  322. # 'sourceType': income_type,
  323. # 'sourceId': source_key
  324. # }
  325. #
  326. # response = agent_no_customized_client.post_json(url, data = payload)
  327. #
  328. # assert response
  329. #
  330. # import simplejson as json
  331. # json_response = json.loads(response.content)
  332. # assert json_response['result'] == 0
  333. # assert json_response['description'] == u"单次提现金额不得大于%s元" % (settings.WITHDRAW_MAXIMUM,)
  334. #
  335. # assert added_balance == agent_no_customized.reload().sub_balance(income_type, source_key)
  336. #
  337. #
  338. # @given(income_type = st.sampled_from(
  339. # [AGENT_INCOME_TYPE.AD,
  340. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  341. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  342. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]))
  343. # def test_agent_withdraw_with_wechat_less_balance(mocker, agent_no_customized, income_type, source_key, agent_no_customized_client):
  344. # # type: (MockFixture, Agent, str, str, RequestTestClient)->None
  345. # withdrawed = RMB(20)
  346. #
  347. # agent_no_customized.set_balance(income_type, source_key, RMB(10))
  348. # pre_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  349. #
  350. # assert pre_balance == RMB(10)
  351. #
  352. # from apps.web.agent.views import agentWithdraw
  353. # url = url_fn(agentWithdraw)
  354. # payload = {
  355. # 'code': '1234',
  356. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  357. # 'amount': str(withdrawed),
  358. # 'sourceType': income_type,
  359. # 'sourceId': source_key
  360. # }
  361. #
  362. # response = agent_no_customized_client.post_json(url, data = payload)
  363. #
  364. # assert response
  365. #
  366. # import simplejson as json
  367. # json_response = json.loads(response.content)
  368. # assert json_response['result'] == 0
  369. # assert json_response['description'] == u"余额不足"
  370. #
  371. # assert pre_balance == agent_no_customized.reload().sub_balance(income_type, source_key)
  372. #
  373. #
  374. # @given(income_type = st.sampled_from(
  375. # [AGENT_INCOME_TYPE.AD,
  376. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  377. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  378. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  379. # test_id = st.integers(min_value = 0, max_value = len(WECHAT_WITHDRAW_ERROR) - 1))
  380. # def test_agent_withdraw_via_wechat_error(mocker, agent_no_customized, income_type, source_key,
  381. # agent_no_customized_client, test_id):
  382. # # type: (MockFixture, Agent, str, str, RequestTestClient, int)->None
  383. # """
  384. # 测试提现到微信失败的场景
  385. # """
  386. # desired_result = WECHAT_WITHDRAW_ERROR[test_id]['result']
  387. # if 'remarks' in WECHAT_WITHDRAW_ERROR[test_id]:
  388. # desired_remarks = WECHAT_WITHDRAW_ERROR[test_id]['remarks']
  389. # else:
  390. # desired_remarks = u'{err_code_des}({err_code})'.format(
  391. # err_code = WECHAT_WITHDRAW_ERROR[test_id]['err_code'],
  392. # err_code_des = WECHAT_WITHDRAW_ERROR[test_id][
  393. # 'err_code_des'])
  394. #
  395. # if 'show_message' in WECHAT_WITHDRAW_ERROR[test_id]:
  396. # desired_show_message = WECHAT_WITHDRAW_ERROR[test_id]['show_message']
  397. # else:
  398. # desired_show_message = WECHAT_WITHDRAW_ERROR[test_id]['err_code_des']
  399. #
  400. # withdrawed = RMB(20)
  401. #
  402. # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key)
  403. #
  404. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed'
  405. #
  406. # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  407. #
  408. # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT)
  409. # desired_actual_pay = withdrawed - desired_service_fee
  410. #
  411. # before_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key)
  412. #
  413. # from apps.web.agent.views import agentWithdraw
  414. # url = url_fn(agentWithdraw)
  415. # payload = {
  416. # 'code': '1234',
  417. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  418. # 'amount': str(withdrawed),
  419. # 'sourceType': income_type,
  420. # 'sourceId': source_key
  421. # }
  422. #
  423. # from apps.web.core.payment.wechat import WechatPaymentGateway
  424. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_ERROR[test_id])
  425. #
  426. # response = agent_no_customized_client.post_json(url, data = payload)
  427. #
  428. # assert response
  429. #
  430. # import simplejson as json
  431. # json_response = json.loads(response.content)
  432. # assert json_response['result'] == desired_result
  433. # assert json_response['description'] == desired_show_message
  434. #
  435. # agent_no_customized.reload()
  436. #
  437. # if WECHAT_WITHDRAW_ERROR[test_id]['refund']:
  438. # assert before_withdraw_balance == agent_no_customized.sub_balance(income_type, source_key)
  439. # else:
  440. # assert after_withdraw_balance == agent_no_customized.sub_balance(income_type, source_key)
  441. #
  442. # withdraw_record_id = json_response['payload']['paymentId']
  443. #
  444. # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord
  445. # assert record
  446. # assert record.serviceFee == desired_service_fee
  447. # assert record.actualPay == desired_actual_pay
  448. # assert record.amount == withdrawed
  449. # assert record.ownerId == str(agent_no_customized.id)
  450. # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT
  451. # assert record.balance == before_withdraw_balance
  452. # assert record.withdrawGatewayKey == source_key
  453. #
  454. # if WECHAT_WITHDRAW_ERROR[test_id]['refund']:
  455. # assert record.status == WithdrawStatus.CLOSED
  456. # else:
  457. # assert record.status == WithdrawStatus.FAILED
  458. #
  459. #
  460. # @given(income_type = st.sampled_from(
  461. # [AGENT_INCOME_TYPE.AD,
  462. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  463. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  464. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  465. # amount = st.one_of(st.floats(min_value = 10, max_value = 1000),
  466. # st.integers(min_value = 10, max_value = 1000)))
  467. # def test_agent_withdraw_with_bank(mocker, agent_no_customized, income_type, source_key, amount,
  468. # agent_no_customized_client):
  469. # # type: (MockFixture, Agent, str, str, Union[int, float], RequestTestClient)->None
  470. #
  471. # withdrawed = RMB(amount)
  472. #
  473. # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key)
  474. #
  475. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed'
  476. #
  477. # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  478. #
  479. # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT)
  480. # desired_actual_pay = withdrawed - desired_service_fee
  481. #
  482. # from apps.web.agent.views import agentWithdraw
  483. # url = url_fn(agentWithdraw)
  484. # payload = {
  485. # 'code': '1234',
  486. # 'payType': WITHDRAW_PAY_TYPE.BANK,
  487. # 'amount': str(withdrawed),
  488. # 'sourceType': income_type,
  489. # 'sourceId': source_key,
  490. # 'bankAccount': BANK_ACCOUNT_CODE
  491. # }
  492. #
  493. # from apps.web.core.payment.wechat import WechatPaymentGateway
  494. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_SUCCEEDED)
  495. #
  496. # response = agent_no_customized_client.post_json(url, data = payload)
  497. #
  498. # assert response
  499. #
  500. # assert after_withdraw_balance == agent_no_customized.reload().sub_balance(income_type, source_key)
  501. #
  502. # import simplejson as json
  503. # json_response = json.loads(response.content)
  504. # assert json_response['result'] == 1
  505. # assert json_response['description'] == u'提现申请微信已经受理'
  506. #
  507. # withdraw_record_id = json_response['payload']['paymentId']
  508. #
  509. # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord
  510. # assert record
  511. # assert record.serviceFee == desired_service_fee
  512. # assert record.actualPay == desired_actual_pay
  513. # assert record.amount == withdrawed
  514. # assert record.ownerId == str(agent_no_customized.id)
  515. # assert record.payType == WITHDRAW_PAY_TYPE.BANK
  516. # assert record.balance == before_withdraw_balance
  517. # assert record.withdrawGatewayKey == source_key
  518. # assert record.status == WithdrawStatus.PROCESSING
  519. # assert record.remarks == u'提现申请微信已经受理'
  520. #
  521. #
  522. # @given(income_type = st.sampled_from(
  523. # [AGENT_INCOME_TYPE.AD,
  524. # AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  525. # AGENT_INCOME_TYPE.DEALER_DEVICE_FEE,
  526. # AGENT_INCOME_TYPE.DEALER_CARD_FEE]),
  527. # manual_type = st.integers(min_value = 1, max_value = 2))
  528. #
  529. # def test_agent_withdraw_with_bank_manual(mocker, agent_no_customized, income_type, source_key, agent_no_customized_client, manual_type):
  530. # # type: (MockFixture, Agent, str, str, RequestTestClient, int)->None
  531. #
  532. # if manual_type == 1:
  533. # bank_card = BankCard.objects(id = MERCHANT_ID).first()
  534. # bank_card.accountType = BankCard.AccountType.PUBLIC
  535. # bank_card.save()
  536. # else:
  537. # wechat_payment_app = get_wechat_app_from_gateway_key(source_key) # type: WechatPayApp
  538. # wechat_payment_app.manual_withdraw = True
  539. #
  540. # withdrawed = RMB(20)
  541. #
  542. # after_withdraw_balance = agent_no_customized.sub_balance(income_type, source_key)
  543. #
  544. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed), 'balance inc failed'
  545. #
  546. # before_withdraw_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  547. #
  548. # desired_service_fee = withdrawed * (Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO / Const.WITHDRAW_FEE_UNIT)
  549. # desired_actual_pay = withdrawed - desired_service_fee
  550. #
  551. # from apps.web.agent.views import agentWithdraw
  552. # url = url_fn(agentWithdraw)
  553. # payload = {
  554. # 'code': '1234',
  555. # 'payType': WITHDRAW_PAY_TYPE.BANK,
  556. # 'amount': str(withdrawed),
  557. # 'sourceType': income_type,
  558. # 'sourceId': source_key,
  559. # 'bankAccount': BANK_ACCOUNT_CODE
  560. # }
  561. #
  562. # response = agent_no_customized_client.post_json(url, data = payload)
  563. #
  564. # assert response
  565. #
  566. # import simplejson as json
  567. # json_response = json.loads(response.content)
  568. # assert json_response['result'] == 1
  569. # assert json_response['description'] == u"提现申请已经受理"
  570. #
  571. # assert after_withdraw_balance == agent_no_customized.reload().sub_balance(income_type, source_key)
  572. #
  573. # withdraw_record_id = json_response['payload']['paymentId']
  574. #
  575. # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord
  576. # assert record
  577. # assert record.serviceFee == desired_service_fee
  578. # assert record.actualPay == desired_actual_pay
  579. # assert record.amount == withdrawed
  580. # assert record.ownerId == str(agent_no_customized.id)
  581. # assert record.payType == WITHDRAW_PAY_TYPE.BANK
  582. # assert record.balance == before_withdraw_balance
  583. # assert record.withdrawGatewayKey == source_key
  584. # assert record.status == WithdrawStatus.PROCESSING
  585. # assert record.remarks == u'提现申请已经受理'
  586. #
  587. #
  588. # @given(income_type = st.sampled_from([DEALER_INCOME_TYPE.DEVICE_INCOME, DEALER_INCOME_TYPE.AD_INCOME]),
  589. # amount = st.one_of(st.floats(min_value = 10, max_value = 1000), st.integers(min_value = 10, max_value = 1000)),
  590. # fee_ratio = st.integers(min_value = Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO,
  591. # max_value = Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO + Permillage("10")))
  592. # def test_record_agent_withdraw_fee(mocker, agent_no_customized, dealer, dealer_client, income_type, amount, fee_ratio):
  593. # # type: (MockFixture, Agent, Dealer, RequestTestClient, str, [int, float], int)->None
  594. # money = RMB(amount)
  595. #
  596. # dealer.withdrawFeeRatio = fee_ratio
  597. # dealer.save()
  598. #
  599. # dealer.reload()
  600. #
  601. # pre_balance = dealer.sub_balance(income_type, source_key)
  602. # assert dealer.incr_fund(income_type, source_key, money), 'balance inc failed'
  603. #
  604. # dealer.reload()
  605. #
  606. # desired_service_fee = money * (dealer.withdrawFeeRatio / Const.WITHDRAW_FEE_UNIT)
  607. # desired_actual_pay = money - desired_service_fee
  608. #
  609. # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO:
  610. # earned = money * (
  611. # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT)
  612. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  613. # source_key) + earned
  614. # else:
  615. # earned = RMB(0)
  616. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key)
  617. #
  618. # from apps.web.dealer.views import dealerWithdraw
  619. # url = url_fn(dealerWithdraw)
  620. # payload = {
  621. # 'code': '1234',
  622. # 'payType': WITHDRAW_PAY_TYPE.WECHAT,
  623. # 'amount': str(money),
  624. # 'sourceType': income_type,
  625. # 'sourceId': source_key
  626. # }
  627. #
  628. # from apps.web.core.payment.wechat import WechatPaymentGateway
  629. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_changes', return_value = WECHAT_WITHDRAW_SUCCEEDED)
  630. #
  631. # response = dealer_client.post_json(url, data = payload)
  632. #
  633. # assert response
  634. #
  635. # dealer.reload()
  636. #
  637. # assert pre_balance == dealer.sub_balance(income_type, source_key)
  638. #
  639. # import simplejson as json
  640. # json_response = json.loads(response.content)
  641. # assert json_response['result'] == 1
  642. # assert json_response['description'] == u'提现成功'
  643. #
  644. # withdraw_record_id = json_response['payload']['paymentId']
  645. #
  646. # record = WithdrawRecord.objects(id = str(withdraw_record_id)).first() # type: WithdrawRecord
  647. # assert record
  648. # assert record.serviceFee == desired_service_fee
  649. # assert record.actualPay == desired_actual_pay
  650. # assert record.amount == money
  651. # assert record.ownerId == str(dealer.id)
  652. # assert record.payType == WITHDRAW_PAY_TYPE.WECHAT
  653. # assert record.balance == (pre_balance + money)
  654. # assert record.withdrawGatewayKey == source_key
  655. # assert record.status == WithdrawStatus.SUCCEEDED
  656. #
  657. # assert agent_no_customized.reload().sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  658. # source_key) == desired_agent_balance
  659. #
  660. # report = AgentIncomeReport.objects(agentId = str(agent_no_customized.id), amount = earned).first() # type: AgentIncomeReport
  661. #
  662. # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO:
  663. # assert report
  664. # assert report.amount == earned
  665. #
  666. # report.delete()
  667. # else:
  668. # assert not report
  669. #
  670. #
  671. # def test_getFeatureList(agent_client, agent):
  672. # from apps.web.agent.views import getFeatureList
  673. #
  674. # from apps.web.common.models import Feature
  675. #
  676. # agent.update(set__features = [])
  677. #
  678. # url = url_fn(getFeatureList)
  679. #
  680. # assert agent_client.post_json(url).json()['payload'] == {}
  681. #
  682. # with DisposableModel(model = Feature, name = 'test', key = 'test', role = "agent") as model:
  683. # agent.update(set__features = [model.key])
  684. # assert agent_client.post_json(url, data = {'list': ['test']}).json()['payload'] == {'test': True}
  685. # agent.update(set__features = [])
  686. #
  687. #
  688. # def test_agent_revoke_dealer_withdraw(mocker, dealer, source_key, merchant, default_agent_client, agent_no_customized):
  689. # from apps.web.core.payment.wechat import WechatPaymentGateway
  690. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0])
  691. #
  692. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  693. # withdrawed = RMB(20)
  694. #
  695. # balance_if_success = dealer.sub_balance(income_type, source_key)
  696. # assert dealer.incr_fund(income_type, source_key, withdrawed)
  697. # before_balance = dealer.reload().sub_balance(income_type, source_key)
  698. # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key)
  699. # before_outgoing = len(dealer.inhandWithdrawList)
  700. #
  701. # withdraw_service = DealerWithdrawService(payee = dealer,
  702. # income_type = income_type,
  703. # code = None, amount = withdrawed,
  704. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  705. # bank_card_no = merchant.accountCode)
  706. # result = withdraw_service.execute(source_key = source_key, recurrent = False)
  707. #
  708. # assert result['result'] == 0
  709. # withdraw_record_id = result['payload']['paymentId']
  710. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  711. # assert withdraw_record
  712. # assert withdraw_record.status == WithdrawStatus.FAILED
  713. #
  714. # from apps.web.agent.views import revokeWithdrawApplication
  715. # url = url_fn(revokeWithdrawApplication)
  716. # payload = {
  717. # 'orderNo': withdraw_record.order,
  718. # 'reason': u'测试的原因'
  719. # }
  720. #
  721. # response = default_agent_client.post_json(url, data = payload)
  722. #
  723. # assert response
  724. #
  725. # import simplejson as json
  726. # json_response = json.loads(response.content)
  727. #
  728. # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first()
  729. # assert refund_record
  730. #
  731. # dealer.reload()
  732. # assert dealer.sub_balance(income_type, source_key) == before_balance
  733. # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  734. # assert before_outgoing == len(dealer.inhandWithdrawList)
  735. #
  736. # withdraw_record.reload()
  737. # assert withdraw_record.status == WithdrawStatus.CLOSED
  738. #
  739. #
  740. # def test_agent_revoke_dealer_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent,
  741. # agent_no_customized):
  742. # default_agent.payAppWechat.manual_withdraw = True
  743. # default_agent.payAppWechat.save()
  744. # default_agent.payAppWechat.reload()
  745. #
  746. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  747. # withdrawed = RMB(20)
  748. #
  749. # balance_if_success = dealer.sub_balance(income_type, source_key)
  750. # assert dealer.incr_fund(income_type, source_key, withdrawed)
  751. # before_balance = dealer.reload().sub_balance(income_type, source_key)
  752. # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key)
  753. # before_outgoing = len(dealer.inhandWithdrawList)
  754. #
  755. # withdraw_service = DealerWithdrawService(payee = dealer,
  756. # income_type = income_type,
  757. # code = None, amount = withdrawed,
  758. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  759. # bank_card_no = merchant.accountCode)
  760. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  761. #
  762. # assert result['result'] == 1
  763. # withdraw_record_id = result['payload']['paymentId']
  764. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  765. # assert withdraw_record
  766. # assert withdraw_record.status == WithdrawStatus.PROCESSING
  767. #
  768. # from apps.web.agent.views import revokeWithdrawApplication
  769. # url = url_fn(revokeWithdrawApplication)
  770. # payload = {
  771. # 'orderNo': withdraw_record.order,
  772. # 'reason': u'测试的原因'
  773. # }
  774. #
  775. # response = default_agent_client.post_json(url, data = payload)
  776. #
  777. # assert response
  778. #
  779. # import simplejson as json
  780. # json_response = json.loads(response.content)
  781. #
  782. # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first()
  783. # assert refund_record
  784. #
  785. # dealer.reload()
  786. # assert dealer.sub_balance(income_type, source_key) == before_balance
  787. # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  788. # assert before_outgoing == len(dealer.inhandWithdrawList)
  789. #
  790. # withdraw_record.reload()
  791. # assert withdraw_record.status == WithdrawStatus.CLOSED
  792. #
  793. #
  794. # def test_agent_approve_dealer_withdraw(mocker, dealer, source_key, merchant, default_agent, default_agent_client, agent_no_customized):
  795. # from apps.web.core.payment.wechat import WechatPaymentGateway
  796. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0])
  797. #
  798. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  799. # withdrawed = RMB(20)
  800. #
  801. # balance_if_success = dealer.sub_balance(income_type, source_key)
  802. # assert dealer.incr_fund(income_type, source_key, withdrawed)
  803. # before_balance = dealer.reload().sub_balance(income_type, source_key)
  804. # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key)
  805. # before_outgoing = len(dealer.inhandWithdrawList)
  806. #
  807. # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO:
  808. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  809. # source_key) + withdrawed * (
  810. # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT)
  811. # else:
  812. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key)
  813. #
  814. # withdraw_service = DealerWithdrawService(payee = dealer,
  815. # income_type = income_type,
  816. # code = None, amount = withdrawed,
  817. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  818. # bank_card_no = merchant.accountCode)
  819. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  820. #
  821. # assert result['result'] == 0
  822. # withdraw_record_id = result['payload']['paymentId']
  823. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  824. # assert withdraw_record
  825. # assert withdraw_record.status == WithdrawStatus.FAILED
  826. #
  827. # from apps.web.agent.views import adminAgreeWallet
  828. # url = url_fn(adminAgreeWallet)
  829. # payload = {
  830. # 'orderNo': withdraw_record.order
  831. # }
  832. #
  833. # response = default_agent_client.post_json(url, data = payload)
  834. #
  835. # assert response
  836. #
  837. # import simplejson as json
  838. # json_response = json.loads(response.content)
  839. #
  840. # assert json_response['result'] == 1
  841. #
  842. # dealer.reload()
  843. # assert dealer.sub_balance(income_type, source_key) == balance_if_success
  844. # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  845. # assert before_outgoing == len(dealer.inhandWithdrawList)
  846. #
  847. # withdraw_record.reload()
  848. # assert withdraw_record.status == WithdrawStatus.SUCCEEDED
  849. #
  850. # agent_no_customized.reload()
  851. # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance
  852. #
  853. #
  854. # def test_agent_approve_dealer_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent,
  855. # agent_no_customized):
  856. # default_agent.payAppWechat.manual_withdraw = True
  857. # default_agent.payAppWechat.save()
  858. # default_agent.payAppWechat.reload()
  859. #
  860. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  861. # withdrawed = RMB(20)
  862. #
  863. # balance_if_success = dealer.sub_balance(income_type, source_key)
  864. # assert dealer.incr_fund(income_type, source_key, withdrawed)
  865. # before_balance = dealer.reload().sub_balance(income_type, source_key)
  866. # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key)
  867. # before_outgoing = len(dealer.inhandWithdrawList)
  868. #
  869. # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO:
  870. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  871. # source_key) + withdrawed * (
  872. # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT)
  873. # else:
  874. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key)
  875. #
  876. # withdraw_service = DealerWithdrawService(payee = dealer,
  877. # income_type = income_type,
  878. # code = None, amount = withdrawed,
  879. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  880. # bank_card_no = merchant.accountCode)
  881. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  882. #
  883. # assert result['result'] == 1
  884. # withdraw_record_id = result['payload']['paymentId']
  885. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  886. # assert withdraw_record
  887. # assert withdraw_record.status == WithdrawStatus.PROCESSING
  888. #
  889. # from apps.web.agent.views import adminAgreeWallet
  890. # url = url_fn(adminAgreeWallet)
  891. # payload = {
  892. # 'orderNo': withdraw_record.order
  893. # }
  894. #
  895. # response = default_agent_client.post_json(url, data = payload)
  896. #
  897. # assert response
  898. #
  899. # import simplejson as json
  900. # json_response = json.loads(response.content)
  901. #
  902. # assert json_response['result'] == 1
  903. #
  904. # dealer.reload()
  905. # assert dealer.sub_balance(income_type, source_key) == balance_if_success
  906. # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  907. # assert before_outgoing == len(dealer.inhandWithdrawList)
  908. #
  909. # withdraw_record.reload()
  910. # assert withdraw_record.status == WithdrawStatus.SUCCEEDED
  911. #
  912. # agent_no_customized.reload()
  913. # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance
  914. #
  915. #
  916. # def test_agent_revoke_agent_withdraw(mocker, dealer, source_key, merchant, default_agent_client, agent_no_customized):
  917. # from apps.web.core.payment.wechat import WechatPaymentGateway
  918. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0])
  919. #
  920. # income_type = AGENT_INCOME_TYPE.DEALER_DEVICE_FEE
  921. #
  922. # withdrawed = RMB(20)
  923. #
  924. # balance_after_withdraw = agent_no_customized.sub_balance(income_type, source_key)
  925. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed)
  926. # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  927. # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  928. # before_outgoing = len(agent_no_customized.inhandWithdrawList)
  929. #
  930. # withdraw_service = AgentWithdrawService(payee = agent_no_customized,
  931. # income_type = income_type,
  932. # code = None, amount = withdrawed,
  933. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  934. # bank_card_no = merchant.accountCode)
  935. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  936. #
  937. # assert result['result'] == 0
  938. # withdraw_record_id = result['payload']['paymentId']
  939. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  940. # assert withdraw_record
  941. # assert withdraw_record.status == WithdrawStatus.FAILED
  942. # assert withdraw_record.role == ROLE.agent
  943. #
  944. # agent_no_customized.reload()
  945. # assert agent_no_customized.sub_balance(income_type, source_key) == balance_after_withdraw
  946. #
  947. # from apps.web.agent.views import revokeWithdrawApplication
  948. # url = url_fn(revokeWithdrawApplication)
  949. # payload = {
  950. # 'orderNo': withdraw_record.order,
  951. # 'reason': u'测试的原因'
  952. # }
  953. #
  954. # response = default_agent_client.post_json(url, data = payload)
  955. #
  956. # assert response
  957. #
  958. # import simplejson as json
  959. # json_response = json.loads(response.content)
  960. #
  961. # assert json_response['result'] == 1
  962. #
  963. # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first()
  964. # assert refund_record
  965. #
  966. # agent_no_customized.reload()
  967. # assert agent_no_customized.sub_balance(income_type, source_key) == before_balance
  968. # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  969. # assert before_outgoing == len(agent_no_customized.inhandWithdrawList)
  970. #
  971. # withdraw_record.reload()
  972. # assert withdraw_record.status == WithdrawStatus.CLOSED
  973. #
  974. #
  975. # def test_agent_revoke_agent_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent,
  976. # agent_no_customized):
  977. # default_agent.payAppWechat.manual_withdraw = True
  978. # default_agent.payAppWechat.save()
  979. # default_agent.payAppWechat.reload()
  980. #
  981. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  982. # withdrawed = RMB(20)
  983. #
  984. # balance_after_withdraw = agent_no_customized.sub_balance(income_type, source_key)
  985. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed)
  986. # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  987. # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  988. # before_outgoing = len(agent_no_customized.inhandWithdrawList)
  989. #
  990. # withdraw_service = AgentWithdrawService(payee = agent_no_customized,
  991. # income_type = income_type,
  992. # code = None, amount = withdrawed,
  993. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  994. # bank_card_no = merchant.accountCode)
  995. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  996. #
  997. # assert result['result'] == 1
  998. #
  999. # agent_no_customized.reload().sub_balance(income_type, source_key) == balance_after_withdraw
  1000. #
  1001. # withdraw_record_id = result['payload']['paymentId']
  1002. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  1003. #
  1004. # assert withdraw_record
  1005. # assert withdraw_record.status == WithdrawStatus.PROCESSING
  1006. #
  1007. # from apps.web.agent.views import revokeWithdrawApplication
  1008. # url = url_fn(revokeWithdrawApplication)
  1009. # payload = {
  1010. # 'orderNo': withdraw_record.order,
  1011. # 'reason': u'测试的原因'
  1012. # }
  1013. #
  1014. # response = default_agent_client.post_json(url, data = payload)
  1015. #
  1016. # assert response
  1017. #
  1018. # import simplejson as json
  1019. # json_response = json.loads(response.content)
  1020. #
  1021. # assert json_response['result'] == 1
  1022. #
  1023. # refund_record = WithdrawRefundRecord.objects(withdraw_record_id = withdraw_record_id).first()
  1024. # assert refund_record
  1025. #
  1026. # agent_no_customized.reload()
  1027. # assert agent_no_customized.sub_balance(income_type, source_key) == before_balance
  1028. # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  1029. # assert before_outgoing == len(agent_no_customized.inhandWithdrawList)
  1030. #
  1031. # withdraw_record.reload()
  1032. # assert withdraw_record.status == WithdrawStatus.CLOSED
  1033. #
  1034. #
  1035. # def test_agent_approve_agent_withdraw(mocker, dealer, source_key, merchant, default_agent, default_agent_client,
  1036. # agent_no_customized):
  1037. # from apps.web.core.payment.wechat import WechatPaymentGateway
  1038. # mocker.patch.object(WechatPaymentGateway, 'withdraw_via_bank', return_value = BANK_WITHDRAW_ERROR[0])
  1039. #
  1040. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  1041. # withdrawed = RMB(20)
  1042. #
  1043. # balance_if_success = agent_no_customized.sub_balance(income_type, source_key)
  1044. # assert agent_no_customized.incr_fund(income_type, source_key, withdrawed)
  1045. # before_balance = agent_no_customized.reload().sub_balance(income_type, source_key)
  1046. # before_frozen_balance = agent_no_customized.sub_frozen_balance(income_type, source_key)
  1047. # before_outgoing = len(agent_no_customized.inhandWithdrawList)
  1048. #
  1049. # withdraw_service = AgentWithdrawService(payee = agent_no_customized,
  1050. # income_type = income_type,
  1051. # code = None, amount = withdrawed,
  1052. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  1053. # bank_card_no = merchant.accountCode)
  1054. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  1055. #
  1056. # assert result['result'] == 0
  1057. # withdraw_record_id = result['payload']['paymentId']
  1058. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  1059. # assert withdraw_record
  1060. # assert withdraw_record.status == WithdrawStatus.FAILED
  1061. #
  1062. # from apps.web.agent.views import adminAgreeWallet
  1063. # url = url_fn(adminAgreeWallet)
  1064. # payload = {
  1065. # 'orderNo': withdraw_record.order
  1066. # }
  1067. #
  1068. # response = default_agent_client.post_json(url, data = payload)
  1069. #
  1070. # assert response
  1071. #
  1072. # import simplejson as json
  1073. # json_response = json.loads(response.content)
  1074. #
  1075. # assert json_response['result'] == 1
  1076. #
  1077. # agent_no_customized.reload()
  1078. # assert agent_no_customized.sub_balance(income_type, source_key) == balance_if_success
  1079. # assert agent_no_customized.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  1080. # assert before_outgoing == len(agent_no_customized.inhandWithdrawList)
  1081. #
  1082. # withdraw_record.reload()
  1083. # assert withdraw_record.status == WithdrawStatus.SUCCEEDED
  1084. #
  1085. #
  1086. # def test_agent_approve_agent_withdraw_manual(mocker, dealer, source_key, merchant, default_agent_client, default_agent,
  1087. # agent_no_customized):
  1088. # default_agent.payAppWechat.manual_withdraw = True
  1089. # default_agent.payAppWechat.save()
  1090. # default_agent.payAppWechat.reload()
  1091. #
  1092. # income_type = DEALER_INCOME_TYPE.DEVICE_INCOME
  1093. # withdrawed = RMB(20)
  1094. #
  1095. # balance_if_success = dealer.sub_balance(income_type, source_key)
  1096. # assert dealer.incr_fund(income_type, source_key, withdrawed)
  1097. # before_balance = dealer.reload().sub_balance(income_type, source_key)
  1098. # before_frozen_balance = dealer.sub_frozen_balance(income_type, source_key)
  1099. # before_outgoing = len(dealer.inhandWithdrawList)
  1100. #
  1101. # if dealer.withdrawFeeRatio > Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO:
  1102. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE,
  1103. # source_key) + withdrawed * (
  1104. # (dealer.withdrawFeeRatio - Const.PLATFORM_DEFAULT_WITHDRAW_FEE_RATIO) / Const.WITHDRAW_FEE_UNIT)
  1105. # else:
  1106. # desired_agent_balance = agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key)
  1107. #
  1108. # withdraw_service = DealerWithdrawService(payee = dealer,
  1109. # income_type = income_type,
  1110. # code = None, amount = withdrawed,
  1111. # pay_type = WITHDRAW_PAY_TYPE.BANK,
  1112. # bank_card_no = merchant.accountCode)
  1113. # result = withdraw_service.execute(gateway_key = source_key, recurrent = False)
  1114. #
  1115. # assert result['result'] == 1
  1116. # withdraw_record_id = result['payload']['paymentId']
  1117. # withdraw_record = WithdrawRecord.objects(id = withdraw_record_id).first() # type: WithdrawRecord
  1118. # assert withdraw_record
  1119. # assert withdraw_record.status == WithdrawStatus.PROCESSING
  1120. #
  1121. # from apps.web.agent.views import adminAgreeWallet
  1122. # url = url_fn(adminAgreeWallet)
  1123. # payload = {
  1124. # 'orderNo': withdraw_record.order
  1125. # }
  1126. #
  1127. # response = default_agent_client.post_json(url, data = payload)
  1128. #
  1129. # assert response
  1130. #
  1131. # import simplejson as json
  1132. # json_response = json.loads(response.content)
  1133. #
  1134. # assert json_response['result'] == 1
  1135. #
  1136. # dealer.reload()
  1137. # assert dealer.sub_balance(income_type, source_key) == balance_if_success
  1138. # assert dealer.sub_frozen_balance(income_type, source_key) == before_frozen_balance
  1139. # assert before_outgoing == len(dealer.inhandWithdrawList)
  1140. #
  1141. # withdraw_record.reload()
  1142. # assert withdraw_record.status == WithdrawStatus.SUCCEEDED
  1143. #
  1144. # agent_no_customized.reload()
  1145. # assert agent_no_customized.sub_balance(AGENT_INCOME_TYPE.DEALER_WITHDRAW_FEE, source_key) == desired_agent_balance