
    %xEi3                         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mZ  G d d	e      Z G d
 de      Zy)z0Tests for deposit address and history endpoints.    )Decimal)TestCase)	APIClient)status)User)PaymentAddressDepositc                   L    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y)DepositAddressTestCasez.Test deposit address generation and retrieval.c                     t               | _        t        j                  j	                  dd      | _        | j                  j                  | j
                         y)Set up test data. 	Test Usertelegram_user_id
first_nameuserN)r   clientr   objectscreate_userr   force_authenticateselfs    R/home/cursorai/projects/telegram-earn/backend/apps/payments/tests/test_deposits.pysetUpzDepositAddressTestCase.setUp   sG    kLL,,#" - 
	 	&&DII&6    c                    | j                   j                  dddi      }| j                  |j                  t        j
                         | j                  |j                  d   d       | j                  |j                  d   d       | j                  |j                  d   d       | j                  |j                  d   j                  d	             y
)z)Test creating a new Tron deposit address.&/api/payments/addresses/get-or-create/
blockchaintronassetUSDTr   activeaddressTN	r   postassertEqualstatus_coder   HTTP_201_CREATEDdata
assertTrue
startswithr   responses     r   *test_get_or_create_address_tron_first_timezADepositAddressTestCase.test_get_or_create_address_tron_first_time   s    ;;##46"

 	--v/F/FG|4f=w/8x0(;i0;;C@Ar   c                 ,   | j                   j                  dddi      }| j                  |j                  t        j
                         | j                  |j                  d   d       | j                  |j                  d   j                  d             y)z(Test creating a new BNB deposit address.r   r    bnbr%   0xNr'   r/   s     r   )test_get_or_create_address_bnb_first_timez@DepositAddressTestCase.test_get_or_create_address_bnb_first_time%   sx    ;;##45!

 	--v/F/FG|4e<i0;;DABr   c                    | j                   j                  dddi      }|j                  d   }| j                   j                  dddi      }| j                  |j                  t
        j                         | j                  |j                  d   |       t        j                  j                  | j                  d      j                         }| j                  |d       y)z?Test retrieving existing address instead of creating duplicate.r   r    r!   r%   )r   r       N)r   r(   r,   r)   r*   r   HTTP_200_OKr   r   filterr   count)r   first_responsefirst_addresssecond_responseaddress_counts        r   +test_get_or_create_address_returns_existingzBDepositAddressTestCase.test_get_or_create_address_returns_existing0   s     ))46"
 '++I6 ++**46"

 	44f6H6HI--i8-H '..55 6 
 %' 	 	*r   c                     | j                   j                  dddi      }| j                  |j                  t        j
                         y)z&Test error for unsupported blockchain.r   r    ethereumN)r   r(   r)   r*   r   HTTP_400_BAD_REQUESTr/   s     r   -test_get_or_create_address_invalid_blockchainzDDepositAddressTestCase.test_get_or_create_address_invalid_blockchainI   s?    ;;##4:&

 	--v/J/JKr   c                 |   | j                   j                  dddi      }| j                   j                  dddi      }| j                  |j                  t        j
                         | j                  |j                  t        j                         | j                  |j                  d   |j                  d          y)z.Test blockchain parameter is case-insensitive.r   r    TRONTrOnr%   N)r   r(   r)   r*   r   r+   r8   r,   )r   	response1	response2s      r   +test_get_or_create_address_case_insensitivezBDepositAddressTestCase.test_get_or_create_address_case_insensitiveR   s    KK$$46"
	 KK$$46"
	
 	..0G0GH..0B0BC	2INN94MNr   c                 h   | j                   j                  dddi      }| j                   j                  dddi      }| j                  |j                  d   |j                  d          t        j
                  j                  | j                        j                         }| j                  |d       y)	z>Test separate addresses are created for different blockchains.r   r    r!   r3   r%   r      N)
