
    %xEiG                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ  G d	 d
e      Z G d de      Z G d de      Zy)z8Tests for withdrawal request and cancellation endpoints.    )Decimal)TestCase)	APIClient)status)User)WithdrawalRequest)Transactionc                   ^    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)WithdrawalRequestTestCasez0Test withdrawal request creation and validation.c                 "   t               | _        t        j                  j	                  dd      | _        t        d      | j
                  _        | j
                  j                          | j                  j                  | j
                         ySet up test data. 	Test Usertelegram_user_id
first_name1000.00000000userN
r   clientr   objectscreate_userr   r   credit_balancesaveforce_authenticateselfs    U/home/cursorai/projects/telegram-earn/backend/apps/payments/tests/test_withdrawals.pysetUpzWithdrawalRequestTestCase.setUp   f    kLL,,#" - 
	 $+?#;		 		&&DII&6    c                 j   | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  |j                  d   d       | j                  t        t        |j                  d               t        d             | j                  |j                  d	   d
       | j                  j                          t        d      t        d      z   t        d      z   }t        d      |z
  }| j                  | j                  j                  |       y)z4Test successful withdrawal creation on Tron network./api/payments/withdrawals/tron      Y@T1234567890abcdef
blockchainamountdestination_addressr*   r+   100.00000000r   	requestedz100.00z1.00z3.001000.00N)r   postassertEqualstatus_coder   HTTP_201_CREATEDdatar   strr   refresh_from_dbr   )r   responseexpected_deductionexpected_balances       r    #test_create_withdrawal_tron_successz=WithdrawalRequestTestCase.test_create_withdrawal_tron_success   s    ;;##$@ #6C
  	--v/F/FG|4f=X]]8%<!=>@WXx0+> 			!!#$X.@76?R"9-0BB113CDr#   c                    | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  |j                  d   d       | j                  j                          t        d      t        d      z
  }| j                  | j                  j                  |       y	)
z3Test successful withdrawal creation on BNB network.r%   bnbg      I@0x1234567890abcdefr)   r*   r/   z52.50N)r   r0   r1   r2   r   r3   r4   r   r6   r   r   )r   r7   r9   s      r    "test_create_withdrawal_bnb_successz<WithdrawalRequestTestCase.test_create_withdrawal_bnb_success.   s    ;;##$@#7C
  	--v/F/FG|4e< 			!!#"9-0@@113CDr#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  d|j                         y)z1Test withdrawal below minimum amount is rejected.r%   r&   g{G#@r(   r)   r+   N)r   r0   r1   r2   r   HTTP_400_BAD_REQUESTassertInr4   r   r7   s     r    $test_create_withdrawal_below_minimumz>WithdrawalRequestTestCase.test_create_withdrawal_below_minimum>   sX    ;;##$@ #6C
  	--v/J/JKh.r#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  dt        |j                               y)z.Test withdrawal exceeding balance is rejected.r%   r&   g     @@r(   r)   zInsufficient balanceN)	r   r0   r1   r2   r   r@   rA   r5   r4   rB   s     r    +test_create_withdrawal_insufficient_balancezEWithdrawalRequestTestCase.test_create_withdrawal_insufficient_balanceI   s^    ;;##$@ #6C
  	--v/J/JK,c(--.@Ar#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         y)z=Test withdrawal at exact balance limit (accounting for fees).r%   r&   g)\؎@r(   r)   N)r   r0   r1   r2   r   r3   rB   s     r    /test_create_withdrawal_exactly_at_balance_limitzIWithdrawalRequestTestCase.test_create_withdrawal_exactly_at_balance_limitT   sH     ;;##$@ #6C
  	--v/F/FGr#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         y)z8Test withdrawal with unsupported blockchain is rejected.r%   ethereumr'   r=   r)   Nr   r0   r1   r2   r   r@   rB   s     r    )test_create_withdrawal_invalid_blockchainzCWithdrawalRequestTestCase.test_create_withdrawal_invalid_blockchaine   sF    ;;##$@$#7C
  	--v/J/JKr#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  |j                  d   d       y)	z5Test blockchain parameter is normalized to lowercase.r%   TRONr'   r(   r)   r*   r&   N)r   r0   r1   r2   r   r3   r4   rB   s     r    2test_create_withdrawal_case_insensitive_blockchainzLWithdrawalRequestTestCase.test_create_withdrawal_case_insensitive_blockchaino   s_    ;;##$@ #6C
  	--v/F/FG|4f=r#   c                    | j                   j                  ddddd      }| j                  |j                  t        j
                         t        j                  j                  | j                  t        j                  t        j                        j                         }| j                  |       | j                  |j                  t        d             y)	z9Test withdrawal creates corresponding transaction record.r%   r&   r'   r(   r)   )r   transaction_typer   104.00000000N)r   r0   r1   r2   r   r3   r	   r   filterr   TYPE_WITHDRAWALSTATUS_PENDINGfirstassertIsNotNoner+   r   )r   r7   txs      r    1test_create_withdrawal_creates_transaction_recordzKWithdrawalRequestTestCase.test_create_withdrawal_creates_transaction_recordz   s    ;;##$@ #6C
  	--v/F/FG   ''(88-- ( 
 %'	 	 	R GN$;<r#   c                    | j                   j                  ddddd      }| j                  |j                  t        j
                         | j                  t        t        |j                  d               t        d             y)z)Test withdrawal with 8-decimal precision.r%   r&   z123.45678901r(   r)   r+   N)	r   r0   r1   r2   r   r3   r   r5   r4   rB   s     r    ,test_create_withdrawal_high_precision_amountzFWithdrawalRequestTestCase.test_create_withdrawal_high_precision_amount   sr    ;;##$@ $#6C
  	--v/F/FGCh/01N#	
