test_agent.py 50 KB

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