r   r(   assertNotEqualr,   r   r   r9   r   r:   r)   )r   tron_responsebnb_responser>   s       r   2test_get_or_create_address_separate_per_blockchainzIDepositAddressTestCase.test_get_or_create_address_separate_per_blockchaina   s    ((46"
 {{''45!

 	M..y9<;L;LY;WX&..554995EKKM*r   c                     | j                   j                  d       | j                   j                  dddi      }| j                  |j                  t
        j                         y))Test unauthenticated request is rejected.Nr   r   r    r!   )r   r   r(   r)   r*   r   HTTP_401_UNAUTHORIZEDr/   s     r   *test_get_or_create_address_unauthenticatedzADepositAddressTestCase.test_get_or_create_address_unauthenticatedq   sT    &&D&1;;##46"

 	--v/K/KLr   c                    t         j                  j                  | j                  dddt         j                         t         j                  j                  | j                  dddt         j                         | j
                  j                  d      }| j                  |j                  t        j                         | j                  t        |j                  d         d	       y
)z&Test listing user's payment addresses.r!   r#   T1234567890r   r    r"   r%   r   r3   0x1234567890/api/payments/addresses/resultsrK   N)r   r   creater   STATUS_ACTIVEr   getr)   r*   r   r8   lenr,   r/   s     r   test_list_payment_addressesz2DepositAddressTestCase.test_list_payment_addresses{   s     	%%!!// 	& 	
 	%%"!// 	& 	
 ;;??#=>--v/A/ABX]]956:r   c                    t         j                  j                  dd      }t        j                  j	                  | j
                  dddt        j                         t        j                  j	                  |d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 addresses.d 
Other Userr   r!   r#   T1111111111rV   T2222222222rX   rY   r7   r   r%   N)r   r   r   r   rZ   r   r[   r   r\   r)   r]   r,   )r   
other_userr0   s      r   +test_list_payment_addresses_filters_by_userzBDepositAddressTestCase.test_list_payment_addresses_filters_by_user   s    \\--## . 


 	%%!!// 	& 	
 	%%!!// 	& 	
 ;;??#=>X]]956:y1!4Y?Or   N)__name__
__module____qualname____doc__r   r1   r5   r?   rC   rI   rO   rS   r^   re    r   r   r   r      s=    87B	C+2LO+ M;.Pr   r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)DepositHistoryTestCasezTest deposit history retrieval.c                 <   t               | _        t        j                  j	                  dd      | _        t        j                  j                  | j
                  dddt        j                        | _	        | j                  j                  | j
                         y	)
r   r   r   r   r!   r#   rU   rV   r   N)r   r   r   r   r   r   r   rZ   r[   r%   r   r   s    r   r   zDepositHistoryTestCase.setUp   s    kLL,,#" - 
	 &--44!!// 5 
 	&&DII&6r   c                     | j                   j                  d      }| j                  |j                  t        j
                         | j                  t        |j                  d         d       y)zTest empty deposit history./api/payments/deposits/rY   r   N)r   r\   r)   r*   r   r8   r]   r,   r/   s     r   test_deposit_history_emptyz1DepositHistoryTestCase.test_deposit_history_empty   sO    ;;??#<=--v/A/ABX]]956:r   c           
      &   t         j                  j                  | j                  | j                  ddt        d      ddt         j                         t         j                  j                  | j                  | j                  ddt        d      dd	t         j                         | j                  j                  d
      }| j                  |j                  t        j                         | j                  t        |j                  d         d       y)z Test retrieving deposit history.r!   r#   100.000000000xabc123
   r   r%   r    r"   amounttransaction_hashconfirmationsr   z50.500000000xdef456   ro   rY   rK   N)r	   r   rZ   r   r%   r   STATUS_CONFIRMEDSTATUS_PENDINGr   r\   r)   r*   r   r8   r]   r,   r/   s     r   "test_deposit_history_with_depositsz9DepositHistoryTestCase.test_deposit_history_with_deposits   s    LL>*'++ 	 		
 	LL=)')) 	 		
 ;;??#<=--v/A/ABX]]956:r   c           
      ^   t         j                  j                  | j                  | j                  ddt        d      ddt         j                         | j                  j                  d      }| j                  t        t        |j                  d   d	   d
               t        d             y)z2Test deposit amounts maintain 8-decimal precision.r!   r#   z123.45678901rs   rt   ru   ro   rY   r   rv   Nr	   r   rZ   r   r%   r   r{   r   r\   r)   strr,   r/   s     r   test_deposit_history_precisionz5DepositHistoryTestCase.test_deposit_history_precision   s    LL>*'++ 	 		
 ;;??#<=Ci03H=>?N#	