r#   c                     | j                   j                  ddddd      }| j                  |j                  t        j
                         y)z/Test withdrawal with empty address is rejected.r%   r&   r'    r)   NrJ   rB   s     r    $test_create_withdrawal_empty_addressz>WithdrawalRequestTestCase.test_create_withdrawal_empty_address   sF    ;;##$@ #%C
  	--v/J/JKr#   c                     t        d      | j                  _        | j                  j                          | j                  j                  ddddd      }| j                  |j                  t        j                         y)z'Test withdrawal with very large amount.z1000000.00000000r%   r&   z	100000.00r(   r)   N)
r   r   r   r   r   r0   r1   r2   r   r3   rB   s     r    (test_create_withdrawal_very_large_amountzBWithdrawalRequestTestCase.test_create_withdrawal_very_large_amount   sf    #*+=#>		 		;;##$@ !#6C
  	--v/F/FGr#   c                     | j                   j                  d       | j                   j                  ddddd      }| j                  |j                  t
        j                         y)z4Test unauthenticated withdrawal request is rejected.Nr   r%   r&   r'   r(   r)   )r   r   r0   r1   r2   r   HTTP_401_UNAUTHORIZEDrB   s     r    &test_create_withdrawal_unauthenticatedz@WithdrawalRequestTestCase.test_create_withdrawal_unauthenticated   s[    &&D&1;;##$@ #6C
  	--v/K/KLr#   N)__name__
__module____qualname____doc__r!   r:   r>   rC   rE   rG   rK   rN   rX   rZ   r]   r_   rb    r#   r    r   r      sN    :	7E&E 	/	BH"L	>=(
LH	Mr#   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)WithdrawalCancellationTestCasez+Test withdrawal cancellation functionality.c                 "   t               | _        t        j                  j	                  dd      | _        t        d      | j
                  _        | j
                  j                          | j                  j                  | j
                         yr   r   r   s    r    r!   z$WithdrawalCancellationTestCase.setUp   r"   r#   c           
         t         j                  j                  | j                  ddt	        d      dt         j
                  ddi      }t	        d      | j                  _        | j                  j                          | j                  j                  d	|j                   d
      }| j                  |j                  t        j                         |j                          | j                  |j                  t         j                          | j                  j                          | j                  | j                  j                  t	        d             y)z1Test cancelling a withdrawal in requested status.r&   USDTr-   r(   total_deductionrQ   r   r*   assetr+   r,   r   metadata896.00000000r%   /r   N)r   r   creater   r   STATUS_REQUESTEDr   r   r   deleteidr1   r2   r   HTTP_200_OKr6   STATUS_CANCELLEDr   
withdrawalr7   s      r     test_cancel_requested_withdrawalz?WithdrawalCancellationTestCase.test_cancel_requested_withdrawal   s    '..55>* 3$55'8 6 

 $+>#:		 		 ;;%%(B:==/QR&ST--v/A/AB 	""$**,=,N,NO 			!!#117?3KLr#   c           
         t         j                  j                  | j                  ddt	        d      dt         j
                  ddi      }| j                  j                  d|j                   d	      }| j                  |j                  t        j                         | j                  d