r   c           	         t         j                  j                  dd      }t        j                  j	                  |dddt        j
                        }t        j                  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                  t        t!        |j                  d   d   d               t        d             y)z'Test user only sees their own deposits.r`   ra   r   r!   r#   T9999999999rV   rr   rs   r   r%   r    r"   rv   rw   r   200.00000000ry   ro   rY   r7   r   rv   N)r   r   r   r   rZ   r[   r	   r   r%   r   r{   r   r\   r)   r]   r,   r   )r   rd   other_addressr0   s       r   $test_deposit_history_filters_by_userz;DepositHistoryTestCase.test_deposit_history_filters_by_user   s3   \\--## . 

 '..55!!// 6 
 	LL>*'++ 	 	
 	!>*'++ 	 	
 ;;??#<=X]]956:X]]9%=a%@%J!KLgVdNefr   c           	         ddl m} ddlm} t        j
                  j                  | j                  | j                  ddt        d      dt        j                        }|j                          |d	
      z
  |_        |j                          t        j
                  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)z8Test deposits are ordered by detected_at (latest first).r   )timezone)	timedeltar!   r#   rr   0xoldr   rz   )daysr   0xnewro   rY   rw   r7   N)django.utilsr   datetimer   r	   r   rZ   r   r%   r   r{   nowdetected_atsaver|   r   r\   r)   r,   )r   r   r   old_depositnew_depositr0   s         r   test_deposit_history_orderingz4DepositHistoryTestCase.test_deposit_history_ordering  s   )&oo,,LL>*$++ - 
 #+,,.9!3D"Doo,,LL>*$)) - 
 ;;??#<= 	y1!45GH'Ry1!45GH'Rr   c                 z   g d}t        |      D ]Q  \  }}t        j                  j                  | j                  | j
                  ddt        |dz    d      d| |       S | j                  j                  d      }|j                  d	   D cg c]  }|d
   	 }}|D ]  }| j                  ||        yc c}w )z6Test all deposit status values are returned correctly.)pending	confirmedfailed	duplicater!   r#   r7   z00.00000000r4   r   ro   rY   r   N)	enumerater	   r   rZ   r   r%   r   r   r\   r,   assertIn)r   statusesidxstatus_valuer0   dreturned_statusess          r   "test_deposit_history_status_valuesz9DepositHistoryTestCase.test_deposit_history_status_values?  s    B!*8!4COO""YY!#'+67#%l^!4# #  "5 ;;??#<=2:--	2JK2JQQx[2JK$LMM,(9: % Ls   B8c           	      \   t         j                  j                  | j                  | j                  ddt        d      dt         j                         | j                  j                  d      }| j                  t        t        |j                  d   d   d	               t        d
             y)z)Test handling very large deposit amounts.r!   r#   z999999999.999999990xhuger   ro   rY   r   rv   z1000000000.00000000Nr   r/   s     r   "test_deposit_history_large_amountsz9DepositHistoryTestCase.test_deposit_history_large_amountsT  s    LL/0%++ 	 	
 ;;??#<=Ci03H=>?)*	
r   c                     | j                   j                  d       | j                   j                  d      }| j                  |j                  t
        j                         y)rQ   Nr   ro   )r   r   r\   r)   r*   r   rR   r/   s     r   $test_deposit_history_unauthenticatedz;DepositHistoryTestCase.test_deposit_history_unauthenticatedg  sF    &&D&1;;??#<=--v/K/KLr   N)rf   rg   rh   ri   r   rp   r}   r   r   r   r   r   r   rj   r   r   rl   rl      s7    )7 ;;8
($gLSB;*
&Mr   rl   N)ri   decimalr   django.testr   rest_framework.testr   rest_frameworkr   apps.users.modelsr   apps.payments.modelsr   r	   r   rl   rj   r   r   <module>r      s;    6    ) ! " 8_PX _PD~MX ~Mr   