t        |j                               y)z>Test cancelling a withdrawal in processing status is rejected.r&   rl   r-   r(   rm   rQ   rn   r%   rr   zCannot cancelN)r   r   rs   r   r   STATUS_PROCESSINGr   ru   rv   r1   r2   r   r@   rA   r5   r4   ry   s      r    !test_cancel_processing_withdrawalz@WithdrawalCancellationTestCase.test_cancel_processing_withdrawal   s    &..55>* 3$66'8 6 

 ;;%%(B:==/QR&ST--v/J/JKos8=='9:r#   c           
      <   t         j                  j                  | j                  ddt	        d      dt         j
                  ddi      }| j                  j                  d|j                   d	      }| j                  |j                  t        j                         y
)z.Test cancelling a sent withdrawal is rejected.r&   rl   r-   r(   rm   rQ   rn   r%   rr   N)r   r   rs   r   r   STATUS_SENTr   ru   rv   r1   r2   r   r@   ry   s      r    test_cancel_sent_withdrawalz:WithdrawalCancellationTestCase.test_cancel_sent_withdrawal   s    &..55>* 3$00'8 6 

 ;;%%(B:==/QR&ST--v/J/JKr#   c           
      <   t         j                  j                  | j                  ddt	        d      dt         j
                  ddi      }| j                  j                  d|j                   d	      }| j                  |j                  t        j                         y
)z<Test cancelling an already cancelled withdrawal is rejected.r&   rl   r-   r(   rm   rQ   rn   r%   rr   N)r   r   rs   r   r   rx   r   ru   rv   r1   r2   r   r@   ry   s      r    (test_cancel_already_cancelled_withdrawalzGWithdrawalCancellationTestCase.test_cancel_already_cancelled_withdrawal  s    &..55>* 3$55'8 6 

 ;;%%(B:==/QR&ST--v/J/JKr#   c                    t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d            }t        j                  j                  | j                  |ddt        d      dt        j                  d	di
      }t        d      | j                  _	        | j                  j                          | j                  j                  d|j                   d      }| j                  |j                  t         j"                         |j%                          | j                  |j                   t         j&                         y)z=Test cancelling withdrawal updates linked transaction status.rQ   r   rq   )r   rP   r+   r   balance_beforebalance_afterr&   rl   r-   r(   rm   )r   transactionr*   ro   r+   r,   r   rp   r%   rr   N)r	   r   rs   r   rS   r   rT   r   rt   r   r   r   ru   rv   r1   r2   r   rw   r6   rx   )r   rW   rz   r7   s       r    *test_cancel_withdrawal_updates_transactionzIWithdrawalCancellationTestCase.test_cancel_withdrawal_updates_transaction  s#      ''(88>*--"?3!.1 ( 
 '..55>* 3$55'8 6 	

 $+>#:		 		 ;;%%(B:==/QR&ST--v/A/AB 	K$@$@Ar#   c                     | j                   j                  d      }| j                  |j                  t        j
                         y)z6Test cancelling a non-existent withdrawal returns 404.z /api/payments/withdrawals/99999/N)r   ru   r1   r2   r   HTTP_404_NOT_FOUNDrB   s     r    "test_cancel_nonexistent_withdrawalzAWithdrawalCancellationTestCase.test_cancel_nonexistent_withdrawal@  s3    ;;%%&HI--v/H/HIr#   c           
      j   t         j                  j                  dd      }t        j                  j	                  |ddt        d      dt        j                  dd	i
      }| j                  j                  d|j                   d      }| j                  |j                  t        j                         y)z2Test user cannot cancel another user's withdrawal.d 
Other Userr   r&   rl   r-   r(   rm   rQ   rn   r%   rr   N)r   r   r   r   rs   r   rt   r   ru   rv   r1   r2   r   r   )r   
other_userrz   r7   s       r    !test_cancel_other_user_withdrawalz@WithdrawalCancellationTestCase.test_cancel_other_user_withdrawalF  s    \\--## . 


 '..55>* 3$55'8 6 

 ;;%%(B:==/QR&ST--v/H/HIr#   N)rc   rd   re   rf   r!   r{   r~   r   r   r   r   r   rg   r#   r    ri   ri      s4    5	7M<;"L L !BFJJr#   ri   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	WithdrawalHistoryTestCasez"Test withdrawal history retrieval.c                     t               | _        t        j                  j	                  dd      | _        | j                  j                  | j
                         y)r   r   r   r   r   N)r   r   r   r   r   r   r   r   s    r    r!   zWithdrawalHistoryTestCase.setUp_  sG    kLL,,#" - 
	 	&&DII&6r#   c                     | j                   j                  d      }| j                  |j                  t        j
                         | j                  t        |j                  d         d       y)zTest empty withdrawal history.r%   resultsr   N)r   getr1   r2   r   rw   lenr4   rB   s     r    test_withdrawal_history_emptyz7WithdrawalHistoryTestCase.test_withdrawal_history_emptyh  sO    ;;??#?@--v/A/ABX]]956:r#   c           
         t         j                  j                  | j                  ddt	        d      dt         j
                  ddi       t         j                  j                  | j                  ddt	        d	      d
t         j                  ddi       | j                  j                  d      }| j                  |j                  t        j                         | j                  t        |j                  d         d       y)z#Test retrieving withdrawal history.r&   rl   r-   T1234567890
fee_amount
4.00000000rn   r<   50.000000000x1234567890z
2.50000000r%   r      N)r   r   rs   r   r   rt   r   r   r   r1   r2   r   rw   r   r4   rB   s     r    (test_withdrawal_history_with_withdrawalszBWithdrawalHistoryTestCase.test_withdrawal_history_with_withdrawalso  s    !!((>* -$55"L1 	) 	
 	!!((=) .$00"L1 	) 	
 ;;??#?@--v/A/ABX]]956:r#   c                    t         j                  j                  | j                  ddt	        d      dt         j
                  ddd       | j                  j                  d	      }| j                  t	        t        |j                  d
   d   d               t	        d             | j                  t	        t        |j                  d
   d   d               t	        d             y)z1Test withdrawal history includes fee information.r&   rl   r-   r   r   rQ   )r   rm   rn   r%   r   r   r   
net_amountN)r   r   rs   r   r   rt   r   r   r1   r5   r4   rB   s     r    %test_withdrawal_history_includes_feesz?WithdrawalHistoryTestCase.test_withdrawal_history_includes_fees  s    !!((>* -$55*#1 	) 	
 ;;??#?@X]]9%=a%@%N!OPRYZfRghX]]9%=a%@%N!OPRYZhRijr#   c                    t         j                  j                  dd      }t        j                  j	                  | j
                  ddt        d      dt        j                         t        j                  j	                  |ddt        d	      d
t        j                         | j                  j                  d      }| j                  t        |j                  d         d       | j                  |j                  d   d   d   d       y)z*Test user only sees their own withdrawals.r   r   r   r&   rl   r-   T1111111111r   r*   ro   r+   r,   r   z200.00000000T2222222222r%   r      r   r,   N)r   r   r   r   rs   r   r   rt   r   r   r1   r   r4   )r   r   r7   s      r    'test_withdrawal_history_filters_by_userzAWithdrawalHistoryTestCase.test_withdrawal_history_filters_by_user  s    \\--## . 


 	!!((>* -$55 	) 	
 	!!((>* -$55 	) 	
 ;;??#?@X]]956:y1!45JK][r#   c                 \   ddl m} ddlm} t        j
                  j                  | j                  ddt        d      dt        j                        }|j                          |d	
      z
  |_        |j                          t        j
                  j                  | j                  ddt        d      dt        j                        }| j                  j                  d      }| j!                  |j"                  d   d   d   d       | j!                  |j"                  d   d   d   d       y)z<Test withdrawals are ordered by requested_at (latest first).r   )timezone)	timedeltar&   rl   r-   T_oldr      )daysr   T_newr%   r   r,   r   N)django.utilsr   datetimer   r   r   rs   r   r   r   nowrequested_atr   rt   r   r   r1   r4   )r   r   r   old_withdrawalnew_withdrawalr7   s         r     test_withdrawal_history_orderingz:WithdrawalHistoryTestCase.test_withdrawal_history_ordering  s   )&*2299>* '$00 : 
 '/llnya7H&H#*2299=) '$55 : 
 ;;??#?@ 	y1!45JKWUy1!45JKWUr#   N)
rc   rd   re   rf   r!   r   r   r   r   r   rg   r#   r    r   r   \  s&    ,7;;4k(\:Vr#   r   N)rf   decimalr   django.testr   rest_framework.testr   rest_frameworkr   apps.users.modelsr   apps.payments.modelsr   apps.transactions.modelsr	   r   ri   r   rg   r#   r    <module>r      sO    >    ) ! " 2 0pM pMfYJX YJx{V {Vr#   